Files
validate-json-by-json-schem…/src/validation.ts
T
t.behrendt 498601b81a
CI / Test (pull_request) Failing after 10s
CI / Check Dist (pull_request) Successful in 10s
CI / Dry-Run (pull_request) Failing after 1m1s
refactor: to node action
2026-06-27 13:42:35 +02:00

26 lines
711 B
TypeScript

import Ajv from "ajv";
export const validateJson = async (jsonFileContent: string): Promise<void> => {
const jsonObject = JSON.parse(jsonFileContent);
const schema = jsonObject["$schema"];
if (!schema) {
throw new Error("No schema found in JSON file.");
}
const schemaUrl = new URL(schema);
const schemaContent = await fetch(schemaUrl.toString());
const schemaText = await schemaContent.text();
const schemaObject = JSON.parse(schemaText);
const validator = new Ajv({
strict: false,
});
const validate = validator.compile(schemaObject);
const isValid = validate(jsonObject);
if (!isValid) {
throw new Error(`JSON file is not valid: ${validator.errorsText()}`);
}
};