|
| 1 | +import {fetchCdn} from "../../utils/cdnHelper.js"; |
| 2 | +import Ajv2020, {AnySchemaObject, ValidateFunction} from "ajv/dist/2020.js"; |
| 3 | +import Ajv from "ajv"; |
| 4 | +import addFormats from "ajv-formats"; |
| 5 | +import {readFile} from "fs/promises"; |
| 6 | +import {getLogger} from "@ui5/logger"; |
| 7 | +import {Mutex} from "async-mutex"; |
| 8 | +import {fileURLToPath} from "url"; |
| 9 | + |
| 10 | +const log = getLogger("tools:run_manifest_validation:createValidationFunction"); |
| 11 | +const schemaCache = new Map<string, AnySchemaObject>(); |
| 12 | +const fetchSchemaMutex = new Mutex(); |
| 13 | + |
| 14 | +const AJV_SCHEMA_PATHS = { |
| 15 | + draft06: fileURLToPath(import.meta.resolve("ajv/dist/refs/json-schema-draft-06.json")), |
| 16 | + draft07: fileURLToPath(import.meta.resolve("ajv/dist/refs/json-schema-draft-07.json")), |
| 17 | +} as const; |
| 18 | + |
| 19 | +async function createUI5ManifestValidateFunction2020(ui5Schema: object) { |
| 20 | + try { |
| 21 | + const ajv = new Ajv2020.default({ |
| 22 | + // Collect all errors, not just the first one |
| 23 | + allErrors: true, |
| 24 | + // Allow additional properties that are not in schema such as "i18n", |
| 25 | + // otherwise compilation fails |
| 26 | + strict: false, |
| 27 | + // Don't use Unicode-aware regular expressions, |
| 28 | + // otherwise compilation fails with "Invalid escape" errors |
| 29 | + unicodeRegExp: false, |
| 30 | + loadSchema: async (uri) => { |
| 31 | + const release = await fetchSchemaMutex.acquire(); |
| 32 | + |
| 33 | + try { |
| 34 | + if (schemaCache.has(uri)) { |
| 35 | + log.info(`Loading cached schema: ${uri}`); |
| 36 | + return schemaCache.get(uri)!; |
| 37 | + } |
| 38 | + |
| 39 | + log.info(`Loading external schema: ${uri}`); |
| 40 | + const schema = await fetchCdn(uri) as AnySchemaObject; |
| 41 | + |
| 42 | + // Special handling for Adaptive Card schema to fix unsupported "id" property |
| 43 | + // According to the JSON Schema spec Draft 06 (used by Adaptive Card schema), |
| 44 | + // "$id" should be used instead of "id" |
| 45 | + // See https://github.com/microsoft/AdaptiveCards/issues/9274 |
| 46 | + if (uri.includes("adaptive-card.json") && typeof schema.id === "string") { |
| 47 | + schema.$id = schema.id; |
| 48 | + delete schema.id; |
| 49 | + } |
| 50 | + |
| 51 | + schemaCache.set(uri, schema); |
| 52 | + |
| 53 | + return schema; |
| 54 | + } catch (error) { |
| 55 | + log.warn(`Failed to load external schema ${uri}:` + |
| 56 | + `${error instanceof Error ? error.message : String(error)}`); |
| 57 | + |
| 58 | + throw error; |
| 59 | + } finally { |
| 60 | + release(); |
| 61 | + } |
| 62 | + }, |
| 63 | + }); |
| 64 | + |
| 65 | + addFormats.default(ajv); |
| 66 | + |
| 67 | + const draft06MetaSchema = JSON.parse( |
| 68 | + await readFile(AJV_SCHEMA_PATHS.draft06, "utf-8") |
| 69 | + ) as AnySchemaObject; |
| 70 | + const draft07MetaSchema = JSON.parse( |
| 71 | + await readFile(AJV_SCHEMA_PATHS.draft07, "utf-8") |
| 72 | + ) as AnySchemaObject; |
| 73 | + |
| 74 | + // Add meta-schemas for draft-06 and draft-07. |
| 75 | + // These are required to support schemas that reference these drafts, |
| 76 | + // for example the Adaptive Card schema and some sap.bpa.task properties. |
| 77 | + |
| 78 | + ajv.addMetaSchema(draft06MetaSchema, "http://json-schema.org/draft-06/schema#"); |
| 79 | + ajv.addMetaSchema(draft07MetaSchema, "http://json-schema.org/draft-07/schema#"); |
| 80 | + |
| 81 | + // Special handling for UI5 manifest schemas (e.g., v1.68.0) that use "items" as an array |
| 82 | + // In JSON Schema 2020-12, "items" must be an object or boolean, not an array |
| 83 | + // Arrays should use "prefixItems" instead |
| 84 | + // See: https://json-schema.org/draft/2020-12/json-schema-core#section-10.3.1.2 |
| 85 | + // See: https://github.com/UI5/manifest/issues/34 |
| 86 | + interface SchemaWithDefs { |
| 87 | + $defs?: Record<string, { |
| 88 | + oneOf?: { |
| 89 | + items?: unknown; |
| 90 | + prefixItems?: unknown; |
| 91 | + }[]; |
| 92 | + }>; |
| 93 | + } |
| 94 | + |
| 95 | + const schemaToCompile = ui5Schema as SchemaWithDefs & AnySchemaObject; |
| 96 | + if (schemaToCompile?.$defs) { |
| 97 | + for (const defKey in schemaToCompile.$defs) { |
| 98 | + const def = schemaToCompile.$defs[defKey]; |
| 99 | + if (def?.oneOf) { |
| 100 | + for (const option of def.oneOf) { |
| 101 | + if (Array.isArray(option?.items)) { |
| 102 | + option.prefixItems = option.items; |
| 103 | + delete option.items; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + const validate = await ajv.compileAsync(schemaToCompile); |
| 111 | + return validate; |
| 112 | + } catch (error) { |
| 113 | + throw new Error(`Failed to create UI5 manifest validate function: ` + |
| 114 | + `${error instanceof Error ? error.message : String(error)}`); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +async function createUI5ManifestValidateFunctionDraft07(ui5Schema: object) { |
| 119 | + try { |
| 120 | + const ajv = new Ajv.default({ |
| 121 | + // Collect all errors, not just the first one |
| 122 | + allErrors: true, |
| 123 | + // Allow additional properties that are not in schema such as "i18n", |
| 124 | + // otherwise compilation fails |
| 125 | + strict: false, |
| 126 | + // Don't use Unicode-aware regular expressions, |
| 127 | + // otherwise compilation fails with "Invalid escape" errors |
| 128 | + unicodeRegExp: false, |
| 129 | + loadSchema: async (uri) => { |
| 130 | + const release = await fetchSchemaMutex.acquire(); |
| 131 | + |
| 132 | + try { |
| 133 | + if (schemaCache.has(uri)) { |
| 134 | + log.info(`Loading cached schema: ${uri}`); |
| 135 | + return schemaCache.get(uri)!; |
| 136 | + } |
| 137 | + |
| 138 | + log.info(`Loading external schema: ${uri}`); |
| 139 | + const schema = await fetchCdn(uri) as AnySchemaObject; |
| 140 | + |
| 141 | + // Special handling for Adaptive Card schema to fix unsupported "id" property |
| 142 | + // According to the JSON Schema spec Draft 06 (used by Adaptive Card schema), |
| 143 | + // "$id" should be used instead of "id" |
| 144 | + // See https://github.com/microsoft/AdaptiveCards/issues/9274 |
| 145 | + if (uri.includes("adaptive-card.json") && typeof schema.id === "string") { |
| 146 | + schema.$id = schema.id; |
| 147 | + delete schema.id; |
| 148 | + } |
| 149 | + |
| 150 | + schemaCache.set(uri, schema); |
| 151 | + |
| 152 | + return schema; |
| 153 | + } catch (error) { |
| 154 | + log.warn(`Failed to load external schema ${uri}:` + |
| 155 | + `${error instanceof Error ? error.message : String(error)}`); |
| 156 | + |
| 157 | + throw error; |
| 158 | + } finally { |
| 159 | + release(); |
| 160 | + } |
| 161 | + }, |
| 162 | + }); |
| 163 | + |
| 164 | + addFormats.default(ajv); |
| 165 | + |
| 166 | + const draft06MetaSchema = JSON.parse( |
| 167 | + await readFile(AJV_SCHEMA_PATHS.draft06, "utf-8") |
| 168 | + ) as AnySchemaObject; |
| 169 | + |
| 170 | + // Add meta-schema for draft-06. |
| 171 | + // This is required to support schemas that reference this draft, |
| 172 | + // for example the Adaptive Card schema. |
| 173 | + ajv.addMetaSchema(draft06MetaSchema, "http://json-schema.org/draft-06/schema#"); |
| 174 | + |
| 175 | + const validate = await ajv.compileAsync(ui5Schema); |
| 176 | + return validate; |
| 177 | + } catch (error) { |
| 178 | + throw new Error(`Failed to create UI5 manifest validate function: ` + |
| 179 | + `${error instanceof Error ? error.message : String(error)}`); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +export async function createValidateFunction( |
| 184 | + ui5ManifestSchema: object |
| 185 | +): Promise<ValidateFunction> { |
| 186 | + // Determine which validator to use based on the $schema attribute |
| 187 | + const schema = ui5ManifestSchema as {$schema?: string}; |
| 188 | + const metaSchema = schema?.$schema; |
| 189 | + |
| 190 | + if (metaSchema === "https://json-schema.org/draft/2020-12/schema") { |
| 191 | + log.info(`Using JSON Schema 2020-12 validation (detected from $schema: ${metaSchema})`); |
| 192 | + return createUI5ManifestValidateFunction2020(ui5ManifestSchema); |
| 193 | + } else if (metaSchema === "http://json-schema.org/draft-07/schema#") { |
| 194 | + log.info(`Using Draft-07 validation (detected from $schema: ${metaSchema})`); |
| 195 | + return createUI5ManifestValidateFunctionDraft07(ui5ManifestSchema); |
| 196 | + } else { |
| 197 | + throw new Error( |
| 198 | + `Failed to create UI5 manifest validate function:` + |
| 199 | + ` ${metaSchema ?? "undefined"} is not a supported meta-schema. ` + |
| 200 | + `Supported meta-schemas: "https://json-schema.org/draft/2020-12/schema", "http://json-schema.org/draft-07/schema#"` |
| 201 | + ); |
| 202 | + } |
| 203 | +} |
0 commit comments