Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better schema errors test #10

Merged
merged 4 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@transmute/verifiable-credentials",
"version": "0.2.2",
"version": "0.2.3",
"description": "An opinionated typescript library for w3c verifiable credentials.",
"main": "./dist/index.js",
"typings": "dist/index.d.ts",
Expand Down 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 }
149 changes: 149 additions & 0 deletions test/json-schema-tests/better-schema-errors.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import * as jose from "jose";
import moment from "moment";

import * as transmute from "../../src";

const alg = `ES256`;
const issuer = `did:example:123`;
const baseURL = `https://vendor.example/api`;

it("validate twice without error", async () => {
const privateKey = await transmute.key.generate({
alg,
type: "application/jwk+json",
});


const publicKey = await transmute.key.publicFromPrivate({
type: "application/jwk+json",
content: privateKey,
});
const issued = await transmute
.issuer({
alg,
type: "application/vc+ld+json+jwt",
signer: {
sign: async (bytes: Uint8Array) => {
const jws = await new jose.CompactSign(bytes)
.setProtectedHeader({ kid: `${issuer}#key-42`, alg })
.sign(
await transmute.key.importKeyLike({
type: "application/jwk+json",
content: privateKey,
})
);
return transmute.text.encoder.encode(jws);
},
},
})
.issue({
claimset: transmute.text.encoder.encode(`
"@context":
- https://www.w3.org/ns/credentials/v2
- ${baseURL}/context/v2
id: ${baseURL}/credentials/3732
type:
- VerifiableCredential
- ExampleDegreeCredential
issuer:
id: ${issuer}
name: "Example University"
validFrom: ${moment().toISOString()}
credentialSchema:
id: ${baseURL}/schemas/product-passport
type: JsonSchema
credentialSubject:
id: did:example:ebfeb1f712ebc6f1c276e12ec21
unexpectedProperty: unexpectedValue
degree:
type: ExampleBachelorDegree
subtype: Bachelor of Science and Arts
`),
});
const validator = await transmute.validator({
resolver: {
resolve: async ({ id, type, content }) => {
// Resolve external resources according to verifier policy
// In this case, we return inline exampes...
if (id === `${baseURL}/schemas/product-passport`) {
return {
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"
},
"degree": {
"type": "object"
}
},
"additionalProperties": false,
"errorMessage": {
"additionalProperties": "🔥 This is a custom error message for extra properties 🔥"
}
}
}
}
`),
};
}

if (content != undefined && type === `application/vc+ld+json+jwt`) {
const { kid } = jose.decodeProtectedHeader(
transmute.text.decoder.decode(content)
);
// lookup public key on a resolver
if (kid === `did:example:123#key-42`) {
return {
type: "application/jwk+json",
content: publicKey,
};
}
}
throw new Error("Resolver option not supported.");
},
},
});
const validation1 = await validator.validate({
type: "application/vc+ld+json+jwt",
content: issued,
});
expect(validation1.valid).toBe(false);
expect(validation1.schema).toEqual({
"https://vendor.example/api/schemas/product-passport": {
"valid": false,
"errors": [
{
"instancePath": "/credentialSubject",
"schemaPath": "#/properties/credentialSubject/errorMessage",
"keyword": "errorMessage",
"params": {
"errors": [
{
"instancePath": "/credentialSubject",
"schemaPath": "#/properties/credentialSubject/additionalProperties",
"keyword": "additionalProperties",
"params": {
"additionalProperty": "unexpectedProperty"
},
"message": "must NOT have additional properties",
"emUsed": true
},
]
},
"message": "🔥 This is a custom error message for extra properties 🔥"
}
]
}
}
)
});
Loading