From 5b80fdcc6e05b9407db1a854a76b905759120554 Mon Sep 17 00:00:00 2001 From: David Biesack Date: Mon, 21 Oct 2024 15:38:28 -0400 Subject: [PATCH 1/7] resolve tsc compiler errors with err.message, err.code --- src/commands/ajv.ts | 11 ++++++----- src/commands/util.ts | 13 +++++++++---- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/commands/ajv.ts b/src/commands/ajv.ts index d06acb5..e8628e1 100644 --- a/src/commands/ajv.ts +++ b/src/commands/ajv.ts @@ -86,12 +86,13 @@ export default function (argv: ParsedArgs): AjvCore { registerer = require("ts-node").register() } catch (err) { /* istanbul ignore next */ - if (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}` - ) + if (err instanceof Error) { + if ((err as NodeJS.ErrnoException).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..d77dd04 100644 --- a/src/commands/util.ts +++ b/src/commands/util.ts @@ -52,8 +52,11 @@ 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 @@ -81,8 +84,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) } } From 63c007888de189e7807ca5b7c8f7b511c037501f Mon Sep 17 00:00:00 2001 From: David Biesack Date: Mon, 21 Oct 2024 16:24:13 -0400 Subject: [PATCH 2/7] resolve tsc compiler errors with err.message, err.code --- src/commands/ajv.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/commands/ajv.ts b/src/commands/ajv.ts index e8628e1..fae22e7 100644 --- a/src/commands/ajv.ts +++ b/src/commands/ajv.ts @@ -85,7 +85,6 @@ export default function (argv: ParsedArgs): AjvCore { try { registerer = require("ts-node").register() } catch (err) { - /* istanbul ignore next */ if (err instanceof Error) { if ((err as NodeJS.ErrnoException).code === "MODULE_NOT_FOUND") { throw new Error( From 031dee08302b97700d7482c832c656f43d305a8d Mon Sep 17 00:00:00 2001 From: David Biesack Date: Mon, 21 Oct 2024 17:06:27 -0400 Subject: [PATCH 3/7] Let file parsing work for named pipes (fifo) Fixes issue #244 Default to parsing JSON if filename (a named fifo) is passed that does not have a recognized extension - parse rather than return `{}` for the schema, which accepts all input. Added code to allow tsc to compile without error when accessing `err.message` etc. --- src/commands/ajv.ts | 10 ++++------ src/commands/util.ts | 1 + 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/commands/ajv.ts b/src/commands/ajv.ts index fae22e7..6fa0b74 100644 --- a/src/commands/ajv.ts +++ b/src/commands/ajv.ts @@ -85,12 +85,10 @@ export default function (argv: ParsedArgs): AjvCore { try { registerer = require("ts-node").register() } catch (err) { - if (err instanceof Error) { - if ((err as NodeJS.ErrnoException).code === "MODULE_NOT_FOUND") { - throw new Error( - `'ts-node' is required for the TypeScript configuration files. Make sure it is installed\nError: ${err.message}` - ) - } + 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 d77dd04..44d8dc2 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": From beb2d09e2287bd2f0c920e1ff2c8b9265486e8f9 Mon Sep 17 00:00:00 2001 From: David Biesack Date: Mon, 21 Oct 2024 17:09:54 -0400 Subject: [PATCH 4/7] increment package patch version to 5.0.1 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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", From 69ce61e6c21e839f55c0d661f4ecb3d17d38dca0 Mon Sep 17 00:00:00 2001 From: David Biesack Date: Mon, 21 Oct 2024 17:23:27 -0400 Subject: [PATCH 5/7] address eslint issues --- src/commands/util.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/util.ts b/src/commands/util.ts index 44d8dc2..abf31f8 100644 --- a/src/commands/util.ts +++ b/src/commands/util.ts @@ -53,9 +53,8 @@ export function openFile(filename: string, suffix: string): any { json = require(file) } } catch (err) { - if (err instanceof Error) { - const msg: string = err.message; + const msg: string = err.message console.error(`error: ${msg.replace(" module", " " + suffix)}`) } process.exit(2) @@ -63,6 +62,7 @@ export function openFile(filename: string, suffix: string): any { 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": From da0ccbf2f395ba70e5a0f0fec0495c9b255e9748 Mon Sep 17 00:00:00 2001 From: David Biesack Date: Tue, 22 Oct 2024 11:56:45 -0400 Subject: [PATCH 6/7] add test cases for file names with no extension - assume JSON --- test/schema_no_ext | 60 ++++++++++++++++++++++++++++++++++++++++++ test/valid_data_no_ext | 34 ++++++++++++++++++++++++ test/validate.spec.ts | 17 ++++++++++-- 3 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 test/schema_no_ext create mode 100644 test/valid_data_no_ext 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..f90525f 100644 --- a/test/validate.spec.ts +++ b/test/validate.spec.ts @@ -42,8 +42,20 @@ 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 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() + }) + 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() + }) + 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 +75,7 @@ describe("validate", function () { ) }) + it("should validate invalid data", (done) => { cli( "-s test/schema.json -d test/invalid_data.json --errors=line", From 43b8139c36deaab2f15ff328d01ace5fd7726fd2 Mon Sep 17 00:00:00 2001 From: David Biesack Date: Tue, 22 Oct 2024 12:18:16 -0400 Subject: [PATCH 7/7] split out new tests with separate it functions --- test/validate.spec.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/validate.spec.ts b/test/validate.spec.ts index f90525f..4d71a40 100644 --- a/test/validate.spec.ts +++ b/test/validate.spec.ts @@ -42,19 +42,23 @@ describe("validate", function () { }) }) - it('should try JSON format if file has no extension', (done) => { + 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)