0.3.4 • Published 2 years ago

vc-schema-tools v0.3.4

Weekly downloads
66
License
-
Repository
github
Last release
2 years ago

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);
});
0.3.2

2 years ago

0.3.4

2 years ago

0.3.3

2 years ago

0.3.0

2 years ago

0.2.5

3 years ago

0.2.4

3 years ago

0.2.3

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.2.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago