From 44aed0fcb7e306b3f4eef05c244560f93c5dd36f Mon Sep 17 00:00:00 2001 From: Orie Steele Date: Tue, 11 Jun 2024 20:33:06 -0500 Subject: [PATCH] Add custom errors test --- package-lock.json | 16 ++++++ package.json | 1 + src/cr1/validator/ajv.ts | 5 +- .../better-schema-errors.test.ts | 56 ++++++++++++------- 4 files changed, 58 insertions(+), 20 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6ec40e8..29c0217 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@transmute/cose": "^0.1.1", "@transmute/vc-jwt-sd": "^0.1.3", "ajv": "^8.12.0", + "ajv-errors": "^3.0.0", "ajv-formats": "^3.0.1", "jose": "^5.2.0", "json-pointer": "^0.6.2", @@ -1991,6 +1992,15 @@ "url": "https://github.com/sponsors/epoberezkin" } }, + "node_modules/ajv-errors": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/ajv-errors/-/ajv-errors-3.0.0.tgz", + "integrity": "sha512-V3wD15YHfHz6y0KdhYFjyy9vWtEVALT9UrxfN3zqlI6dMioHnJrqOYfyPKol3oqrnCM9uwkcdCwkJ0WUcbLMTQ==", + "license": "MIT", + "peerDependencies": { + "ajv": "^8.0.1" + } + }, "node_modules/ajv-formats": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-3.0.1.tgz", @@ -7063,6 +7073,12 @@ "uri-js": "^4.2.2" } }, + "ajv-errors": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/ajv-errors/-/ajv-errors-3.0.0.tgz", + "integrity": "sha512-V3wD15YHfHz6y0KdhYFjyy9vWtEVALT9UrxfN3zqlI6dMioHnJrqOYfyPKol3oqrnCM9uwkcdCwkJ0WUcbLMTQ==", + "requires": {} + }, "ajv-formats": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/ajv-formats/-/ajv-formats-3.0.1.tgz", diff --git a/package.json b/package.json index c45c4e6..5003c01 100644 --- a/package.json +++ b/package.json @@ -46,6 +46,7 @@ "@transmute/cose": "^0.1.1", "@transmute/vc-jwt-sd": "^0.1.3", "ajv": "^8.12.0", + "ajv-errors": "^3.0.0", "ajv-formats": "^3.0.1", "jose": "^5.2.0", "json-pointer": "^0.6.2", diff --git a/src/cr1/validator/ajv.ts b/src/cr1/validator/ajv.ts index 9df4d23..c0ffe4b 100644 --- a/src/cr1/validator/ajv.ts +++ b/src/cr1/validator/ajv.ts @@ -1,11 +1,14 @@ import Ajv from 'ajv/dist/2020' import addFormats from 'ajv-formats' - +import ajvErrors from 'ajv-errors' const ajv = new Ajv({ strict: false, + allErrors: true }) addFormats(ajv) +ajvErrors(ajv) + export { ajv } diff --git a/test/json-schema-tests/better-schema-errors.test.ts b/test/json-schema-tests/better-schema-errors.test.ts index 93cfeda..1878277 100644 --- a/test/json-schema-tests/better-schema-errors.test.ts +++ b/test/json-schema-tests/better-schema-errors.test.ts @@ -70,22 +70,28 @@ credentialSubject: type: `application/schema+json`, content: transmute.text.encoder.encode(` { -"$id": "${baseURL}/schemas/product-passport", -"$schema": "https://json-schema.org/draft/2020-12/schema", -"title": "Example JSON Schema", -"description": "This is a test schema", -"type": "object", -"properties": { - "credentialSubject": { - "type": "object", - "properties": { - "id": { - "type": "string" + "$id": "${baseURL}/schemas/product-passport", + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "Example JSON Schema", + "description": "This is a test schema", + "type": "object", + "properties": { + "credentialSubject": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "degree": { + "type": "object" + } + }, + "additionalProperties": false, + "errorMessage": { + "additionalProperties": "🔥 This is a custom error message for extra properties 🔥" } - }, - "additionalProperties": false + } } -} } `), }; @@ -118,14 +124,26 @@ credentialSubject: "errors": [ { "instancePath": "/credentialSubject", - "schemaPath": "#/properties/credentialSubject/additionalProperties", - "keyword": "additionalProperties", + "schemaPath": "#/properties/credentialSubject/errorMessage", + "keyword": "errorMessage", "params": { - "additionalProperty": "unexpectedProperty" + "errors": [ + { + "instancePath": "/credentialSubject", + "schemaPath": "#/properties/credentialSubject/additionalProperties", + "keyword": "additionalProperties", + "params": { + "additionalProperty": "unexpectedProperty" + }, + "message": "must NOT have additional properties", + "emUsed": true + }, + ] }, - "message": "must NOT have additional properties" + "message": "🔥 This is a custom error message for extra properties 🔥" } ] } - }) + } + ) }); \ No newline at end of file