Skip to content

Commit

Permalink
Add custom errors test
Browse files Browse the repository at this point in the history
  • Loading branch information
OR13 committed Jun 12, 2024
1 parent 4c10ab3 commit 44aed0f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 20 deletions.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
5 changes: 4 additions & 1 deletion src/cr1/validator/ajv.ts
Original file line number Diff line number Diff line change
@@ -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 }
56 changes: 37 additions & 19 deletions test/json-schema-tests/better-schema-errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
`),
};
Expand Down Expand Up @@ -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 🔥"
}
]
}
})
}
)
});

0 comments on commit 44aed0f

Please sign in to comment.