diff --git a/package.json b/package.json index 1ac8acf..2c543b7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ajv-cli", - "version": "5.0.0", + "version": "5.0.1", "description": "Command line interface for Ajv JSON schema validator", "scripts": { "build": "rimraf dist && tsc", diff --git a/src/commands/ajv.ts b/src/commands/ajv.ts index d06acb5..6fa0b74 100644 --- a/src/commands/ajv.ts +++ b/src/commands/ajv.ts @@ -85,13 +85,11 @@ export default function (argv: ParsedArgs): AjvCore { try { registerer = require("ts-node").register() } catch (err) { - /* istanbul ignore next */ - if (err.code === "MODULE_NOT_FOUND") { + if (err instanceof Error && "code" in err && err.code === "MODULE_NOT_FOUND") { throw new Error( `'ts-node' is required for the TypeScript configuration files. Make sure it is installed\nError: ${err.message}` ) } - throw err } diff --git a/src/commands/util.ts b/src/commands/util.ts index 42b4ed1..abf31f8 100644 --- a/src/commands/util.ts +++ b/src/commands/util.ts @@ -29,6 +29,7 @@ function getFormatFromFileName(filename: string): string { function decodeFile(contents: string, format: string): any { switch (format) { case "json": + case "": return JSON.parse(contents) case "jsonc": case "json5": @@ -52,13 +53,16 @@ export function openFile(filename: string, suffix: string): any { json = require(file) } } catch (err) { - const msg: string = err.message - console.error(`error: ${msg.replace(" module", " " + suffix)}`) + if (err instanceof Error) { + const msg: string = err.message + console.error(`error: ${msg.replace(" module", " " + suffix)}`) + } process.exit(2) } return json } +// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types export function logJSON(mode: string, data: any, ajv?: Ajv): string { switch (mode) { case "json": @@ -81,8 +85,10 @@ export function compile(ajv: Ajv, schemaFile: string): AnyValidateFunction { try { return ajv.compile(schema) } catch (err) { - console.error(`schema ${schemaFile} is invalid`) - console.error(`error: ${err.message}`) + if (err instanceof Error) { + console.error(`schema ${schemaFile} is invalid`) + console.error(`error: ${err.message}`) + } process.exit(1) } } diff --git a/test/schema_no_ext b/test/schema_no_ext new file mode 100644 index 0000000..711fed8 --- /dev/null +++ b/test/schema_no_ext @@ -0,0 +1,60 @@ +{ + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "schema.json", + "description": "basic schema from z-schema benchmark (https://github.com/zaggino/z-schema)", + "title": "Product set", + "type": "array", + "items": { + "title": "Product", + "type": "object", + "additionalProperties": false, + "properties": { + "id": { + "description": "The unique identifier for a product", + "type": "number" + }, + "name": { + "type": "string" + }, + "price": { + "type": "number", + "exclusiveMinimum": 0 + }, + "tags": { + "type": "array", + "items": { + "type": "string" + }, + "minItems": 1, + "uniqueItems": true + }, + "dimensions": { + "type": "object", + "properties": { + "length": { + "type": "number" + }, + "width": { + "type": "number" + }, + "height": { + "type": "number" + } + }, + "required": [ + "length", + "width", + "height" + ] + }, + "warehouseLocation": { + "description": "Coordinates of the warehouse with the product" + } + }, + "required": [ + "id", + "name", + "price" + ] + } +} diff --git a/test/valid_data_no_ext b/test/valid_data_no_ext new file mode 100644 index 0000000..90fe498 --- /dev/null +++ b/test/valid_data_no_ext @@ -0,0 +1,34 @@ +[ + { + "id": 2, + "name": "An ice sculpture", + "price": 12.5, + "tags": [ + "cold", + "ice" + ], + "dimensions": { + "length": 7, + "width": 12, + "height": 9.5 + }, + "warehouseLocation": { + "latitude": -78.75, + "longitude": 20.4 + } + }, + { + "id": 3, + "name": "A blue mouse", + "price": 25.5, + "dimensions": { + "length": 3.1, + "width": 1, + "height": 1 + }, + "warehouseLocation": { + "latitude": 54.4, + "longitude": -32.7 + } + } +] diff --git a/test/validate.spec.ts b/test/validate.spec.ts index 109f91e..4d71a40 100644 --- a/test/validate.spec.ts +++ b/test/validate.spec.ts @@ -42,8 +42,24 @@ describe("validate", function () { }) }) - it('should validate valid data with the "jsonc" extension', (done) => { - cli("-s test/schema -d test/valid_data.jsonc", (error, stdout, stderr) => { + it("should try JSON format if data file has no extension", (done) => { + cli("-s test/schema -d test/valid_data_no_ext", (error, stdout, stderr) => { + assert.strictEqual(error, null) + assertValid(stdout, 1) + assert.strictEqual(stderr, "") + done() + }) + }) + it("should try JSON format if schema file has no extension", (done) => { + cli("-s test/schema_no_ext -d test/valid_data.json", (error, stdout, stderr) => { + assert.strictEqual(error, null) + assertValid(stdout, 1) + assert.strictEqual(stderr, "") + done() + }) + }) + it("should try JSON format if schema and data file have no extension", (done) => { + cli("-s test/schema_no_ext -d test/valid_data_no_ext", (error, stdout, stderr) => { assert.strictEqual(error, null) assertValid(stdout, 1) assert.strictEqual(stderr, "") @@ -63,6 +79,7 @@ describe("validate", function () { ) }) + it("should validate invalid data", (done) => { cli( "-s test/schema.json -d test/invalid_data.json --errors=line",