From aca901c2a71b89d27c2df98686ad035efa3871ac Mon Sep 17 00:00:00 2001 From: Matthieu Morin Date: Fri, 5 Apr 2024 16:03:29 +0200 Subject: [PATCH] SCHEMA - delete $schema , it is only a metaschema property now allowed in data, update ajv validator --- src/content/schema-categories.json | 2 +- src/content/schema-services.json | 1 + .../services/dalsi-sluzby/dalsi-sluzby.json | 1 - src/content/services/depilace/depilace.json | 1 - .../kosmeticke-osetreni/kosmeticke-osetreni.json | 1 - .../permanentni-make-up/permanentni-make-up.json | 1 - src/content/services/vakuslim/vakuslim.json | 1 - tests/ValidateServices.js | 14 +++++++++++--- 8 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/content/schema-categories.json b/src/content/schema-categories.json index 2c4b63e..be91602 100644 --- a/src/content/schema-categories.json +++ b/src/content/schema-categories.json @@ -40,7 +40,7 @@ "title": "services under the category", "type": "array", "items": { - "$ref": "src/content/schema-services.json" + "$ref": "schema-services.json#" } } } diff --git a/src/content/schema-services.json b/src/content/schema-services.json index fba7982..39da6d4 100644 --- a/src/content/schema-services.json +++ b/src/content/schema-services.json @@ -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"], diff --git a/src/content/services/dalsi-sluzby/dalsi-sluzby.json b/src/content/services/dalsi-sluzby/dalsi-sluzby.json index db447dc..f4f3858 100644 --- a/src/content/services/dalsi-sluzby/dalsi-sluzby.json +++ b/src/content/services/dalsi-sluzby/dalsi-sluzby.json @@ -1,5 +1,4 @@ { - "$schema": "../../schema-categories.json", "title": "DALŠÍ VELMI OBLÍBENÉ SLUŽBY", "description": "A description of a description", "id": "depilace", diff --git a/src/content/services/depilace/depilace.json b/src/content/services/depilace/depilace.json index dad810d..7cc2e00 100644 --- a/src/content/services/depilace/depilace.json +++ b/src/content/services/depilace/depilace.json @@ -1,5 +1,4 @@ { - "$schema": "../../schema-categories.json", "title": "Depilace", "description": "A description of a description", "id": "depilace", diff --git a/src/content/services/kosmeticke-osetreni/kosmeticke-osetreni.json b/src/content/services/kosmeticke-osetreni/kosmeticke-osetreni.json index f71bd4a..32755d7 100644 --- a/src/content/services/kosmeticke-osetreni/kosmeticke-osetreni.json +++ b/src/content/services/kosmeticke-osetreni/kosmeticke-osetreni.json @@ -1,5 +1,4 @@ { - "$schema": "../../schema-categories.json", "title": "Kosmetické ošetření", "description": "A description of a description", "id": "depilace", diff --git a/src/content/services/permanentni-make-up/permanentni-make-up.json b/src/content/services/permanentni-make-up/permanentni-make-up.json index 61dad1e..6765b18 100644 --- a/src/content/services/permanentni-make-up/permanentni-make-up.json +++ b/src/content/services/permanentni-make-up/permanentni-make-up.json @@ -1,5 +1,4 @@ { - "$schema": "../../schema-categories.json", "title": "Permanentní make-up", "description": "A description of a description", "id": "pmu", diff --git a/src/content/services/vakuslim/vakuslim.json b/src/content/services/vakuslim/vakuslim.json index 500fac2..ca53819 100644 --- a/src/content/services/vakuslim/vakuslim.json +++ b/src/content/services/vakuslim/vakuslim.json @@ -1,5 +1,4 @@ { - "$schema": "../../schema-categories.json", "title": "Vakuslim 48 - zeštíhlující procedura", "description": "A description of a description", "id": "depilace", diff --git a/tests/ValidateServices.js b/tests/ValidateServices.js index c4443a3..05a4507 100644 --- a/tests/ValidateServices.js +++ b/tests/ValidateServices.js @@ -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 } }