|
2 | 2 |
|
3 | 3 | const util = require("../util");
|
4 | 4 | const { ono } = require("@jsdevtools/ono");
|
5 |
| -const ZSchema = require("z-schema"); |
| 5 | +const AjvDraft4 = require("ajv-draft-04"); |
| 6 | +const Ajv = require("ajv/dist/2020"); |
6 | 7 | const { openapi } = require("@apidevtools/openapi-schemas");
|
7 | 8 |
|
8 | 9 | module.exports = validateSchema;
|
9 | 10 |
|
10 |
| -let zSchema = initializeZSchema(); |
11 |
| - |
12 | 11 | /**
|
13 |
| - * Validates the given Swagger API against the Swagger 2.0 or 3.0 schema. |
| 12 | + * Validates the given Swagger API against the Swagger 2.0 or OpenAPI 3.0 and 3.1 schemas. |
14 | 13 | *
|
15 | 14 | * @param {SwaggerObject} api
|
16 | 15 | */
|
17 | 16 | function validateSchema (api) {
|
| 17 | + let ajv; |
| 18 | + |
18 | 19 | // Choose the appropriate schema (Swagger or OpenAPI)
|
19 |
| - let schema = api.swagger ? openapi.v2 : openapi.v3; |
| 20 | + let schema; |
20 | 21 |
|
21 |
| - // Validate against the schema |
22 |
| - let isValid = zSchema.validate(api, schema); |
| 22 | + if (api.swagger) { |
| 23 | + schema = openapi.v2; |
| 24 | + ajv = initializeAjv(); |
| 25 | + } |
| 26 | + else { |
| 27 | + if (api.openapi.startsWith("3.1")) { |
| 28 | + schema = openapi.v31; |
| 29 | + |
| 30 | + // There's a bug with Ajv in how it handles `$dynamicRef` in the way that it's used within the 3.1 schema so we |
| 31 | + // need to do some adhoc workarounds. |
| 32 | + // https://github.com/OAI/OpenAPI-Specification/issues/2689 |
| 33 | + // https://github.com/ajv-validator/ajv/issues/1573 |
| 34 | + const schemaDynamicRef = schema.$defs.schema; |
| 35 | + delete schemaDynamicRef.$dynamicAnchor; |
| 36 | + |
| 37 | + schema.$defs.components.properties.schemas.additionalProperties = schemaDynamicRef; |
| 38 | + schema.$defs.header.dependentSchemas.schema.properties.schema = schemaDynamicRef; |
| 39 | + schema.$defs["media-type"].properties.schema = schemaDynamicRef; |
| 40 | + schema.$defs.parameter.properties.schema = schemaDynamicRef; |
| 41 | + |
| 42 | + ajv = initializeAjv(false); |
| 43 | + } |
| 44 | + else { |
| 45 | + schema = openapi.v3; |
| 46 | + ajv = initializeAjv(); |
| 47 | + } |
| 48 | + } |
23 | 49 |
|
| 50 | + // Validate against the schema |
| 51 | + let isValid = ajv.validate(schema, api); |
24 | 52 | if (!isValid) {
|
25 |
| - let err = zSchema.getLastError(); |
26 |
| - let message = "Swagger schema validation failed. \n" + formatZSchemaError(err.details); |
27 |
| - throw ono.syntax(err, { details: err.details }, message); |
| 53 | + let err = ajv.errors; |
| 54 | + let message = "Swagger schema validation failed.\n" + formatAjvError(err); |
| 55 | + throw ono.syntax(err, { details: err }, message); |
28 | 56 | }
|
29 | 57 | }
|
30 | 58 |
|
31 | 59 | /**
|
32 |
| - * Performs one-time initialization logic to prepare for Swagger Schema validation. |
| 60 | + * Determines which version of Ajv to load and prepares it for use. |
| 61 | + * |
| 62 | + * @param {bool} draft04 |
| 63 | + * @returns {Ajv} |
33 | 64 | */
|
34 |
| -function initializeZSchema () { |
35 |
| - // HACK: Delete the OpenAPI schema IDs because ZSchema can't resolve them |
36 |
| - delete openapi.v2.id; |
37 |
| - delete openapi.v3.id; |
| 65 | +function initializeAjv (draft04 = true) { |
| 66 | + const opts = { |
| 67 | + allErrors: false, |
| 68 | + strict: false, |
| 69 | + validateFormats: false, |
| 70 | + }; |
38 | 71 |
|
39 |
| - // The OpenAPI 3.0 schema uses "uri-reference" formats. |
40 |
| - // Assume that any non-whitespace string is valid. |
41 |
| - ZSchema.registerFormat("uri-reference", (value) => value.trim().length > 0); |
| 72 | + if (draft04) { |
| 73 | + return new AjvDraft4(opts); |
| 74 | + } |
42 | 75 |
|
43 |
| - // Configure ZSchema |
44 |
| - return new ZSchema({ |
45 |
| - breakOnFirstError: true, |
46 |
| - noExtraKeywords: true, |
47 |
| - ignoreUnknownFormats: false, |
48 |
| - reportPathAsArray: true |
49 |
| - }); |
| 76 | + return new Ajv(opts); |
50 | 77 | }
|
51 | 78 |
|
52 | 79 | /**
|
53 |
| - * Z-Schema validation errors are a nested tree structure. |
54 |
| - * This function crawls that tree and builds an error message string. |
| 80 | + * Run through a set of Ajv errors and compile them into an error message string. |
55 | 81 | *
|
56 |
| - * @param {object[]} errors - The Z-Schema error details |
| 82 | + * @param {object[]} errors - The Ajv errors |
57 | 83 | * @param {string} [indent] - The whitespace used to indent the error message
|
58 | 84 | * @returns {string}
|
59 | 85 | */
|
60 |
| -function formatZSchemaError (errors, indent) { |
| 86 | +function formatAjvError (errors, indent) { |
61 | 87 | indent = indent || " ";
|
62 | 88 | let message = "";
|
63 | 89 | for (let error of errors) {
|
64 |
| - message += util.format(`${indent}${error.message} at #/${error.path.join("/")}\n`); |
65 |
| - if (error.inner) { |
66 |
| - message += formatZSchemaError(error.inner, indent + " "); |
67 |
| - } |
| 90 | + message += util.format(`${indent}#${error.instancePath.length ? error.instancePath : "/"} ${error.message}\n`); |
68 | 91 | }
|
69 | 92 | return message;
|
70 | 93 | }
|
0 commit comments