From c9d69d07e196714b05ca015a64aa03198c54057f Mon Sep 17 00:00:00 2001 From: Alban Minassian Date: Thu, 22 Jan 2015 13:46:46 +0100 Subject: [PATCH] add cli register options --- README.md | 21 +++++++++++++++++ bin/validate.js | 62 +++++++++++++++++++++++++++++++++++++++++++++---- package.json | 1 + 3 files changed, 79 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8609385..f758cca 100644 --- a/README.md +++ b/README.md @@ -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] [] + if is omitted, the 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 +``` \ No newline at end of file diff --git a/bin/validate.js b/bin/validate.js index 133e618..7a4a6eb 100644 --- a/bin/validate.js +++ b/bin/validate.js @@ -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] []\n\tif is omitted, the 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 and +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 []'); - console.log('\tif is omitted, the 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(); } @@ -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) { diff --git a/package.json b/package.json index 6df5379..c8bab0d 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ "node": ">=0.6.6" }, "dependencies": { + "yargs": "~1.2.6", "when": "~3.4.6" }, "devDependencies": {