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

Fixes #244 - try parsing as JSON if filename is a named pipe/fifo #246

Open
wants to merge 7 commits 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 1 addition & 3 deletions src/commands/ajv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
14 changes: 10 additions & 4 deletions src/commands/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand All @@ -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":
Expand All @@ -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)
}
}
60 changes: 60 additions & 0 deletions test/schema_no_ext
Original file line number Diff line number Diff line change
@@ -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"
]
}
}
34 changes: 34 additions & 0 deletions test/valid_data_no_ext
Original file line number Diff line number Diff line change
@@ -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
}
}
]
21 changes: 19 additions & 2 deletions test/validate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, "")
Expand All @@ -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",
Expand Down
Loading