Skip to content
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

add cli register options #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,24 @@ For the `format` keyword, **JaySchema** allows you to add custom validation func
* `ipv4`: Must be a dotted-quad IPv4 address.
* `ipv6`: Must be a valid IPv6 address as per [RFC 2373 section 2.2](http://tools.ietf.org/html/rfc2373#section-2.2).
* `uri`: As in [RFC 3986 Appendix A](http://tools.ietf.org/html/rfc3986#appendix-A), including relative URIs (no scheme part, fragment-only), with the exception that well-formedness of internal elements, including percent encoding and authority strings, is not verified.

## Cli

```
npm -g install jayschema

jayschema [Options] <instance> [<schema>]
if <schema> is omitted, the <instance> will be validated
against the JSON Schema Draft v4 meta-schema

Examples:
jayschema path/to/instance path/to/schema example without register
jayschema --register path/to/schema/register path/to/instance path/to/schema example with one register
jayschema --register path/to/schema/register1,path/to/file/register2 path/to/instance path/to/schema example to add two registers


Options:
-r, --register register externally-referenced schemas
-h, --help output usage information
-v, --version output version
```
62 changes: 57 additions & 5 deletions bin/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,64 @@ var META_SCHEMA_PATH = path.join(__dirname, '..', 'lib', 'suites', 'draft-04',
'json-schema-draft-v4.json');
var META_SCHEMA = require(META_SCHEMA_PATH);

var instance = process.argv[2];
var schema = process.argv[3] || META_SCHEMA_PATH;
// init yargs
var yargs = require('yargs')
.usage('jayschema [Options] <instance> [<schema>]\n\tif <schema> is omitted, the <instance> will be validated\n\tagainst the JSON Schema Draft v4 meta-schema')
.example("jayschema path/to/instance path/to/schema", "example without register")
.example("jayschema --register path/to/schema/register path/to/instance path/to/schema", "example with one register")
.example("jayschema --register path/to/schema/register1,path/to/file/register2 path/to/instance path/to/schema", "example to add multiple register")
.options('r', {
"alias" : 'register',
"describe" : 'register externally-referenced schemas'
})
.options('h', {
"alias" : 'help',
"describe" : ' output usage information'
})
.options('v', {
"alias" : 'version',
"describe" : ' output version'
})
;
options = yargs.argv;

// read commands <instance> and <schema>
var instance = undefined;
var schema = META_SCHEMA_PATH;
if (options._.length > 0) { instance = options._[0]; }
if (options._.length > 1) { schema = options._[1]; }

var syntax = function() {
console.log('Syntax: jayschema <instance> [<schema>]');
console.log('\tif <schema> is omitted, the <instance> will be validated');
console.log('\tagainst the JSON Schema Draft v4 meta-schema');
yargs.showHelp();
process.exit(0);
};

if (options.h) {
return syntax();
}

if (options.v) {
var version = []
var packagejson = require(path.join(__dirname, '..', 'package.json'));
version.push(packagejson.name)
version.push(" v")
version.push(packagejson.version)
version = version.join("")
console.log(version)
process.exit(0);
}

var registers = [];
if (options.r) {
registers = options.r.split(",");
for ( idx in registers) {
if (!existsSync(registers[idx])) {
console.error('ERR: register', '"' + registers[idx] + '"', 'not found');
return;
}
}
}

if (!instance || !schema) {
return syntax();
}
Expand Down Expand Up @@ -55,6 +104,9 @@ try {
}

var js = new JaySchema();
for (idx in registers) {
js.register(require(registers[idx]));
}

var schemaErrors = js.validate(schemaJson, META_SCHEMA);
if (schemaErrors.length) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"node": ">=0.6.6"
},
"dependencies": {
"yargs": "~1.2.6",
"when": "~3.4.6"
},
"devDependencies": {
Expand Down