forked from ajv-validator/ajv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson-schema.spec.ts
150 lines (141 loc) · 3.74 KB
/
json-schema.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import _Ajv from "../ajv"
import type {JSONSchemaType} from "../.."
import type {SchemaObject} from "../.."
import chai from "../chai"
const should = chai.should()
interface MyData {
foo: string
bar?: number // "boo" should be present if "bar" is present
baz: {
quux: "quux"
[x: string]: string
}
boo?: true
tuple?: [number, string]
arr: {id: number}[]
map: {[K in string]?: number}
notBoo?: string // should not be present if "boo" is present
negativeIfBoo?: number // should be negative if "boo" is present
}
const arrSchema: JSONSchemaType<MyData["arr"]> = {
type: "array",
items: {
type: "object",
properties: {
id: {
type: "integer",
},
},
additionalProperties: false,
required: ["id"],
},
uniqueItems: true,
}
const mySchema: JSONSchemaType<MyData> & {
definitions: {
baz: JSONSchemaType<MyData["baz"]>
tuple: JSONSchemaType<MyData["tuple"]>
}
} = {
type: "object",
definitions: {
baz: {
// schema type is checked here ...
type: "object",
properties: {
quux: {type: "string", const: "quux"},
},
patternProperties: {
abc: {type: "string"},
},
additionalProperties: false,
required: [],
},
tuple: {
// ... and here ...
type: "array",
items: [{type: "number"}, {type: "string"}],
minItems: 2,
additionalItems: false,
nullable: true,
},
},
dependencies: {
bar: ["boo"],
boo: {
not: {required: ["notBoo"]}, // optional properties can be cheched in "required" in PartialSchema
required: ["negativeIfBoo"],
properties: {
// partial properties can be used in partial schemas
negativeIfBoo: {type: "number", nullable: true, exclusiveMaximum: 0},
},
},
},
properties: {
foo: {type: "string"},
bar: {type: "number", nullable: true},
baz: {$ref: "#/definitions/baz"}, // ... but it does not check type here, ...
boo: {
type: "boolean",
nullable: true,
enum: [true, null],
},
tuple: {$ref: "#/definitions/tuple"}, // ... nor here.
arr: arrSchema, // ... The alternative is to define it externally - here it checks type
map: {
type: "object",
required: [],
additionalProperties: {type: "number"},
},
notBoo: {type: "string", nullable: true},
negativeIfBoo: {type: "number", nullable: true},
},
additionalProperties: false,
required: ["foo", "baz", "arr", "map"], // any other property added here won't typecheck
}
describe("JSONSchemaType type and validation as a type guard", () => {
const ajv = new _Ajv()
const validData: unknown = {
foo: "foo",
bar: 1,
baz: {
quux: "quux",
abc: "abc",
},
boo: true,
arr: [{id: 1}, {id: 2}],
tuple: [1, "abc"],
map: {
a: 1,
b: 2,
},
negativeIfBoo: -1,
}
describe("schema has type JSONSchemaType<MyData>", () => {
it("should prove the type of validated data", () => {
const validate = ajv.compile(mySchema)
if (validate(validData)) {
validData.foo.should.equal("foo")
}
should.not.exist(validate.errors)
if (ajv.validate(mySchema, validData)) {
validData.foo.should.equal("foo")
}
should.not.exist(ajv.errors)
})
})
describe("schema has type SchemaObject", () => {
it("should prove the type of validated data", () => {
const schema = mySchema as SchemaObject
const validate = ajv.compile<MyData>(schema)
if (validate(validData)) {
validData.foo.should.equal("foo")
}
should.not.exist(validate.errors)
if (ajv.validate<MyData>(schema, validData)) {
validData.foo.should.equal("foo")
}
should.not.exist(ajv.errors)
})
})
})