| 1 |
import _Ajv from "../ajv" |
| 2 |
import type {JSONSchemaType} from "../.." |
| 3 |
import type {SchemaObject} from "../.." |
| 4 |
import chai from "../chai" |
| 5 |
const should = chai.should() |
| 6 |
|
| 7 |
interface MyData { |
| 8 |
foo: string |
| 9 |
bar?: number // "boo" should be present if "bar" is present |
| 10 |
baz: { |
| 11 |
quux: "quux" |
| 12 |
[x: string]: string |
| 13 |
} |
| 14 |
boo?: true |
| 15 |
tuple?: readonly [number, string] |
| 16 |
arr: {id: number}[] |
| 17 |
map: {[K in string]?: number} |
| 18 |
notBoo?: string // should not be present if "boo" is present |
| 19 |
negativeIfBoo?: number // should be negative if "boo" is present |
| 20 |
} |
| 21 |
|
| 22 |
const arrSchema: JSONSchemaType<MyData["arr"]> = { |
| 23 |
type: "array", |
| 24 |
items: { |
| 25 |
type: "object", |
| 26 |
properties: { |
| 27 |
id: { |
| 28 |
type: "integer", |
| 29 |
}, |
| 30 |
}, |
| 31 |
additionalProperties: false, |
| 32 |
required: ["id"], |
| 33 |
}, |
| 34 |
uniqueItems: true, |
| 35 |
} as const |
| 36 |
|
| 37 |
const mySchema: JSONSchemaType<MyData> & { |
| 38 |
definitions: { |
| 39 |
baz: JSONSchemaType<MyData["baz"]> |
| 40 |
tuple: JSONSchemaType<MyData["tuple"]> |
| 41 |
} |
| 42 |
} = { |
| 43 |
type: "object", |
| 44 |
definitions: { |
| 45 |
baz: { |
| 46 |
// schema type is checked here ... |
| 47 |
type: "object", |
| 48 |
properties: { |
| 49 |
quux: {type: "string", const: "quux"}, |
| 50 |
}, |
| 51 |
patternProperties: { |
| 52 |
abc: {type: "string"}, |
| 53 |
}, |
| 54 |
additionalProperties: false, |
| 55 |
required: [], |
| 56 |
}, |
| 57 |
tuple: { |
| 58 |
// ... and here ... |
| 59 |
type: "array", |
| 60 |
items: [{type: "number"}, {type: "string"}], |
| 61 |
minItems: 2, |
| 62 |
additionalItems: false, |
| 63 |
nullable: true, |
| 64 |
}, |
| 65 |
}, |
| 66 |
dependencies: { |
| 67 |
bar: ["boo"], |
| 68 |
boo: { |
| 69 |
not: {required: ["notBoo"]}, // optional properties can be cheched in "required" in PartialSchema |
| 70 |
required: ["negativeIfBoo"], |
| 71 |
properties: { |
| 72 |
// partial properties can be used in partial schemas |
| 73 |
negativeIfBoo: {type: "number", nullable: true, exclusiveMaximum: 0}, |
| 74 |
}, |
| 75 |
}, |
| 76 |
}, |
| 77 |
properties: { |
| 78 |
foo: {type: "string"}, |
| 79 |
bar: {type: "number", nullable: true}, |
| 80 |
baz: {$ref: "#/definitions/baz"}, // ... but it does not check type here, ... |
| 81 |
boo: { |
| 82 |
type: "boolean", |
| 83 |
nullable: true, |
| 84 |
enum: [true, null], |
| 85 |
}, |
| 86 |
tuple: {$ref: "#/definitions/tuple"}, // ... nor here. |
| 87 |
arr: arrSchema, // ... The alternative is to define it externally - here it checks type |
| 88 |
map: { |
| 89 |
type: "object", |
| 90 |
required: [], |
| 91 |
additionalProperties: {type: "number"}, |
| 92 |
}, |
| 93 |
notBoo: {type: "string", nullable: true}, |
| 94 |
negativeIfBoo: {type: "number", nullable: true}, |
| 95 |
}, |
| 96 |
additionalProperties: false, |
| 97 |
required: ["foo", "baz", "arr", "map"], // any other property added here won't typecheck |
| 98 |
} as const |
| 99 |
|
| 100 |
type MyUnionData = {a: boolean} | string | number |
| 101 |
|
| 102 |
const myUnionSchema: JSONSchemaType<MyUnionData> = { |
| 103 |
anyOf: [ |
| 104 |
{ |
| 105 |
type: "object", |
| 106 |
properties: { |
| 107 |
a: {type: "boolean"}, |
| 108 |
}, |
| 109 |
required: ["a"], |
| 110 |
}, |
| 111 |
{ |
| 112 |
type: ["string", "number"], |
| 113 |
// can specify properties for either type |
| 114 |
minimum: 0, |
| 115 |
minLength: 1, |
| 116 |
}, |
| 117 |
], |
| 118 |
} as const |
| 119 |
|
| 120 |
// because of the current definition, you can do this nested recusion |
| 121 |
const myNestedUnionSchema: JSONSchemaType<MyUnionData> = { |
| 122 |
anyOf: [ |
| 123 |
{ |
| 124 |
oneOf: [ |
| 125 |
{ |
| 126 |
type: "object", |
| 127 |
properties: { |
| 128 |
a: {type: "boolean"}, |
| 129 |
}, |
| 130 |
required: ["a"], |
| 131 |
}, |
| 132 |
{ |
| 133 |
type: "string", |
| 134 |
}, |
| 135 |
], |
| 136 |
}, |
| 137 |
{ |
| 138 |
type: "number", |
| 139 |
}, |
| 140 |
], |
| 141 |
} as const |
| 142 |
|
| 143 |
// allowing union types necessarily allows this to pass :/ |
| 144 |
const emptyType: JSONSchemaType<MyData> = { |
| 145 |
type: [], |
| 146 |
} as const |
| 147 |
|
| 148 |
type MyEnumRecord = Record<"a" | "b" | "c" | "d", number | undefined> |
| 149 |
|
| 150 |
describe("JSONSchemaType type and validation as a type guard", () => { |
| 151 |
const ajv = new _Ajv({allowUnionTypes: true}) |
| 152 |
|
| 153 |
const validData: unknown = { |
| 154 |
foo: "foo", |
| 155 |
bar: 1, |
| 156 |
baz: { |
| 157 |
quux: "quux", |
| 158 |
abc: "abc", |
| 159 |
}, |
| 160 |
boo: true, |
| 161 |
arr: [{id: 1}, {id: 2}], |
| 162 |
tuple: [1, "abc"], |
| 163 |
map: { |
| 164 |
a: 1, |
| 165 |
b: 2, |
| 166 |
}, |
| 167 |
negativeIfBoo: -1, |
| 168 |
} |
| 169 |
|
| 170 |
describe("schema has type JSONSchemaType<MyData>", () => { |
| 171 |
it("should prove the type of validated data", () => { |
| 172 |
const validate = ajv.compile(mySchema) |
| 173 |
if (validate(validData)) { |
| 174 |
validData.foo.should.equal("foo") |
| 175 |
} |
| 176 |
should.not.exist(validate.errors) |
| 177 |
|
| 178 |
if (ajv.validate(mySchema, validData)) { |
| 179 |
validData.foo.should.equal("foo") |
| 180 |
} |
| 181 |
should.not.exist(ajv.errors) |
| 182 |
}) |
| 183 |
}) |
| 184 |
|
| 185 |
const validUnionData: unknown = { |
| 186 |
a: true, |
| 187 |
} |
| 188 |
|
| 189 |
describe("schema has type JSONSchemaType<MyUnionData>", () => { |
| 190 |
it("should prove the type of validated data", () => { |
| 191 |
const validate = ajv.compile(myUnionSchema) |
| 192 |
if (validate(validUnionData)) { |
| 193 |
if (typeof validUnionData === "string") { |
| 194 |
should.fail("not a string") |
| 195 |
} else if (typeof validUnionData === "number") { |
| 196 |
should.fail("not a number") |
| 197 |
} else { |
| 198 |
validUnionData.a.should.equal(true) |
| 199 |
} |
| 200 |
} else { |
| 201 |
should.fail("is valid") |
| 202 |
} |
| 203 |
should.not.exist(validate.errors) |
| 204 |
|
| 205 |
if (ajv.validate(myUnionSchema, validUnionData)) { |
| 206 |
if (typeof validUnionData === "string") { |
| 207 |
should.fail("not a string") |
| 208 |
} else if (typeof validUnionData === "number") { |
| 209 |
should.fail("not a number") |
| 210 |
} else { |
| 211 |
validUnionData.a.should.equal(true) |
| 212 |
} |
| 213 |
} else { |
| 214 |
should.fail("is valid") |
| 215 |
} |
| 216 |
should.not.exist(ajv.errors) |
| 217 |
}) |
| 218 |
|
| 219 |
it("should prove the type of validated nested data", () => { |
| 220 |
const validate = ajv.compile(myNestedUnionSchema) |
| 221 |
if (validate(validUnionData)) { |
| 222 |
if (typeof validUnionData === "string") { |
| 223 |
should.fail("not a string") |
| 224 |
} else if (typeof validUnionData === "number") { |
| 225 |
should.fail("not a number") |
| 226 |
} else { |
| 227 |
validUnionData.a.should.equal(true) |
| 228 |
} |
| 229 |
} else { |
| 230 |
should.fail("is valid") |
| 231 |
} |
| 232 |
should.not.exist(validate.errors) |
| 233 |
|
| 234 |
if (ajv.validate(myNestedUnionSchema, validUnionData)) { |
| 235 |
if (typeof validUnionData === "string") { |
| 236 |
should.fail("not a string") |
| 237 |
} else if (typeof validUnionData === "number") { |
| 238 |
should.fail("not a number") |
| 239 |
} else { |
| 240 |
validUnionData.a.should.equal(true) |
| 241 |
} |
| 242 |
} else { |
| 243 |
should.fail("is valid") |
| 244 |
} |
| 245 |
should.not.exist(ajv.errors) |
| 246 |
}) |
| 247 |
|
| 248 |
it("should fail for invalid unions", () => { |
| 249 |
// @ts-expect-error extra type |
| 250 |
const extraSchema: JSONSchemaType<string | number> = { |
| 251 |
type: ["string", "number", "boolean"], |
| 252 |
} as const |
| 253 |
|
| 254 |
// @ts-expect-error extra properties |
| 255 |
const extraProps: JSONSchemaType<string, boolean> = { |
| 256 |
type: ["string", "boolean"], |
| 257 |
maximum: 5, // number property |
| 258 |
} as const |
| 259 |
|
| 260 |
// eslint-disable-next-line no-void |
| 261 |
void [extraSchema, extraProps] |
| 262 |
}) |
| 263 |
}) |
| 264 |
|
| 265 |
describe("schema has type SchemaObject", () => { |
| 266 |
it("should prove the type of validated data", () => { |
| 267 |
const schema = mySchema as SchemaObject |
| 268 |
const validate = ajv.compile<MyData>(schema) |
| 269 |
if (validate(validData)) { |
| 270 |
validData.foo.should.equal("foo") |
| 271 |
} |
| 272 |
should.not.exist(validate.errors) |
| 273 |
|
| 274 |
if (ajv.validate<MyData>(schema, validData)) { |
| 275 |
validData.foo.should.equal("foo") |
| 276 |
} |
| 277 |
should.not.exist(ajv.errors) |
| 278 |
}) |
| 279 |
}) |
| 280 |
|
| 281 |
describe("schema should be simple for record types", () => { |
| 282 |
it("typechecks a valid type without required", () => { |
| 283 |
const myEnumRecordSchema: JSONSchemaType<MyEnumRecord> = { |
| 284 |
type: "object", |
| 285 |
propertyNames: {enum: ["a", "b", "c", "d"]}, |
| 286 |
additionalProperty: {type: "number"}, |
| 287 |
} |
| 288 |
// eslint-disable-next-line no-void |
| 289 |
void myEnumRecordSchema |
| 290 |
}) |
| 291 |
|
| 292 |
it("requires required for non-optional types", () => { |
| 293 |
// @ts-expect-error missing required |
| 294 |
const requiredSchema: JSONSchemaType<{a: number}> = { |
| 295 |
type: "object", |
| 296 |
} |
| 297 |
// eslint-disable-next-line no-void |
| 298 |
void requiredSchema |
| 299 |
}) |
| 300 |
|
| 301 |
it("doesn't require required for optional types", () => { |
| 302 |
const optionalSchema: JSONSchemaType<{a?: number}> = { |
| 303 |
type: "object", |
| 304 |
} |
| 305 |
|
| 306 |
// eslint-disable-next-line no-void |
| 307 |
void optionalSchema |
| 308 |
}) |
| 309 |
|
| 310 |
it("won't accept nullable for non-null types", () => { |
| 311 |
// @ts-expect-error can't set nullable |
| 312 |
const nonNullSchema: JSONSchemaType<{a: number}> = { |
| 313 |
type: "object", |
| 314 |
properties: { |
| 315 |
a: { |
| 316 |
type: "number", |
| 317 |
nullable: true, |
| 318 |
}, |
| 319 |
}, |
| 320 |
required: [], |
| 321 |
} |
| 322 |
|
| 323 |
// eslint-disable-next-line no-void |
| 324 |
void nonNullSchema |
| 325 |
}) |
| 326 |
}) |
| 327 |
|
| 328 |
describe("schema works for primitives", () => { |
| 329 |
it("allows partial boolean sub schemas", () => { |
| 330 |
// this schema doesn't have much meaning, but it wasn't allowed before |
| 331 |
const trueSchema: JSONSchemaType<true> = { |
| 332 |
type: "boolean", |
| 333 |
not: {const: false}, |
| 334 |
} as const |
| 335 |
|
| 336 |
// eslint-disable-next-line no-void |
| 337 |
void trueSchema |
| 338 |
}) |
| 339 |
|
| 340 |
it("validates simple null", () => { |
| 341 |
const nullSchema: JSONSchemaType<null> = { |
| 342 |
type: "null", |
| 343 |
nullable: true, |
| 344 |
const: null, |
| 345 |
enum: [null], |
| 346 |
} as const |
| 347 |
const validate = ajv.compile(nullSchema) |
| 348 |
// eslint-disable-next-line @typescript-eslint/no-unused-expressions |
| 349 |
validate(null).should.be.true |
| 350 |
}) |
| 351 |
}) |
| 352 |
}) |
| 353 |
|
| 354 |
// eslint-disable-next-line no-void |
| 355 |
void emptyType |