Skip to content

Schema creation and validation tools for Verified Credentials

License

Notifications You must be signed in to change notification settings

SertoID/vc-schema-tools

Repository files navigation

VC Schema Tools

CircleCI

Installing

yarn add vc-schema-tools
# or
npm install vc-schema-tools

Validating a VC according to schema

Import the validateVc function, an async function with the following signature:

validateVc(vc: string | object): Promise<{
  valid: boolean;
  errors: string[];
  warnings: string[];
}>;

This function primarily looks at the credentialSchema property, fetching any JSON Schema from the URL found there and validating the VC according to it.

If no credentialSchema property is found, the VC is checked against a fallback schema that checks for basic properties like type, issuer, issuanceDate, etc.

Example usage:

import { validateVc, EXAMPLE_VCS } from "vc-schema-tools";

(async function () {
  const vc = JSON.parse(EXAMPLE_VCS.DiplomaCredential);

  let { valid, warnings, errors } = await validateVc(vc);
  // valid === true
  // warnings.length === 0
  // errors.length === 0

  delete vc.credentialSubject.degreeName;
  ({ valid, warnings, errors } = await validateVc(vc));
  // valid === false
  // warnings.length === 0
  // errors.length === 1
  // errors[0].includes("should have required property 'degreeName'")
})();

Generating a VC schema

This library provides a VcSchema class that can be instantiated with a JSON-LD Context Plus schema. The resulting instance can be used to validate a VC, generate a JSON-LD context, generate a JSON Schema, and various other utilities.

Example usage:

import { VcSchema, EXAMPLE_SCHEMAS, EXAMPLE_VCS } from "vc-schema-tools";

const schema = new VcSchema(EXAMPLE_SCHEMAS.DiplomaCredential);
console.log("JSON-LD Context Plus:", schema.getLdContextPlusString());
console.log("JSON-LD Context:", schema.getJsonLdContextString());
console.log("JSON Schema:", schema.getJsonSchemaString());

schema.validateVc(EXAMPLE_VCS.DiplomaCredential, (isValid, message) => {
  console.log("VC validity:", isValid);
  console.log("Validation message:", message);
});

About

Schema creation and validation tools for Verified Credentials

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 3

  •  
  •  
  •