Skip to content

Commit dba04a1

Browse files
committed
support JSON Type Definition
1 parent 810ea8e commit dba04a1

File tree

12 files changed

+101
-40
lines changed

12 files changed

+101
-40
lines changed

README.md

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ npm install -g ajv-cli
2929

3030
## JSON schema version
3131

32-
Parameter `--spec` can be used with all commands (other than help) to define which JSON schema version is used:
32+
Parameter `--spec` can be used with all commands (other than help) to define which JSON schema language is used:
3333

3434
- `--spec=draft7` (default) - support JSON Schema draft-07 (uses `import Ajv from "ajv"`)
3535
- `--spec=draft2019` - support JSON Schema draft-2019-09 (uses `import Ajv from "ajv/dist/2019"`)
36+
- `--spec=jtd` - support JSON Type Definition (uses `import Ajv from "ajv/dist/jtd"`)
3637

3738
## Commands
3839

@@ -148,7 +149,7 @@ This command also supports parameters `-r`, `-m` and `-c` as in [validate](#vali
148149

149150
This command validates and migrates schema from JSON Schema draft-04 to JSON Schema draft-07 or draft-2019-09 using [json-schema-migrate](https://github.com/epoberezkin/json-schema-migrate) package.
150151

151-
The [version of JSON Schema](#json-schema-version) is determined by `--spec` parameter (`"draft7"` or `"draft2019"`).
152+
The [version of JSON Schema](#json-schema-version) is determined by `--spec` parameter (only `"draft7"` or `"draft2019"`).
152153

153154
```sh
154155
ajv migrate -s schema
@@ -202,36 +203,36 @@ This command supports the same options and parameters as [validate](#validate-da
202203

203204
You can pass the following [Ajv options](https://github.com/ajv-validator/ajv/blob/master/docs/api.md#options):
204205

205-
| Option | Description |
206-
| ---------------------------- | ------------------------------------------------------------------------- |
207-
| Strict mode |
208-
| `--strict=false` | disable [strict mode](https://github.com/ajv-validator/ajv/blob/master/docs/strict-mode.md) |
209-
| `--strict-tuples=` | throw on (`true`) or ignore (`false`) [strict tuples](https://github.com/ajv-validator/ajv/blob/master/docs/strict-mode.md#prohibit-unconstrained-tuples) restrictions (the default is to log) |
210-
| `--strict-types=` | throw on (`true`) or ignore (`false`) [strict types](https://github.com/ajv-validator/ajv/blob/master/docs/strict-mode.md#strict-types) restrictions (the default is to log) |
211-
| `--allow-matching-properties`| allow `properties` [matching patterns](https://github.com/ajv-validator/ajv/blob/master/docs/strict-mode.md#prohibit-overlap-between-properties-and-patternproperties-keywords) in `patternProperties` |
212-
| `--allow-union-types` | allow [union types](https://github.com/ajv-validator/ajv/blob/master/docs/strict-mode.md#prohibit-union-types) |
213-
| `--validate-formats=false` | disable format validation |
214-
| Validation and reporting |
215-
| `--data` | use [$data references](https://github.com/ajv-validator/ajv/blob/master/docs/validation.md#data-reference) |
216-
| `--all-errors` | collect all validation errors |
217-
| `--verbose` | include schema and data in errors |
218-
| `--comment` | log schema `$comment`s |
219-
| `--inline-refs=` | referenced schemas compilation mode (true/false/\<number\>) |
220-
| Modify validated data |
221-
| `--remove-additional` | remove additional properties (true/all/failing) |
222-
| `--use-defaults` | replace missing properties/items with the values from default keyword |
223-
| `--coerce-types` | change type of data to match type keyword |
224-
| Advanced |
225-
| `--multiple-of-precision` | precision of multipleOf, pass integer number |
226-
| `--messages=false` | do not include text messages in errors |
227-
| `--loop-required=` | max size of `required` to compile to expression (rather than to loop) |
228-
| `--loop-enum=` | max size of `enum` to compile to expression (rather than to loop) |
229-
| `--own-properties` | only validate own properties (not relevant for JSON, but can have effect for JavaScript objects) |
230-
| Code generation |
231-
| `--code-es5` | generate ES5 code |
232-
| `--code-lines` | generate multi-line code |
233-
| `--code-optimize=` | disable optimization (`false`) or number of optimization passes (1 pass by default) |
234-
| `--code-formats=` | code to require formats object (only needed if you generate standalone code and do not use [ajv-formats](https://github.com/ajv-validator/ajv-formats)) |
206+
| Option | Description |
207+
| ----------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
208+
| Strict mode |
209+
| `--strict=false` | disable [strict mode](https://github.com/ajv-validator/ajv/blob/master/docs/strict-mode.md) |
210+
| `--strict-tuples=` | throw on (`true`) or ignore (`false`) [strict tuples](https://github.com/ajv-validator/ajv/blob/master/docs/strict-mode.md#prohibit-unconstrained-tuples) restrictions (the default is to log) |
211+
| `--strict-types=` | throw on (`true`) or ignore (`false`) [strict types](https://github.com/ajv-validator/ajv/blob/master/docs/strict-mode.md#strict-types) restrictions (the default is to log) |
212+
| `--allow-matching-properties` | allow `properties` [matching patterns](https://github.com/ajv-validator/ajv/blob/master/docs/strict-mode.md#prohibit-overlap-between-properties-and-patternproperties-keywords) in `patternProperties` |
213+
| `--allow-union-types` | allow [union types](https://github.com/ajv-validator/ajv/blob/master/docs/strict-mode.md#prohibit-union-types) |
214+
| `--validate-formats=false` | disable format validation |
215+
| Validation and reporting |
216+
| `--data` | use [$data references](https://github.com/ajv-validator/ajv/blob/master/docs/validation.md#data-reference) |
217+
| `--all-errors` | collect all validation errors |
218+
| `--verbose` | include schema and data in errors |
219+
| `--comment` | log schema `$comment`s |
220+
| `--inline-refs=` | referenced schemas compilation mode (true/false/\<number\>) |
221+
| Modify validated data |
222+
| `--remove-additional` | remove additional properties (true/all/failing) |
223+
| `--use-defaults` | replace missing properties/items with the values from default keyword |
224+
| `--coerce-types` | change type of data to match type keyword |
225+
| Advanced |
226+
| `--multiple-of-precision` | precision of multipleOf, pass integer number |
227+
| `--messages=false` | do not include text messages in errors |
228+
| `--loop-required=` | max size of `required` to compile to expression (rather than to loop) |
229+
| `--loop-enum=` | max size of `enum` to compile to expression (rather than to loop) |
230+
| `--own-properties` | only validate own properties (not relevant for JSON, but can have effect for JavaScript objects) |
231+
| Code generation |
232+
| `--code-es5` | generate ES5 code |
233+
| `--code-lines` | generate multi-line code |
234+
| `--code-optimize=` | disable optimization (`false`) or number of optimization passes (1 pass by default) |
235+
| `--code-formats=` | code to require formats object (only needed if you generate standalone code and do not use [ajv-formats](https://github.com/ajv-validator/ajv-formats)) |
235236

236237
Options can be passed using either dash-case or camelCase.
237238

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"build": "rimraf dist && tsc",
77
"prepublish": "npm run build",
88
"eslint": "eslint \"src/**/*.*s\" \"test/**/*.js\"",
9-
"prettier:write": "prettier --write \"./**/*.{md,json,yaml,js,ts}\"",
9+
"prettier:write": "prettier --write \"./**/*.{json,yaml,js,ts}\"",
1010
"prettier:check": "prettier --list-different \"./**/*.{md,json,yaml,js,ts}\"",
1111
"test-spec": "cross-env TS_NODE_PROJECT=test/tsconfig.json mocha -r ts-node/register \"test/**/*.spec.{ts,js}\" -R spec",
1212
"test-cov": "nyc npm run test-spec",
@@ -34,7 +34,7 @@
3434
"url": "https://github.com/ajv-validator/ajv-cli"
3535
},
3636
"dependencies": {
37-
"ajv": "^7.1.0",
37+
"ajv": "^7.2.1",
3838
"fast-json-patch": "^2.0.0",
3939
"glob": "^7.1.0",
4040
"js-yaml": "^3.14.0",

src/commands/ajv.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type AjvCore from "ajv/dist/core"
22
import type {ParsedArgs} from "minimist"
33
import Ajv7, {Plugin} from "ajv"
44
import Ajv2019 from "ajv/dist/2019"
5+
import AjvJTD from "ajv/dist/jtd"
56
import {Service} from "ts-node"
67
import {getOptions} from "./options"
78
import * as util from "./util"
@@ -18,13 +19,19 @@ const interopRequireDefault = (obj: any): {default: any} =>
1819
const importDefault = <T = unknown>(moduleName: string): T =>
1920
interopRequireDefault(require(moduleName)).default
2021

22+
const AjvClass: {[S in string]?: typeof AjvCore} = {
23+
jtd: AjvJTD,
24+
draft7: Ajv7,
25+
draft2019: Ajv2019,
26+
}
27+
2128
export default function (argv: ParsedArgs): AjvCore {
2229
const opts = getOptions(argv)
2330
if (argv.o) opts.code.source = true
24-
const Ajv: typeof AjvCore = argv.spec === "draft2019" ? Ajv2019 : Ajv7
31+
const Ajv: typeof AjvCore = AjvClass[argv.spec] || Ajv7
2532
const ajv = new Ajv(opts)
2633
let invalid: boolean | undefined
27-
ajv.addMetaSchema(draft6metaSchema)
34+
if (argv.spec !== "jtd") ajv.addMetaSchema(draft6metaSchema)
2835
addSchemas(argv.m, "addMetaSchema", "meta-schema")
2936
addSchemas(argv.r, "addSchema", "schema")
3037
customFormatsKeywords(argv.c)

src/commands/compile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const cmd: Command = {
1717
m: {$ref: "#/$defs/stringOrArray"},
1818
c: {$ref: "#/$defs/stringOrArray"},
1919
o: {anyOf: [{type: "string", format: "notGlob"}, {type: "boolean"}]},
20-
spec: {enum: ["draft7", "draft2019"]},
20+
spec: {enum: ["draft7", "draft2019", "jtd"]},
2121
},
2222
ajvOptions: true,
2323
},

src/commands/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const cmd: Command = {
2121
valid: {type: "boolean"},
2222
invalid: {type: "boolean", enum: [true]},
2323
errors: {enum: ["json", "line", "text", "js", "no"]},
24-
spec: {enum: ["draft7", "draft2019"]},
24+
spec: {enum: ["draft7", "draft2019", "jtd"]},
2525
},
2626
ajvOptions: true,
2727
},

src/commands/validate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const cmd: Command = {
2020
c: {$ref: "#/$defs/stringOrArray"},
2121
errors: {enum: ["json", "line", "text", "js", "no"]},
2222
changes: {enum: [true, "json", "line", "js"]},
23-
spec: {enum: ["draft7", "draft2019"]},
23+
spec: {enum: ["draft7", "draft2019", "jtd"]},
2424
},
2525
ajvOptions: true,
2626
},

test/compile.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ describe("compile", function () {
172172
done()
173173
})
174174
})
175+
176+
it("should compile JTD schema", (done) => {
177+
cli("compile -s test/jtd/schema --spec=jtd", (error, stdout, stderr) => {
178+
assert.strictEqual(error, null)
179+
assertValid(stdout, 1)
180+
assert.strictEqual(stderr, "")
181+
done()
182+
})
183+
})
175184
})
176185

177186
function assertValid(stdout: string, count: number): void {

test/custom/invalid_custom_ts.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export = [];
1+
export = []

test/jtd/data.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "a",
3+
"a": 42
4+
}

test/jtd/invalid_data.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "b",
3+
"a": 42
4+
}

0 commit comments

Comments
 (0)