# vc-schema-tools

> Schema creation and validation tools for Verified Credentials

Latest version **0.3.4** (published 2021-12-15) · 0 weekly downloads

## Install

```sh
npm install vc-schema-tools
pnpm add vc-schema-tools
yarn add vc-schema-tools
bun add vc-schema-tools
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.3.4 |
| Published | 2021-12-15 |
| First published | 2021-02-03 |
| Weekly downloads | 0 |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 157.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Serto |
| Maintainers | devops-serto, tobek, aldigjo, sruddy, italobb |

## Links

- npm: https://www.npmjs.com/package/vc-schema-tools
- Repository: https://github.com/SertoID/vc-schema-tools
- Homepage: https://serto.id
- Issues: https://github.com/SertoID/vc-schema-tools/issues
- npm.io page: https://npm.io/package/vc-schema-tools

## Dependencies (5)

- [ajv](https://npm.io/package/ajv.md) ^8.7.1
- [typescript](https://npm.io/package/typescript.md) ^4.1.3
- [ajv-formats](https://npm.io/package/ajv-formats.md) ^2.1.1
- [cross-fetch](https://npm.io/package/cross-fetch.md) ^3.1.4
- [@transmute/jsonld-schema](https://npm.io/package/@transmute/jsonld-schema.md) ^0.7.0-unstable.28

## Recent versions

- 0.3.4 (latest) — 2021-12-15
- 0.3.3 — 2021-12-09
- 0.3.2 — 2021-12-06
- 0.3.0 — 2021-11-17
- 0.2.5 — 2021-10-14
- 0.2.4 — 2021-07-29
- 0.2.3 — 2021-05-28
- 0.2.2 — 2021-05-18
- 0.2.1 — 2021-05-07
- 0.2.0 — 2021-04-26
- 0.1.1 — 2021-02-25
- 0.1.0 — 2021-02-03

## README

## VC Schema Tools

[![CircleCI](https://circleci.com/gh/SertoID/vc-schema-tools.svg?style=svg)](https://circleci.com/gh/SertoID/vc-schema-tools)

### Installing

```bash
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:

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

This function primarily looks at the [`credentialSchema`](https://www.w3.org/TR/vc-data-model/#data-schemas) property, fetching any [JSON Schema](https://json-schema.org/) 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:

```js
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](https://docs.google.com/document/d/1l41XsI1nTCxx3T6IpAV59UkBrMfRzC89TZRPYiDjOC4/edit?usp=sharing). 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:

```js
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);
});
```

---
_Source: https://npm.io/package/vc-schema-tools · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
