* Returns the correct and configured Ajv instance for a particular $schema version
(
/** @type {AjvFactoryOptions} */ {
draftVersion,
fullStrictMode = true,
unknownFormats = [],
unknownKeywords = [],
unknownSchemas = [],
options,
},
)
| 431 | * Returns the correct and configured Ajv instance for a particular $schema version |
| 432 | */ |
| 433 | async function ajvFactory( |
| 434 | /** @type {AjvFactoryOptions} */ { |
| 435 | draftVersion, |
| 436 | fullStrictMode = true, |
| 437 | unknownFormats = [], |
| 438 | unknownKeywords = [], |
| 439 | unknownSchemas = [], |
| 440 | options, |
| 441 | }, |
| 442 | ) { |
| 443 | let ajvOptions = {} |
| 444 | Object.assign( |
| 445 | ajvOptions, |
| 446 | fullStrictMode |
| 447 | ? { |
| 448 | strict: true, |
| 449 | } |
| 450 | : { |
| 451 | strictTypes: false, // recommended: true |
| 452 | strictTuples: false, // recommended: true |
| 453 | allowMatchingProperties: true, // recommended: false |
| 454 | }, |
| 455 | ) |
| 456 | Object.assign(ajvOptions, options) |
| 457 | |
| 458 | let ajv |
| 459 | switch (draftVersion) { |
| 460 | case 'draft-04': |
| 461 | ajv = new AjvDraft04(ajvOptions) |
| 462 | addFormats(ajv) |
| 463 | break |
| 464 | case 'draft-06': |
| 465 | ajv = new AjvDraft06And07(ajvOptions) |
| 466 | ajv.addMetaSchema(AjvDraft06SchemaJson) |
| 467 | addFormats(ajv) |
| 468 | break |
| 469 | case 'draft-07': |
| 470 | /** |
| 471 | * Note that draft-07 defines iri{,-reference}, idn-{hostname,email}, which |
| 472 | * are not available through `addFormats`. So, use `ajvFormatsDraft2019` to |
| 473 | * obtain these. Thus, some draft2019 formats like "duration" are applied. |
| 474 | * See https://ajv.js.org/packages/ajv-formats.html for details. |
| 475 | */ |
| 476 | ajv = new AjvDraft06And07(ajvOptions) |
| 477 | addFormats(ajv) |
| 478 | ajvFormatsDraft2019(ajv) |
| 479 | break |
| 480 | case '2019-09': |
| 481 | ajv = new Ajv2019(ajvOptions) |
| 482 | addFormats(ajv) |
| 483 | ajvFormatsDraft2019(ajv) |
| 484 | break |
| 485 | case '2020-12': |
| 486 | ajv = new Ajv2020(ajvOptions) |
| 487 | addFormats(ajv) |
| 488 | ajvFormatsDraft2019(ajv) |
| 489 | break |
| 490 | default: |
no test coverage detected