| 1 |
import type Ajv from ".." |
| 2 |
import type {KeywordCxt, SchemaObject} from ".." |
| 3 |
import _Ajv from "./ajv" |
| 4 |
import {_} from "../dist/compile/codegen/code" |
| 5 |
import * as assert from "assert" |
| 6 |
import chai from "./chai" |
| 7 |
const should = chai.should() |
| 8 |
|
| 9 |
describe("Ajv", () => { |
| 10 |
let ajv: Ajv |
| 11 |
|
| 12 |
beforeEach(() => { |
| 13 |
ajv = new _Ajv({keywords: ["foo"], allowUnionTypes: true}) |
| 14 |
}) |
| 15 |
|
| 16 |
it("should create instance", () => { |
| 17 |
// @ts-ignore |
| 18 |
ajv.should.be.instanceof(_Ajv) |
| 19 |
}) |
| 20 |
|
| 21 |
describe("compile method", () => { |
| 22 |
it("should compile schema and return validating function", () => { |
| 23 |
const validate = ajv.compile({type: "integer"}) |
| 24 |
// @ts-ignore |
| 25 |
validate.should.be.a("function") |
| 26 |
// @ts-ignore |
| 27 |
validate(1).should.equal(true) |
| 28 |
// @ts-ignore |
| 29 |
validate(1.1).should.equal(false) |
| 30 |
// @ts-ignore |
| 31 |
validate("1").should.equal(false) |
| 32 |
}) |
| 33 |
|
| 34 |
it("should cache compiled functions for the same schema", () => { |
| 35 |
const schema = { |
| 36 |
$id: "//e.com/int.json", |
| 37 |
type: "integer", |
| 38 |
minimum: 1, |
| 39 |
} |
| 40 |
const v1 = ajv.compile(schema) |
| 41 |
const v2 = ajv.compile(schema) |
| 42 |
// @ts-ignore |
| 43 |
v1.should.equal(v2) |
| 44 |
}) |
| 45 |
|
| 46 |
it("should throw if different schema has the same id", () => { |
| 47 |
ajv.compile({$id: "//e.com/int.json", type: "integer"}) |
| 48 |
// @ts-ignore |
| 49 |
should.throw(() => { |
| 50 |
ajv.compile({$id: "//e.com/int.json", type: "integer", minimum: 1}) |
| 51 |
}, /already exists/) |
| 52 |
}) |
| 53 |
|
| 54 |
it("should throw if invalid schema is compiled", () => { |
| 55 |
// @ts-ignore |
| 56 |
should.throw(() => { |
| 57 |
ajv.compile({type: null}) |
| 58 |
}, /must be equal to one of the allowed values/) |
| 59 |
}) |
| 60 |
|
| 61 |
it("should throw if compiled schema has an invalid JavaScript code", () => { |
| 62 |
const _ajv = new _Ajv({logger: false}) |
| 63 |
_ajv.addKeyword({keyword: "even", code: badEvenCode}) |
| 64 |
let schema = {even: true} |
| 65 |
const validate: any = _ajv.compile(schema) |
| 66 |
// @ts-ignore |
| 67 |
validate(2).should.equal(true) |
| 68 |
// @ts-ignore |
| 69 |
validate(3).should.equal(false) |
| 70 |
|
| 71 |
schema = {even: false} |
| 72 |
// @ts-ignore |
| 73 |
should.throw(() => { |
| 74 |
_ajv.compile(schema) |
| 75 |
}, /Unexpected token/) |
| 76 |
|
| 77 |
function badEvenCode(cxt: KeywordCxt) { |
| 78 |
const op = cxt.schema ? _`===` : _`!===` // invalid on purpose |
| 79 |
cxt.pass(_`${cxt.data} % 2 ${op} 0`) |
| 80 |
} |
| 81 |
}) |
| 82 |
}) |
| 83 |
|
| 84 |
describe("validate method", () => { |
| 85 |
it("should compile schema and validate data against it", () => { |
| 86 |
// @ts-ignore |
| 87 |
ajv.validate({type: "integer"}, 1).should.equal(true) |
| 88 |
// @ts-ignore |
| 89 |
ajv.validate({type: "integer"}, "1").should.equal(false) |
| 90 |
// @ts-ignore |
| 91 |
ajv.validate({type: "string"}, "a").should.equal(true) |
| 92 |
// @ts-ignore |
| 93 |
ajv.validate({type: "string"}, 1).should.equal(false) |
| 94 |
}) |
| 95 |
|
| 96 |
it("should validate against previously compiled schema by id (also see addSchema)", () => { |
| 97 |
// @ts-ignore |
| 98 |
ajv.validate({$id: "//e.com/int.json", type: "integer"}, 1).should.equal(true) |
| 99 |
// @ts-ignore |
| 100 |
ajv.validate("//e.com/int.json", 1).should.equal(true) |
| 101 |
// @ts-ignore |
| 102 |
ajv.validate("//e.com/int.json", "1").should.equal(false) |
| 103 |
|
| 104 |
// @ts-ignore |
| 105 |
ajv.compile({$id: "//e.com/str.json", type: "string"}).should.be.a("function") |
| 106 |
// @ts-ignore |
| 107 |
ajv.validate("//e.com/str.json", "a").should.equal(true) |
| 108 |
// @ts-ignore |
| 109 |
ajv.validate("//e.com/str.json", 1).should.equal(false) |
| 110 |
}) |
| 111 |
|
| 112 |
it("should throw exception if no schema with ref", () => { |
| 113 |
// @ts-ignore |
| 114 |
ajv.validate({$id: "integer", type: "integer"}, 1).should.equal(true) |
| 115 |
// @ts-ignore |
| 116 |
ajv.validate("integer", 1).should.equal(true) |
| 117 |
// @ts-ignore |
| 118 |
should.throw(() => { |
| 119 |
ajv.validate("string", "foo") |
| 120 |
}, /no schema with key or ref/) |
| 121 |
}) |
| 122 |
|
| 123 |
it("should validate schema fragment by ref", () => { |
| 124 |
ajv.addSchema({ |
| 125 |
$id: "http://e.com/types.json", |
| 126 |
definitions: { |
| 127 |
int: {type: "integer"}, |
| 128 |
str: {type: "string"}, |
| 129 |
}, |
| 130 |
}) |
| 131 |
|
| 132 |
// @ts-ignore |
| 133 |
ajv.validate("http://e.com/types.json#/definitions/int", 1).should.equal(true) |
| 134 |
// @ts-ignore |
| 135 |
ajv.validate("http://e.com/types.json#/definitions/int", "1").should.equal(false) |
| 136 |
}) |
| 137 |
|
| 138 |
it("should return schema fragment by id", () => { |
| 139 |
ajv.addSchema({ |
| 140 |
$id: "http://e.com/types.json", |
| 141 |
definitions: { |
| 142 |
int: {$id: "#int", type: "integer"}, |
| 143 |
str: {$id: "#str", type: "string"}, |
| 144 |
}, |
| 145 |
}) |
| 146 |
|
| 147 |
// @ts-ignore |
| 148 |
ajv.validate("http://e.com/types.json#int", 1).should.equal(true) |
| 149 |
// @ts-ignore |
| 150 |
ajv.validate("http://e.com/types.json#int", "1").should.equal(false) |
| 151 |
}) |
| 152 |
}) |
| 153 |
|
| 154 |
describe("addSchema method", () => { |
| 155 |
it("should add and compile schema with key", () => { |
| 156 |
ajv.addSchema({type: "integer"}, "int") |
| 157 |
const validate = ajv.getSchema("int") |
| 158 |
assert(typeof validate == "function") |
| 159 |
|
| 160 |
// @ts-ignore |
| 161 |
validate(1).should.equal(true) |
| 162 |
// @ts-ignore |
| 163 |
validate(1.1).should.equal(false) |
| 164 |
// @ts-ignore |
| 165 |
validate("1").should.equal(false) |
| 166 |
// @ts-ignore |
| 167 |
ajv.validate("int", 1).should.equal(true) |
| 168 |
// @ts-ignore |
| 169 |
ajv.validate("int", "1").should.equal(false) |
| 170 |
}) |
| 171 |
|
| 172 |
it("should add and compile schema without key", () => { |
| 173 |
ajv.addSchema({type: "integer"}) |
| 174 |
// @ts-ignore |
| 175 |
ajv.validate("", 1).should.equal(true) |
| 176 |
// @ts-ignore |
| 177 |
ajv.validate("", "1").should.equal(false) |
| 178 |
}) |
| 179 |
|
| 180 |
it("should add and compile schema with id", () => { |
| 181 |
ajv.addSchema({$id: "//e.com/int.json", type: "integer"}) |
| 182 |
// @ts-ignore |
| 183 |
ajv.validate("//e.com/int.json", 1).should.equal(true) |
| 184 |
// @ts-ignore |
| 185 |
ajv.validate("//e.com/int.json", "1").should.equal(false) |
| 186 |
}) |
| 187 |
|
| 188 |
it("should normalize schema keys and ids", () => { |
| 189 |
ajv.addSchema({$id: "//e.com/int.json#", type: "integer"}, "int#") |
| 190 |
// @ts-ignore |
| 191 |
ajv.validate("int", 1).should.equal(true) |
| 192 |
// @ts-ignore |
| 193 |
ajv.validate("int", "1").should.equal(false) |
| 194 |
// @ts-ignore |
| 195 |
ajv.validate("//e.com/int.json", 1).should.equal(true) |
| 196 |
// @ts-ignore |
| 197 |
ajv.validate("//e.com/int.json", "1").should.equal(false) |
| 198 |
// @ts-ignore |
| 199 |
ajv.validate("int#/", 1).should.equal(true) |
| 200 |
// @ts-ignore |
| 201 |
ajv.validate("int#/", "1").should.equal(false) |
| 202 |
// @ts-ignore |
| 203 |
ajv.validate("//e.com/int.json#/", 1).should.equal(true) |
| 204 |
// @ts-ignore |
| 205 |
ajv.validate("//e.com/int.json#/", "1").should.equal(false) |
| 206 |
}) |
| 207 |
|
| 208 |
it("should add and compile array of schemas with ids", () => { |
| 209 |
ajv.addSchema([ |
| 210 |
{$id: "//e.com/int.json", type: "integer"}, |
| 211 |
{$id: "//e.com/str.json", type: "string"}, |
| 212 |
]) |
| 213 |
|
| 214 |
const validate0 = ajv.getSchema("//e.com/int.json") |
| 215 |
const validate1 = ajv.getSchema("//e.com/str.json") |
| 216 |
assert(typeof validate0 == "function") |
| 217 |
assert(typeof validate1 == "function") |
| 218 |
|
| 219 |
// @ts-ignore |
| 220 |
validate0(1).should.equal(true) |
| 221 |
// @ts-ignore |
| 222 |
validate0("1").should.equal(false) |
| 223 |
// @ts-ignore |
| 224 |
validate1("a").should.equal(true) |
| 225 |
// @ts-ignore |
| 226 |
validate1(1).should.equal(false) |
| 227 |
|
| 228 |
// @ts-ignore |
| 229 |
ajv.validate("//e.com/int.json", 1).should.equal(true) |
| 230 |
// @ts-ignore |
| 231 |
ajv.validate("//e.com/int.json", "1").should.equal(false) |
| 232 |
// @ts-ignore |
| 233 |
ajv.validate("//e.com/str.json", "a").should.equal(true) |
| 234 |
// @ts-ignore |
| 235 |
ajv.validate("//e.com/str.json", 1).should.equal(false) |
| 236 |
}) |
| 237 |
|
| 238 |
it("should throw on duplicate key", () => { |
| 239 |
ajv.addSchema({type: "integer"}, "int") |
| 240 |
// @ts-ignore |
| 241 |
should.throw(() => { |
| 242 |
ajv.addSchema({type: "integer", minimum: 1}, "int") |
| 243 |
}, /already exists/) |
| 244 |
}) |
| 245 |
|
| 246 |
it("should throw on duplicate normalized key", () => { |
| 247 |
ajv.addSchema({type: "number"}, "num") |
| 248 |
// @ts-ignore |
| 249 |
should.throw(() => { |
| 250 |
ajv.addSchema({type: "integer"}, "num#") |
| 251 |
}, /already exists/) |
| 252 |
// @ts-ignore |
| 253 |
should.throw(() => { |
| 254 |
ajv.addSchema({type: "integer"}, "num#/") |
| 255 |
}, /already exists/) |
| 256 |
}) |
| 257 |
|
| 258 |
it("should allow only one schema without key and id", () => { |
| 259 |
ajv.addSchema({type: "number"}) |
| 260 |
// @ts-ignore |
| 261 |
should.throw(() => { |
| 262 |
ajv.addSchema({type: "integer"}) |
| 263 |
}, /already exists/) |
| 264 |
// @ts-ignore |
| 265 |
should.throw(() => { |
| 266 |
ajv.addSchema({type: "integer"}, "") |
| 267 |
}, /already exists/) |
| 268 |
// @ts-ignore |
| 269 |
should.throw(() => { |
| 270 |
ajv.addSchema({type: "integer"}, "#") |
| 271 |
}, /already exists/) |
| 272 |
}) |
| 273 |
|
| 274 |
it("should throw if schema is not an object", () => { |
| 275 |
// @ts-ignore |
| 276 |
should.throw(() => { |
| 277 |
// @ts-ignore |
| 278 |
ajv.addSchema("foo") |
| 279 |
}, /schema must be object or boolean/) |
| 280 |
}) |
| 281 |
|
| 282 |
it("should throw if schema id is not a string", () => { |
| 283 |
try { |
| 284 |
// @ts-ignore |
| 285 |
ajv.addSchema({$id: 1, type: "integer"}) |
| 286 |
throw new Error("should have throw exception") |
| 287 |
} catch (e) { |
| 288 |
// @ts-ignore |
| 289 |
;(e as Error).message.should.equal("schema $id must be string") |
| 290 |
} |
| 291 |
}) |
| 292 |
|
| 293 |
it("should return instance of itself", () => { |
| 294 |
const res = ajv.addSchema({type: "integer"}, "int") |
| 295 |
// @ts-ignore |
| 296 |
res.should.equal(ajv) |
| 297 |
}) |
| 298 |
}) |
| 299 |
|
| 300 |
describe("getSchema method", () => { |
| 301 |
it("should return compiled schema by key", () => { |
| 302 |
ajv.addSchema({type: "integer"}, "int") |
| 303 |
const validate = ajv.getSchema("int") |
| 304 |
assert(typeof validate == "function") |
| 305 |
// @ts-ignore |
| 306 |
validate(1).should.equal(true) |
| 307 |
// @ts-ignore |
| 308 |
validate("1").should.equal(false) |
| 309 |
}) |
| 310 |
|
| 311 |
it("should return compiled schema by id or ref", () => { |
| 312 |
ajv.addSchema({$id: "//e.com/int.json", type: "integer"}) |
| 313 |
const validate = ajv.getSchema("//e.com/int.json") |
| 314 |
assert(typeof validate == "function") |
| 315 |
// @ts-ignore |
| 316 |
validate(1).should.equal(true) |
| 317 |
// @ts-ignore |
| 318 |
validate("1").should.equal(false) |
| 319 |
}) |
| 320 |
|
| 321 |
it("should return compiled schema without key or with empty key", () => { |
| 322 |
ajv.addSchema({type: "integer"}) |
| 323 |
const validate = ajv.getSchema("") |
| 324 |
assert(typeof validate == "function") |
| 325 |
// @ts-ignore |
| 326 |
validate(1).should.equal(true) |
| 327 |
// @ts-ignore |
| 328 |
validate("1").should.equal(false) |
| 329 |
}) |
| 330 |
|
| 331 |
it("should return schema fragment by ref", () => { |
| 332 |
ajv.addSchema({ |
| 333 |
$id: "http://e.com/types.json", |
| 334 |
definitions: { |
| 335 |
int: {type: "integer"}, |
| 336 |
str: {type: "string"}, |
| 337 |
}, |
| 338 |
}) |
| 339 |
|
| 340 |
const vInt = ajv.getSchema("http://e.com/types.json#/definitions/int") |
| 341 |
assert(typeof vInt == "function") |
| 342 |
// @ts-ignore |
| 343 |
vInt(1).should.equal(true) |
| 344 |
// @ts-ignore |
| 345 |
vInt("1").should.equal(false) |
| 346 |
}) |
| 347 |
|
| 348 |
it("should return schema fragment by ref with protocol-relative URIs", () => { |
| 349 |
ajv.addSchema({ |
| 350 |
$id: "//e.com/types.json", |
| 351 |
definitions: { |
| 352 |
int: {type: "integer"}, |
| 353 |
str: {type: "string"}, |
| 354 |
}, |
| 355 |
}) |
| 356 |
|
| 357 |
const vInt = ajv.getSchema("//e.com/types.json#/definitions/int") |
| 358 |
assert(typeof vInt == "function") |
| 359 |
// @ts-ignore |
| 360 |
vInt(1).should.equal(true) |
| 361 |
// @ts-ignore |
| 362 |
vInt("1").should.equal(false) |
| 363 |
}) |
| 364 |
|
| 365 |
it("should return schema fragment by id", () => { |
| 366 |
ajv.addSchema({ |
| 367 |
$id: "http://e.com/types.json", |
| 368 |
definitions: { |
| 369 |
int: {$id: "#int", type: "integer"}, |
| 370 |
str: {$id: "#str", type: "string"}, |
| 371 |
}, |
| 372 |
}) |
| 373 |
|
| 374 |
const vInt = ajv.getSchema("http://e.com/types.json#int") |
| 375 |
assert(typeof vInt == "function") |
| 376 |
// @ts-ignore |
| 377 |
vInt(1).should.equal(true) |
| 378 |
// @ts-ignore |
| 379 |
vInt("1").should.equal(false) |
| 380 |
}) |
| 381 |
}) |
| 382 |
|
| 383 |
describe("removeSchema method", () => { |
| 384 |
it("should remove schema by key", () => { |
| 385 |
const schema = {type: "integer"} |
| 386 |
ajv.addSchema(schema, "int") |
| 387 |
const v = ajv.getSchema("int") |
| 388 |
assert(typeof v == "function") |
| 389 |
// @ts-ignore |
| 390 |
v.should.be.a("function") |
| 391 |
// @ts-ignore |
| 392 |
ajv._cache.get(schema).validate.should.equal(v) |
| 393 |
|
| 394 |
ajv.removeSchema("int") |
| 395 |
// @ts-ignore |
| 396 |
should.not.exist(ajv.getSchema("int")) |
| 397 |
// @ts-ignore |
| 398 |
should.not.exist(ajv._cache.get(schema)) |
| 399 |
}) |
| 400 |
|
| 401 |
it("should remove schema by id", () => { |
| 402 |
const schema = {$id: "//e.com/int.json", type: "integer"} |
| 403 |
ajv.addSchema(schema) |
| 404 |
|
| 405 |
const v = ajv.getSchema("//e.com/int.json") |
| 406 |
assert(typeof v == "function") |
| 407 |
// @ts-ignore |
| 408 |
v.should.be.a("function") |
| 409 |
// @ts-ignore |
| 410 |
ajv._cache.get(schema).validate.should.equal(v) |
| 411 |
|
| 412 |
ajv.removeSchema("//e.com/int.json") |
| 413 |
// @ts-ignore |
| 414 |
should.not.exist(ajv.getSchema("//e.com/int.json")) |
| 415 |
// @ts-ignore |
| 416 |
should.not.exist(ajv._cache.get(schema)) |
| 417 |
}) |
| 418 |
|
| 419 |
it("should remove schema by schema object", () => { |
| 420 |
const schema = {type: "integer"} |
| 421 |
ajv.addSchema(schema) |
| 422 |
// @ts-ignore |
| 423 |
ajv._cache.get(schema).should.be.an("object") |
| 424 |
ajv.removeSchema(schema) |
| 425 |
// @ts-ignore |
| 426 |
should.not.exist(ajv._cache.get(schema)) |
| 427 |
}) |
| 428 |
|
| 429 |
it("should remove schema with id by schema object", () => { |
| 430 |
const schema = {$id: "//e.com/int.json", type: "integer"} |
| 431 |
ajv.addSchema(schema) |
| 432 |
// @ts-ignore |
| 433 |
ajv._cache.get(schema).should.be.an("object") |
| 434 |
ajv.removeSchema(schema) |
| 435 |
// @ts-ignore |
| 436 |
should.not.exist(ajv.getSchema("//e.com/int.json")) |
| 437 |
// @ts-ignore |
| 438 |
should.not.exist(ajv._cache.get(schema)) |
| 439 |
}) |
| 440 |
|
| 441 |
it("should not throw if there is no schema with passed id", () => { |
| 442 |
// @ts-ignore |
| 443 |
should.not.exist(ajv.getSchema("//e.com/int.json")) |
| 444 |
// @ts-ignore |
| 445 |
should.not.throw(() => { |
| 446 |
ajv.removeSchema("//e.com/int.json") |
| 447 |
}) |
| 448 |
}) |
| 449 |
|
| 450 |
it("should remove all schemas but meta-schemas if called without an arguments", () => { |
| 451 |
const schema1 = {$id: "//e.com/int.json", type: "integer"} |
| 452 |
ajv.addSchema(schema1) |
| 453 |
// @ts-ignore |
| 454 |
ajv._cache.get(schema1).should.be.an("object") |
| 455 |
|
| 456 |
const schema2 = {type: "integer"} |
| 457 |
ajv.addSchema(schema2) |
| 458 |
// @ts-ignore |
| 459 |
ajv._cache.get(schema2).should.be.an("object") |
| 460 |
|
| 461 |
ajv.removeSchema() |
| 462 |
// @ts-ignore |
| 463 |
should.not.exist(ajv._cache.get(schema1)) |
| 464 |
// @ts-ignore |
| 465 |
should.not.exist(ajv._cache.get(schema2)) |
| 466 |
}) |
| 467 |
|
| 468 |
it("should remove all schemas but meta-schemas with key/id matching pattern", () => { |
| 469 |
const schema1 = {$id: "//e.com/int.json", type: "integer"} |
| 470 |
ajv.addSchema(schema1) |
| 471 |
// @ts-ignore |
| 472 |
ajv._cache.get(schema1).should.be.an("object") |
| 473 |
|
| 474 |
const schema2 = {$id: "str.json", type: "string"} |
| 475 |
ajv.addSchema(schema2, "//e.com/str.json") |
| 476 |
// @ts-ignore |
| 477 |
ajv._cache.get(schema2).should.be.an("object") |
| 478 |
|
| 479 |
const schema3 = {type: "integer"} |
| 480 |
ajv.addSchema(schema3) |
| 481 |
// @ts-ignore |
| 482 |
ajv._cache.get(schema3).should.be.an("object") |
| 483 |
|
| 484 |
ajv.removeSchema(/e\.com/) |
| 485 |
// @ts-ignore |
| 486 |
should.not.exist(ajv._cache.get(schema1)) |
| 487 |
// @ts-ignore |
| 488 |
should.not.exist(ajv._cache.get(schema2)) |
| 489 |
// @ts-ignore |
| 490 |
ajv._cache.get(schema3).should.be.an("object") |
| 491 |
}) |
| 492 |
|
| 493 |
it("should return instance of itself", () => { |
| 494 |
const res = ajv.addSchema({type: "integer"}, "int").removeSchema("int") |
| 495 |
// @ts-ignore |
| 496 |
res.should.equal(ajv) |
| 497 |
}) |
| 498 |
}) |
| 499 |
|
| 500 |
describe("addFormat method", () => { |
| 501 |
it("should add format as regular expression", () => { |
| 502 |
ajv.addFormat("identifier", /^[a-z_$][a-z0-9_$]*$/i) |
| 503 |
testFormat() |
| 504 |
}) |
| 505 |
|
| 506 |
it("should add format as string", () => { |
| 507 |
ajv.addFormat("identifier", "^[A-Za-z_$][A-Za-z0-9_$]*$") |
| 508 |
testFormat() |
| 509 |
}) |
| 510 |
|
| 511 |
it("should add format as function", () => { |
| 512 |
ajv.addFormat("identifier", (str) => /^[a-z_$][a-z0-9_$]*$/i.test(str)) |
| 513 |
testFormat() |
| 514 |
}) |
| 515 |
|
| 516 |
it("should add format as object", () => { |
| 517 |
ajv.addFormat("identifier", { |
| 518 |
validate: (str: string) => /^[a-z_$][a-z0-9_$]*$/i.test(str), |
| 519 |
}) |
| 520 |
testFormat() |
| 521 |
}) |
| 522 |
|
| 523 |
it("should return instance of itself", () => { |
| 524 |
const res = ajv.addFormat("identifier", /^[a-z_$][a-z0-9_$]*$/i) |
| 525 |
// @ts-ignore |
| 526 |
res.should.equal(ajv) |
| 527 |
}) |
| 528 |
|
| 529 |
function testFormat() { |
| 530 |
const validate = ajv.compile({ |
| 531 |
type: ["number", "string"], |
| 532 |
format: "identifier", |
| 533 |
}) |
| 534 |
// @ts-ignore |
| 535 |
validate("Abc1").should.equal(true) |
| 536 |
// @ts-ignore |
| 537 |
validate("123").should.equal(false) |
| 538 |
// @ts-ignore |
| 539 |
validate(123).should.equal(true) |
| 540 |
} |
| 541 |
|
| 542 |
describe("formats for number", () => { |
| 543 |
it("should validate only numbers", () => { |
| 544 |
ajv.addFormat("positive", { |
| 545 |
type: "number", |
| 546 |
validate: function (x: number) { |
| 547 |
return x > 0 |
| 548 |
}, |
| 549 |
}) |
| 550 |
|
| 551 |
const validate = ajv.compile({ |
| 552 |
type: ["string", "number"], |
| 553 |
format: "positive", |
| 554 |
}) |
| 555 |
// @ts-ignore |
| 556 |
validate(-2).should.equal(false) |
| 557 |
// @ts-ignore |
| 558 |
validate(0).should.equal(false) |
| 559 |
// @ts-ignore |
| 560 |
validate(2).should.equal(true) |
| 561 |
// @ts-ignore |
| 562 |
validate("abc").should.equal(true) |
| 563 |
}) |
| 564 |
|
| 565 |
it("should validate numbers with format via $data", () => { |
| 566 |
ajv = new _Ajv({$data: true, allowUnionTypes: true}) |
| 567 |
ajv.addFormat("positive", { |
| 568 |
type: "number", |
| 569 |
validate: function (x: number) { |
| 570 |
return x > 0 |
| 571 |
}, |
| 572 |
}) |
| 573 |
|
| 574 |
const validate = ajv.compile({ |
| 575 |
type: "object", |
| 576 |
properties: { |
| 577 |
data: { |
| 578 |
type: ["number", "string"], |
| 579 |
format: {$data: "1/frmt"}, |
| 580 |
}, |
| 581 |
frmt: {type: "string"}, |
| 582 |
}, |
| 583 |
}) |
| 584 |
// @ts-ignore |
| 585 |
validate({data: -2, frmt: "positive"}).should.equal(false) |
| 586 |
// @ts-ignore |
| 587 |
validate({data: 0, frmt: "positive"}).should.equal(false) |
| 588 |
// @ts-ignore |
| 589 |
validate({data: 2, frmt: "positive"}).should.equal(true) |
| 590 |
// @ts-ignore |
| 591 |
validate({data: "abc", frmt: "positive"}).should.equal(true) |
| 592 |
}) |
| 593 |
}) |
| 594 |
}) |
| 595 |
|
| 596 |
describe("validateSchema method", () => { |
| 597 |
it("should validate schema against meta-schema", () => { |
| 598 |
let valid = ajv.validateSchema({ |
| 599 |
$schema: "http://json-schema.org/draft-07/schema#", |
| 600 |
type: "number", |
| 601 |
}) |
| 602 |
|
| 603 |
// @ts-ignore |
| 604 |
valid.should.equal(true) |
| 605 |
// @ts-ignore |
| 606 |
should.equal(ajv.errors, null) |
| 607 |
|
| 608 |
valid = ajv.validateSchema({ |
| 609 |
$schema: "http://json-schema.org/draft-07/schema#", |
| 610 |
type: "wrong_type", |
| 611 |
}) |
| 612 |
|
| 613 |
// @ts-ignore |
| 614 |
valid.should.equal(false) |
| 615 |
assert(Array.isArray(ajv.errors)) |
| 616 |
// @ts-ignore |
| 617 |
ajv.errors.length.should.equal(3) |
| 618 |
// @ts-ignore |
| 619 |
ajv.errors[0].keyword.should.equal("enum") |
| 620 |
// @ts-ignore |
| 621 |
ajv.errors[1].keyword.should.equal("type") |
| 622 |
// @ts-ignore |
| 623 |
ajv.errors[2].keyword.should.equal("anyOf") |
| 624 |
}) |
| 625 |
|
| 626 |
it("should throw exception if meta-schema is unknown", () => { |
| 627 |
// @ts-ignore |
| 628 |
should.throw(() => { |
| 629 |
ajv.validateSchema({ |
| 630 |
$schema: "http://example.com/unknown/schema#", |
| 631 |
type: "number", |
| 632 |
}) |
| 633 |
}, /no schema with key or ref/) |
| 634 |
}) |
| 635 |
|
| 636 |
it("should throw exception if $schema is not a string", () => { |
| 637 |
// @ts-ignore |
| 638 |
should.throw(() => { |
| 639 |
ajv.validateSchema({ |
| 640 |
// @ts-ignore |
| 641 |
$schema: {}, |
| 642 |
type: "number", |
| 643 |
}) |
| 644 |
}, /\$schema must be a string/) |
| 645 |
}) |
| 646 |
|
| 647 |
describe("sub-schema validation outside of definitions during compilation", () => { |
| 648 |
it("maximum", () => { |
| 649 |
passValidationThrowCompile({ |
| 650 |
$ref: "#/foo", |
| 651 |
foo: {type: "number", maximum: "bar"}, |
| 652 |
}) |
| 653 |
}) |
| 654 |
|
| 655 |
it("exclusiveMaximum", () => { |
| 656 |
passValidationThrowCompile({ |
| 657 |
$ref: "#/foo", |
| 658 |
foo: {type: "number", exclusiveMaximum: "bar"}, |
| 659 |
}) |
| 660 |
}) |
| 661 |
|
| 662 |
it("maxItems", () => { |
| 663 |
passValidationThrowCompile({ |
| 664 |
$ref: "#/foo", |
| 665 |
foo: {type: "array", maxItems: "bar"}, |
| 666 |
}) |
| 667 |
}) |
| 668 |
|
| 669 |
it("maxLength", () => { |
| 670 |
passValidationThrowCompile({ |
| 671 |
$ref: "#/foo", |
| 672 |
foo: {type: "string", maxLength: "bar"}, |
| 673 |
}) |
| 674 |
}) |
| 675 |
|
| 676 |
it("maxProperties", () => { |
| 677 |
passValidationThrowCompile({ |
| 678 |
$ref: "#/foo", |
| 679 |
foo: {type: "object", maxProperties: "bar"}, |
| 680 |
}) |
| 681 |
}) |
| 682 |
|
| 683 |
it("multipleOf", () => { |
| 684 |
passValidationThrowCompile({ |
| 685 |
$ref: "#/foo", |
| 686 |
foo: {type: "number", multipleOf: "bar"}, |
| 687 |
}) |
| 688 |
}) |
| 689 |
|
| 690 |
function passValidationThrowCompile(schema: SchemaObject) { |
| 691 |
// @ts-ignore |
| 692 |
ajv.validateSchema(schema).should.equal(true) |
| 693 |
// @ts-ignore |
| 694 |
should.throw(() => { |
| 695 |
ajv.compile(schema) |
| 696 |
}, /value must be/) |
| 697 |
} |
| 698 |
}) |
| 699 |
}) |
| 700 |
}) |