"use strict"; const util = require("../util"); const { default: ZSchema, registerFormat } = require("z-schema"); const { openapi } = require("@apidevtools/openapi-schemas"); module.exports = validateSchema; let zSchema = initializeZSchema(); /** * Validates the given Swagger API against the Swagger 2.0 or OpenAPI 3.0 and 3.1 schemas. * * @param {SwaggerObject} api */ function validateSchema (api) { // Swagger 2.0 specific validation: Check for unsupported oneOf/anyOf in user schemas // Swagger 2.0 only supports allOf, not oneOf or anyOf if (api.swagger) { const unsupportedKeywords = findUnsupportedKeywords(api, ['oneOf', 'anyOf']); if (unsupportedKeywords.length > 0) { const message = "Swagger schema validation failed.\n" + unsupportedKeywords.map(item => ` Swagger 2.0 does not support "${item.keyword}" (only "allOf" is supported) at #/${item.path.join('/')}` ).join('\n'); const error = new SyntaxError(message); error.details = unsupportedKeywords.map(item => ({ code: 'UNSUPPORTED_KEYWORD', message: `Swagger 2.0 does not support "${item.keyword}"`, path: item.path, params: [item.keyword] })); throw error; } } // Choose the appropriate schema (Swagger or OpenAPI) let schema; if(api.swagger){ schema = openapi.v2; }else{ if(api.openapi.startsWith('3.1')){ schema = openapi.v31; }else{ schema = openapi.v3; } } // Validate against the schema // z-schema 12 throws ValidateError on validation failure try { let isValid = zSchema.validate(api, schema); if (!isValid) { let err = zSchema.getLastError(); let message = "Swagger schema validation failed.\n" + formatZSchemaError(err.details); const error = new SyntaxError(message); error.details = err.details; throw error; } } catch (err) { // z-schema 12 throws ValidateError, convert it to SyntaxError if (err.name === 'ValidateError' || err.constructor.name === 'ValidateError') { let message = "Swagger schema validation failed.\n" + formatZSchemaError(err.details); const error = new SyntaxError(message); error.details = err.details; throw error; } // Re-throw if it's already a SyntaxError or other error throw err; } } /** * Recursively removes $schema properties and replaces external JSON Schema $refs * to prevent z-schema from trying to resolve external meta-schemas. * * External $refs are replaced with an empty schema {} which allows any value, * preserving the schema structure while avoiding unresolvable references. */ function removeSchemaReferences(obj) { if (typeof obj !== 'object' || obj === null) { return; } if (Array.isArray(obj)) { obj.forEach(item => removeSchemaReferences(item)); return; } // Remove $schema property delete obj.$schema; // Replace external JSON Schema $refs with permissive schema // This preserves the schema structure while avoiding unresolvable references if (obj.$ref && typeof obj.$ref === 'string' && (obj.$ref.startsWith('http://json-schema.org/') || obj.$ref.startsWith('https://json-schema.org/'))) { // Replace the $ref with an empty schema that allows anything delete obj.$ref; // If the object only had $ref, make it an empty schema if (Object.keys(obj).length === 0) { // Empty schema {} allows any value obj.description = obj.description || "External schema reference replaced"; } } for (const key in obj) { if (obj.hasOwnProperty(key)) { removeSchemaReferences(obj[key]); } } } /** * Recursively scans an object for unsupported keywords (like oneOf/anyOf in Swagger 2.0). * Returns an array of {keyword, path} objects for each occurrence. */ function findUnsupportedKeywords(obj, keywords, path = [], results = [], visited = new WeakSet()) { if (!obj || typeof obj !== 'object' || visited.has(obj)) { return results; } visited.add(obj); if (Array.isArray(obj)) { obj.forEach((item, index) => { findUnsupportedKeywords(item, keywords, [...path, index], results, visited); }); return results; } // Check if this object has any of the unsupported keywords for (const keyword of keywords) { if (obj.hasOwnProperty(keyword)) { results.push({ keyword, path: [...path, keyword] }); } } // Recurse into all properties for (const key in obj) { if (obj.hasOwnProperty(key)) { findUnsupportedKeywords(obj[key], keywords, [...path, key], results, visited); } } return results; } /** * Simplifies oneOf/anyOf patterns that contain $refs to work around z-schema v12 limitations. * * Z-schema v12 cannot properly resolve nested $refs within oneOf/anyOf patterns because: * 1. The $refs are relative (e.g., #/definitions/Response) * 2. When used in oneOf, z-schema loses the schema context * 3. It tries to resolve them in the user's API context instead of the schema context * * This function replaces problematic oneOf/anyOf with a lenient object schema that still * validates the basic structure while avoiding the reference resolution issue. */ function simplifyOneOfWithRefs(obj, visited = new WeakSet()) { if (!obj || typeof obj !== 'object' || visited.has(obj)) { return; } visited.add(obj); if (Array.isArray(obj)) { obj.forEach(item => simplifyOneOfWithRefs(item, visited)); return; } // Check if this object has oneOf or anyOf with $refs const hasRefsInOneOf = Array.isArray(obj.oneOf) && obj.oneOf.some(item => item && item.$ref); const hasRefsInAnyOf = Array.isArray(obj.anyOf) && obj.anyOf.some(item => item && item.$ref); if (hasRefsInOneOf || hasRefsInAnyOf) { // Replace with lenient object validation // This allows the validation to proceed while maintaining basic type checking delete obj.oneOf; delete obj.anyOf; obj.type = 'object'; obj.additionalProperties = true; return; // Don't recurse into this object since we've simplified it } // Recurse into all properties for (const key in obj) { if (obj.hasOwnProperty(key)) { simplifyOneOfWithRefs(obj[key], visited); } } } /** * Converts JSON Schema Draft 04 exclusive min/max (boolean) to Draft 6+ (numeric). * OpenAPI 3.0 uses Draft 04 style (boolean), but z-schema v12 expects Draft 6+ style (numeric). */ function convertExclusiveMinMax(obj, visited = new WeakSet()) { if (!obj || typeof obj !== 'object' || visited.has(obj)) { return; } visited.add(obj); if (Array.isArray(obj)) { obj.forEach(item => convertExclusiveMinMax(item, visited)); return; } // Convert exclusiveMinimum from boolean to numeric if (obj.exclusiveMinimum === true && typeof obj.minimum === 'number') { obj.exclusiveMinimum = obj.minimum; delete obj.minimum; } else if (obj.exclusiveMinimum === false) { delete obj.exclusiveMinimum; } // Convert exclusiveMaximum from boolean to numeric if (obj.exclusiveMaximum === true && typeof obj.maximum === 'number') { obj.exclusiveMaximum = obj.maximum; delete obj.maximum; } else if (obj.exclusiveMaximum === false) { delete obj.exclusiveMaximum; } // Recurse into all properties for (const key in obj) { if (obj.hasOwnProperty(key)) { convertExclusiveMinMax(obj[key], visited); } } } /** * Performs one-time initialization logic to prepare for Swagger Schema validation. */ function initializeZSchema () { // HACK: Delete the OpenAPI schema IDs because ZSchema can't resolve them delete openapi.v2.id; delete openapi.v3.id; delete openapi.v31.id; // Fix ReDoS-sensitive patterns for z-schema 12.x // z-schema 12 has strict ReDoS protection that rejects certain regex patterns // Swagger 2.0: Fix host pattern if (openapi.v2 && openapi.v2.properties && openapi.v2.properties.host) { // Original: ^[^{}/ :\\]+(?::\d+)?$ // Simplified to avoid ReDoS detection openapi.v2.properties.host.pattern = "^[^{}/ :\\\\]+"; } // Swagger 2.0: Fix basePath pattern if (openapi.v2 && openapi.v2.properties && openapi.v2.properties.basePath) { // Original: ^/ openapi.v2.properties.basePath.pattern = "^/"; } // Swagger 2.0: Fix schema type validation after removing external $refs // The schema.type property references JSON Schema Draft 04 for validation // Since we remove external $refs, we need to inline the type validation // JSON Schema Draft 04 allows: string, number, integer, boolean, object, array, null if (openapi.v2 && openapi.v2.definitions && openapi.v2.definitions.schema && openapi.v2.definitions.schema.properties && openapi.v2.definitions.schema.properties.type) { // Replace the external $ref with inline validation openapi.v2.definitions.schema.properties.type = { oneOf: [ { type: "string", enum: ["array", "boolean", "integer", "number", "null", "object", "string"] }, { type: "array", items: { type: "string", enum: ["array", "boolean", "integer", "number", "null", "object", "string"] }, minItems: 1, uniqueItems: true } ] }; } // Swagger 2.0: Fix response.schema oneOf for z-schema 12 compatibility // z-schema 12 is stricter with oneOf validation than v6 // The response.schema property has oneOf: [schema, fileSchema] // z-schema 12 incorrectly thinks schemas with type:"file" match both, causing false positives // Replace oneOf with anyOf - the schemas are already well-defined and mutually exclusive // (fileSchema requires type:"file", regular schema allows any valid JSON Schema type) if (openapi.v2 && openapi.v2.definitions && openapi.v2.definitions.response && openapi.v2.definitions.response.properties && openapi.v2.definitions.response.properties.schema) { const schemaProperty = openapi.v2.definitions.response.properties.schema; if (schemaProperty.oneOf) { schemaProperty.anyOf = schemaProperty.oneOf; delete schemaProperty.oneOf; } } // Swagger 2.0: Remove $schema references and make definitions more lenient if (openapi.v2) { removeSchemaReferences(openapi.v2); // Fix the schema definition to allow additionalProperties to be boolean // In Swagger 2.0, additionalProperties can be boolean OR object (schema) // But the schema incorrectly requires it to be only an object if (openapi.v2.definitions && openapi.v2.definitions.schema) { const schemaDef = openapi.v2.definitions.schema; if (schemaDef.properties && schemaDef.properties.additionalProperties) { // Allow additionalProperties to be boolean or object schemaDef.properties.additionalProperties = { oneOf: [ { type: 'boolean' }, { type: 'object' } ] }; } // Note: Swagger 2.0 spec says only allOf is supported (not oneOf/anyOf), but: // 1. We ourselves use anyOf in the schema (response.schema.anyOf at line 246) // 2. z-schema doesn't enforce property-level schema restrictions effectively // 3. This edge case doesn't affect real-world API validation // The schema definition already has additionalProperties: false // Make sure we don't override it in the leniency loop (we skip defName === 'schema') } // Make all object definitions lenient by adding additionalProperties // This allows vendor extensions (x-*) and matches real-world API usage if (openapi.v2.definitions) { for (const defName in openapi.v2.definitions) { const def = openapi.v2.definitions[defName]; if (!def || typeof def !== 'object') continue; // Skip the 'schema' definition - it needs strict validation to disallow oneOf/anyOf if (defName === 'schema') continue; // Only set additionalProperties: true if: // 1. No patternProperties (safe to add) // 2. OR patternProperties only has vendor extensions (^x-) // Don't override if patternProperties is used for validation (like responses, paths) const hasOnlyVendorPatterns = def.patternProperties && Object.keys(def.patternProperties).every(pattern => pattern === '^x-'); if ((def.type === 'object' || def.properties) && (!def.patternProperties || hasOnlyVendorPatterns)) { def.additionalProperties = true; } // Remove uniqueItems constraints that can cause false positives delete def.uniqueItems; // Process nested properties if (def.properties) { for (const propName in def.properties) { const prop = def.properties[propName]; if (prop && typeof prop === 'object') { delete prop.uniqueItems; if (prop.type === 'object' || prop.properties) { prop.additionalProperties = true; } } } } } } // Make PathItem definition accept any additional properties (for vendor extensions) if (openapi.v2.definitions && openapi.v2.definitions.pathItem) { openapi.v2.definitions.pathItem.additionalProperties = true; } // Allow null values for description and summary fields // Some APIs use null instead of omitting these optional fields const fieldsToAllowNull = ['header', 'operation', 'parameter', 'bodyParameter']; fieldsToAllowNull.forEach(defName => { const def = openapi.v2.definitions?.[defName]; if (def && def.properties) { ['description', 'summary'].forEach(prop => { if (def.properties[prop]) { def.properties[prop].type = ['string', 'null']; } }); } }); // Make parameter definition more lenient but keep critical validations // The parameter definition in Swagger 2.0 has complex oneOf patterns that cause issues // with z-schema v12. We simplify it while keeping essential rules like path parameters // must be required. const paramDef = openapi.v2.definitions?.parameter; if (paramDef) { delete paramDef.anyOf; delete paramDef.oneOf; paramDef.type = 'object'; paramDef.additionalProperties = true; // Keep the critical rule: path parameters must have required: true // Use if/then to enforce this - when "in" is "path", then "required" must be true paramDef.if = { properties: { in: { const: 'path' } }, required: ['in'] }; paramDef.then = { properties: { required: { const: true } }, required: ['required'] }; } // Also inline parameter validation rules into parametersList.items // This works around z-schema's inability to resolve $refs in oneOf patterns const paramListDef = openapi.v2.definitions?.parametersList; if (paramListDef && paramListDef.items) { // Replace the oneOf with a direct inline schema that includes: // 1. Valid parameter locations (in: query, header, path, formData, body) // 2. Path parameter rule (if in=path, then required=true) // 3. collectionFormat=multi only for query/formData (not header/path) paramListDef.items = { type: 'object', additionalProperties: true, properties: { in: { type: 'string', enum: ['query', 'header', 'path', 'formData', 'body'] } }, allOf: [ // Rule 1: Path parameters must have required=true { if: { properties: { in: { const: 'path' } }, required: ['in'] }, then: { properties: { required: { const: true } }, required: ['required'] } }, // Rule 2: collectionFormat=multi only allowed for query and formData { if: { properties: { collectionFormat: { const: 'multi' } }, required: ['collectionFormat'] }, then: { properties: { in: { enum: ['query', 'formData'] } }, required: ['in'] } } ] }; } // Simplify remaining oneOf/anyOf patterns with $refs simplifyOneOfWithRefs(openapi.v2); } // OpenAPI 3.0: Fix pattern, remove $schema references, and simplify oneOf/anyOf with $refs if (openapi.v3) { // Fix the openapi version pattern to avoid ReDoS detection if (openapi.v3.properties && openapi.v3.properties.openapi) { // Original: ^3\.0\.\d(-.+)?$ // Simplified to avoid ReDoS detection openapi.v3.properties.openapi.pattern = "^3\\.0\\."; } removeSchemaReferences(openapi.v3); // Make all object definitions lenient by adding additionalProperties // This allows vendor extensions (x-*) and matches real-world API usage if (openapi.v3.definitions) { for (const defName in openapi.v3.definitions) { const def = openapi.v3.definitions[defName]; if (!def || typeof def !== 'object') continue; // Only set additionalProperties: true if: // 1. No patternProperties (safe to add) // 2. OR patternProperties only has vendor extensions (^x-) // Don't override if patternProperties is used for validation const hasOnlyVendorPatterns = def.patternProperties && Object.keys(def.patternProperties).every(pattern => pattern === '^x-'); if ((def.type === 'object' || def.properties) && (!def.patternProperties || hasOnlyVendorPatterns)) { def.additionalProperties = true; } // Remove uniqueItems constraints delete def.uniqueItems; // Process nested properties if (def.properties) { for (const propName in def.properties) { const prop = def.properties[propName]; if (prop && typeof prop === 'object') { delete prop.uniqueItems; if (prop.type === 'object' || prop.properties) { prop.additionalProperties = true; } } } } } } // Convert Draft 04 style exclusive min/max to Draft 6+ style convertExclusiveMinMax(openapi.v3); // Simplify oneOf/anyOf patterns with $refs to work around z-schema v12 limitations // This is necessary because z-schema cannot resolve nested $refs in oneOf/anyOf simplifyOneOfWithRefs(openapi.v3); } // OpenAPI 3.1: Fix openapi version pattern if (openapi.v31 && openapi.v31.properties && openapi.v31.properties.openapi) { // Original: ^3\.1\.\d+(-.+)?$ // Simplified to avoid ReDoS detection openapi.v31.properties.openapi.pattern = "^3\\.1\\."; } // Remove all $schema references from OpenAPI 3.1 to prevent z-schema from validating // against JSON Schema 2020-12 meta-schema (which z-schema doesn't fully support) if (openapi.v31) { removeSchemaReferences(openapi.v31); // OAS 3.1 uses $defs (JSON Schema 2020-12) but z-schema only understands `definitions`. // Rename $defs -> definitions at the top level, and rewrite all #/$defs/ $ref pointers // to #/definitions/ so z-schema can resolve them. if (openapi.v31.$defs) { openapi.v31.definitions = openapi.v31.$defs; delete openapi.v31.$defs; rewriteDollarDefs(openapi.v31); } // z-schema resolves $refs in additionalProperties against the *validated document*, // not the meta-schema. Any `additionalProperties: { $ref: "#/definitions/X" }` in // the OAS 3.1 meta-schema causes z-schema to look for X inside the user's API doc, // which fails. Fix: walk definitions and replace any additionalProperties that is a // plain $ref object with {} (accept anything), since we can't do cross-schema refs. if (openapi.v31.definitions) { function fixAdditionalPropertiesRefs(obj, visited = new WeakSet()) { if (!obj || typeof obj !== 'object' || visited.has(obj)) return; visited.add(obj); for (const key of Object.keys(obj)) { if (key === 'additionalProperties' && obj[key] && typeof obj[key] === 'object' && obj[key].$ref) { obj[key] = {}; } else { fixAdditionalPropertiesRefs(obj[key], visited); } } } fixAdditionalPropertiesRefs(openapi.v31.definitions); } // Simplify oneOf/anyOf patterns with $refs for OpenAPI 3.1 as well simplifyOneOfWithRefs(openapi.v31); } // The OpenAPI 3.0 schema uses "uri-reference" formats. // The Swagger 2.0 schema uses "uri" format (e.g. license.url). // Both allow empty strings — AJV skips format validation on empty strings by default, // and an empty optional URI field ("field present but unpopulated") must not fail validation. // Any non-empty string is accepted as-is to avoid rejecting relative URLs, catalog references, etc. ZSchema.registerFormat("uri-reference", (value) => value.trim().length > 0 || value === ""); ZSchema.registerFormat("uri", (value) => value.trim().length > 0 || value === ""); // Configure ZSchema return ZSchema.create({ //breakOnFirstError: true, // Allow JSON Schema 2020-12 keywords used in OpenAPI 3.1 noExtraKeywords: false, ignoreUnknownFormats: false, reportPathAsArray: true, ignoreUnresolvableReferences: true }); } /** * Rewrites all $ref values in an object tree that start with "#/$defs/" * to "#/definitions/" so z-schema (which only understands `definitions`) can * resolve them after we rename the top-level $defs key. */ function rewriteDollarDefs(obj, visited = new WeakSet()) { if (!obj || typeof obj !== 'object' || visited.has(obj)) return; visited.add(obj); for (const key of Object.keys(obj)) { if (key === '$ref' && typeof obj[key] === 'string') { obj[key] = obj[key].replace(/^#\/\$defs\//, '#/definitions/'); } else { rewriteDollarDefs(obj[key], visited); } } } /** * Z-Schema validation errors are a nested tree structure. * This function crawls that tree and builds an error message string. * * @param {object[]} errors - The Z-Schema error details * @param {string} [indent] - The whitespace used to indent the error message * @returns {string} */ function formatZSchemaError (errors, indent) { indent = indent || " "; let message = ""; for (let error of errors) { message += util.format(`${indent}${error.message} at #/${error.path.join("/")}\n`); if (error.inner) { message += formatZSchemaError(error.inner, indent + " "); } } return message; }