|
| 1 | +import {fetchCdn} from "../../utils/cdnHelper.js"; |
| 2 | +import {RunSchemaValidationResult} from "./schema.js"; |
| 3 | +import Ajv2020, {AnySchemaObject} from "ajv/dist/2020.js"; |
| 4 | +import addFormats from "ajv-formats"; |
| 5 | +import {readFile} from "fs/promises"; |
| 6 | +import {getLogger} from "@ui5/logger"; |
| 7 | +import {InvalidInputError} from "../../utils.js"; |
| 8 | +import {getManifestSchema, getManifestVersion} from "../../utils/ui5Manifest.js"; |
| 9 | +import {Mutex} from "async-mutex"; |
| 10 | +import {fileURLToPath} from "url"; |
| 11 | +import {isAbsolute} from "path"; |
| 12 | + |
| 13 | +const log = getLogger("tools:run_manifest_validation:runValidation"); |
| 14 | +const schemaCache = new Map<string, AnySchemaObject>(); |
| 15 | +const fetchSchemaMutex = new Mutex(); |
| 16 | + |
| 17 | +const AJV_SCHEMA_PATHS = { |
| 18 | + draft06: fileURLToPath(import.meta.resolve("ajv/dist/refs/json-schema-draft-06.json")), |
| 19 | + draft07: fileURLToPath(import.meta.resolve("ajv/dist/refs/json-schema-draft-07.json")), |
| 20 | +} as const; |
| 21 | + |
| 22 | +async function createUI5ManifestValidateFunction(ui5Schema: object) { |
| 23 | + try { |
| 24 | + const ajv = new Ajv2020.default({ |
| 25 | + // Collect all errors, not just the first one |
| 26 | + allErrors: true, |
| 27 | + // Allow additional properties that are not in schema such as "i18n", |
| 28 | + // otherwise compilation fails |
| 29 | + strict: false, |
| 30 | + // Don't use Unicode-aware regular expressions, |
| 31 | + // otherwise compilation fails with "Invalid escape" errors |
| 32 | + unicodeRegExp: false, |
| 33 | + loadSchema: async (uri) => { |
| 34 | + const release = await fetchSchemaMutex.acquire(); |
| 35 | + |
| 36 | + try { |
| 37 | + if (schemaCache.has(uri)) { |
| 38 | + log.info(`Loading cached schema: ${uri}`); |
| 39 | + return schemaCache.get(uri)!; |
| 40 | + } |
| 41 | + |
| 42 | + log.info(`Loading external schema: ${uri}`); |
| 43 | + const schema = await fetchCdn(uri) as AnySchemaObject; |
| 44 | + |
| 45 | + // Special handling for Adaptive Card schema to fix unsupported "id" property |
| 46 | + // According to the JSON Schema spec Draft 06 (used by Adaptive Card schema), |
| 47 | + // "$id" should be used instead of "id" |
| 48 | + // See https://github.com/microsoft/AdaptiveCards/issues/9274 |
| 49 | + if (uri.includes("adaptive-card.json") && typeof schema.id === "string") { |
| 50 | + schema.$id = schema.id; |
| 51 | + delete schema.id; |
| 52 | + } |
| 53 | + |
| 54 | + schemaCache.set(uri, schema); |
| 55 | + |
| 56 | + return schema; |
| 57 | + } catch (error) { |
| 58 | + log.warn(`Failed to load external schema ${uri}:` + |
| 59 | + `${error instanceof Error ? error.message : String(error)}`); |
| 60 | + |
| 61 | + throw error; |
| 62 | + } finally { |
| 63 | + release(); |
| 64 | + } |
| 65 | + }, |
| 66 | + }); |
| 67 | + |
| 68 | + addFormats.default(ajv); |
| 69 | + |
| 70 | + const draft06MetaSchema = JSON.parse( |
| 71 | + await readFile(AJV_SCHEMA_PATHS.draft06, "utf-8") |
| 72 | + ) as AnySchemaObject; |
| 73 | + const draft07MetaSchema = JSON.parse( |
| 74 | + await readFile(AJV_SCHEMA_PATHS.draft07, "utf-8") |
| 75 | + ) as AnySchemaObject; |
| 76 | + |
| 77 | + // Add meta-schemas for draft-06 and draft-07. |
| 78 | + // These are required to support schemas that reference these drafts, |
| 79 | + // for example the Adaptive Card schema and some sap.bpa.task properties. |
| 80 | + ajv.addMetaSchema(draft06MetaSchema, "http://json-schema.org/draft-06/schema#"); |
| 81 | + ajv.addMetaSchema(draft07MetaSchema, "http://json-schema.org/draft-07/schema#"); |
| 82 | + |
| 83 | + const validate = await ajv.compileAsync(ui5Schema); |
| 84 | + |
| 85 | + return validate; |
| 86 | + } catch (error) { |
| 87 | + throw new Error(`Failed to create UI5 manifest validate function: ` + |
| 88 | + `${error instanceof Error ? error.message : String(error)}`); |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +async function readManifest(path: string) { |
| 93 | + let content: string; |
| 94 | + let json: object; |
| 95 | + |
| 96 | + if (!isAbsolute(path)) { |
| 97 | + throw new InvalidInputError(`The manifest path must be absolute: '${path}'`); |
| 98 | + } |
| 99 | + |
| 100 | + try { |
| 101 | + content = await readFile(path, "utf-8"); |
| 102 | + } catch (error) { |
| 103 | + throw new InvalidInputError(`Failed to read manifest file at ${path}: ` + |
| 104 | + `${error instanceof Error ? error.message : String(error)}`); |
| 105 | + } |
| 106 | + |
| 107 | + try { |
| 108 | + json = JSON.parse(content) as object; |
| 109 | + } catch (error) { |
| 110 | + throw new InvalidInputError(`Failed to parse manifest file at ${path} as JSON: ` + |
| 111 | + `${error instanceof Error ? error.message : String(error)}`); |
| 112 | + } |
| 113 | + |
| 114 | + return json; |
| 115 | +} |
| 116 | + |
| 117 | +export default async function runValidation(manifestPath: string): Promise<RunSchemaValidationResult> { |
| 118 | + log.info(`Starting manifest validation for file: ${manifestPath}`); |
| 119 | + |
| 120 | + const manifest = await readManifest(manifestPath); |
| 121 | + const manifestVersion = await getManifestVersion(manifest); |
| 122 | + log.info(`Using manifest version: ${manifestVersion}`); |
| 123 | + const ui5ManifestSchema = await getManifestSchema(manifestVersion); |
| 124 | + const validate = await createUI5ManifestValidateFunction(ui5ManifestSchema); |
| 125 | + const isValid = validate(manifest); |
| 126 | + |
| 127 | + if (isValid) { |
| 128 | + log.info("Manifest validation successful"); |
| 129 | + |
| 130 | + return { |
| 131 | + isValid: true, |
| 132 | + errors: [], |
| 133 | + }; |
| 134 | + } |
| 135 | + |
| 136 | + // Map AJV errors to our schema format |
| 137 | + const validationErrors = validate.errors ?? []; |
| 138 | + const errors = validationErrors.map((error): RunSchemaValidationResult["errors"][number] => { |
| 139 | + return { |
| 140 | + keyword: error.keyword ?? "", |
| 141 | + instancePath: error.instancePath ?? "", |
| 142 | + schemaPath: error.schemaPath ?? "", |
| 143 | + params: error.params ?? {}, |
| 144 | + propertyName: error.propertyName, |
| 145 | + message: error.message, |
| 146 | + }; |
| 147 | + }); |
| 148 | + |
| 149 | + log.info(`Manifest validation failed with ${errors.length} error(s)`); |
| 150 | + |
| 151 | + return { |
| 152 | + isValid: false, |
| 153 | + errors: errors, |
| 154 | + }; |
| 155 | +} |
0 commit comments