Skip to content

feat/merge-upstream #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 25 commits into
base: feat/merge-upstream
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,26 @@ Options:
I created this schema generator to validate JSON responses from APIs. As the JSON API is enhanced and nodes are added or removed from the response, the schema is regenerated and validated against the newly deployed API.

### Tests
Install [mocha](https://github.com/mochajs/mocha) globally (as cli) and run `npm test`.
To run tests, including fetching documents via HTTP, we've added [node-stubby-server-cli](https://github.com/krg7880/node-stubby-server-cli) to help with serving mock data. The ports for the stub server is defined under *test/helpers/stubby-cli*, in the event the default port is in use, you can change them there.

```bash
npm install -g stubby
```

Install [mocha](https://github.com/mochajs/mocha) globally (as cli) and run

```bash
npm test
```

### Validating Documents
JSON documents can be validated against schemas using [chai-json-schema](http://chaijs.com/plugins/chai-json-schema). See the tests under [test](https://github.com/krg7880/json-schema-generator/tree/master/test) for example usage.

### Contributors
Thanks to those who have contributed. These kind folks are listed below:

* [nickyout](https://github.com/nickyout)
* [Edward Silverton](https://github.com/edsilv)
* [Tschef](https://github.com/Tschef)


4 changes: 4 additions & 0 deletions bin/cli.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#!/usr/bin/env node

console.log('Node');

var url = require('url'),
path = require('path'),
cliConsole = require('../lib/cli/console'),
Expand Down Expand Up @@ -166,6 +169,7 @@ handleInput(config.src, function(jsonString) {
handleOutput(config.dest, jsonSchemaString);
})
.catch(function(e) {
console.log(e);
errorHandler(e.toString('utf8'));
});
});
100 changes: 5 additions & 95 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ equal.

* **Object**

## buildPrimative(tree, node)
## buildPrimitive(tree, node)

Inspect primitatives and apply the correct type
and mark as required if the element contains a value.
Expand All @@ -52,7 +52,7 @@ and mark as required if the element contains a value.

### Return:

* **void**
* void

## buildObject(tree, node)

Expand Down Expand Up @@ -87,7 +87,7 @@ given JSON document.

### Return:

* **void**
* void

<!-- End lib/ast.js -->

Expand All @@ -113,7 +113,7 @@ Generates a JSON schema based on the provided AST tree.

### Return:

* **void**
* void

## compile(tree)

Expand All @@ -126,7 +126,7 @@ JSON schema.

### Return:

* **void**
* void

<!-- End lib/compiler.js -->

Expand All @@ -135,96 +135,6 @@ JSON schema.

<!-- Start lib/index.js -->

## jsondir

Specify a directory where a remote
JSON will be stored after downloading
the resource.

CLI Flag
--jsondir

## schemadir

Specify a directory where the generated
schema will be stored.

CLI Flag
--schemadir

## file

Specify a local JSON document that is
used to generate the schema.

CLI Flag
--file

## url

Specfiy a remote JSON document to fetch
and generate it's schema.

CLI Flag
--url

## fetchResource(url)

Fetches a remote JSON document and generate
the schema based on the contents of the remote
resource. If an output directory is specified,
the document will be saved locally.

### Params:

* **String** *url* The location of the remote resource

### Return:

* **void**

## readFile(filepath)

Reads the specified JSON file from the
filesystem which is used to generate
the schema.

### Params:

* **String** *filepath* Path to JSON document to load.

### Return:

* **void**

## writeFile(contents, file)

Writes the given data to the specified
file location.

### Params:

* **String** *contents* The data to write to the FS
* **String** *file* The file to write the data to

### Return:

* **void**

## getName(str)

Get the name of the JSON resource so the schema
matches the source. The .json extension is added
if it's missing from the filename.

### Params:

* **String** *str* File name

### Return:

* **String** The name of the file only.

<!-- End lib/index.js -->


Expand Down
48 changes: 47 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,45 @@
var markdox = require("gulp-markdox");
var gulp = require('gulp');
var mocha = require('gulp-mocha');
var concat = require("gulp-concat");
var Stubby = require('node-stubby-server-cli').CLI;
var Events = Stubby.Events;
var path = require('path');
var stubbyHelper = require(path.normalize(__dirname + '/test/helpers/stubby-cli'));



gulp.task('start:stubby', function(next) {
var cli = new Stubby();
cli.admin(stubbyHelper.ports.admin)
.stubs(stubbyHelper.ports.stubs)
.tls(stubbyHelper.ports.tls)
.data(path.normalize(__dirname + '/test/fixtures/stubby/routes.json'))
.unmute()

cli.once('LISTENING', function() {
next();
}).start();
});


gulp.task('test', ['start:stubby'], function() {
return gulp.src('test/**/*-test.js')
.pipe(mocha({reporter: 'nyan'}))
.once('error', function(e) {
process.exit(1);
})
.once('end', function() {
process.exit(0)
});
});


gulp.task('stop:stubby', ['test'], function(next) {
cli.kill();
next();
});


gulp.task("doc", function(){
gulp.src("./lib/*.js")
Expand All @@ -9,4 +48,11 @@ gulp.task("doc", function(){
.pipe(gulp.dest("./doc"));
});

gulp.task('default', ['doc']);

// Default tasks to run
gulp.task('default', [
'start:stubby',
'test',
'stop:stubby',
'doc'
]);
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
require('./lib/index');
module.exports = require('./lib/index');
6 changes: 3 additions & 3 deletions lib/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ AST.prototype.generateHash = function(value) {
if (utils.isObject(value)) {
var keys = Object.keys(value);
return crypto.createHash("md5").update(JSON.stringify(keys)).digest("hex");
} else if (utils.Array(value)) {
} else if (utils.isArray(value)) {
return crypto.createHash("md5").update(JSON.stringify(value)).digest("hex");
} else {
return crypto.createHash("md5").update(value).digest("hex");
Expand Down Expand Up @@ -152,7 +152,7 @@ AST.prototype.buildArrayTree = function(tree, node) {
if (node[i].length > 0) {
tree.children[i].required = true;
}
tree.buildArrayTree(tree.children[i], node[i]);
this.buildArrayTree(tree.children[i], node[i]);
} else {
if (tree.type === 'object') {
tree.children[i] = {};
Expand All @@ -177,4 +177,4 @@ AST.prototype.build = function(json) {
}
};

module.exports = AST;
module.exports = AST;
Loading