SCHEMA - delete $schema , it is only a metaschema property now allowed in data, update ajv validator

This commit is contained in:
Matthieu Morin 2024-04-05 16:03:29 +02:00
parent 0115cb501c
commit aca901c2a7
8 changed files with 13 additions and 9 deletions

View File

@ -40,7 +40,7 @@
"title": "services under the category",
"type": "array",
"items": {
"$ref": "src/content/schema-services.json"
"$ref": "schema-services.json#"
}
}
}

View File

@ -1,5 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "schema-services.json",
"title": "Service schema",
"type": "object",
"required": ["title", "description", "id", "duration", "price"],

View File

@ -1,5 +1,4 @@
{
"$schema": "../../schema-categories.json",
"title": "DALŠÍ VELMI OBLÍBENÉ SLUŽBY",
"description": "A description of a description",
"id": "depilace",

View File

@ -1,5 +1,4 @@
{
"$schema": "../../schema-categories.json",
"title": "Depilace",
"description": "A description of a description",
"id": "depilace",

View File

@ -1,5 +1,4 @@
{
"$schema": "../../schema-categories.json",
"title": "Kosmetické ošetření",
"description": "A description of a description",
"id": "depilace",

View File

@ -1,5 +1,4 @@
{
"$schema": "../../schema-categories.json",
"title": "Permanentní make-up",
"description": "A description of a description",
"id": "pmu",

View File

@ -1,5 +1,4 @@
{
"$schema": "../../schema-categories.json",
"title": "Vakuslim 48 - zeštíhlující procedura",
"description": "A description of a description",
"id": "depilace",

View File

@ -1,17 +1,24 @@
import Ajv from 'ajv';
import fs from 'fs';
import path from 'path';
const ajv = new Ajv({verbose: true});
// Load and add the service schema
const serviceSchema = JSON.parse(fs.readFileSync('./src/content/schema-services.json', 'utf-8'));
ajv.addSchema(serviceSchema, 'serviceSchema');
const serviceSchema = JSON.parse(fs.readFileSync('./src/content/schema-categories.json', 'utf-8'));
// Load and add the category schema
const categorySchema = JSON.parse(fs.readFileSync('./src/content/schema-categories.json', 'utf-8'));
ajv.addSchema(categorySchema, 'categorySchema');
// Compile the category schema validator
// const validate = ajv.getSchema('categorySchema');
/**
* @param {string} filePath
*/
function validateFile(filePath) {
const ajv = new Ajv();
const data = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
const validate = ajv.compile(serviceSchema);
const validate = ajv.compile(categorySchema);
const valid = validate(data);
if (!valid) {
throw new Error(`Invalid service data in ${filePath}: ${JSON.stringify(validate.errors)}`);
@ -40,6 +47,7 @@ export function validateServices() {
console.log('All services validated successfully!');
} catch (error) {
console.error(`Error validating services: ${error}`);
console.log(ajv.errors);
process.exit(1); // Exit with non-zero code to signal failure
}
}