npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@transmute/jsonld-schema

v0.7.0-unstable.82

Published

``` npm i @transmute/jsonld-schema@latest --save ```

Downloads

5,137

Readme

@transmute/jsonld-schema

npm i @transmute/jsonld-schema@latest --save

This module provides support for working with JSON-LD and JSON Schema together.

Usage

Validation without JSON Schema
import { check } from '@transmute/jsonld-schema';
import * as cred from '@transmute/credentials-context';

const input = {
  '@context': ['https://www.w3.org/2018/credentials/v1'],
  id: 'http://example.gov/credentials/3732',
  type: ['VerifiableCredential'],
  issuer: 'did:key:z6Mkk7yqnGF3YwTrLpqrW6PGsKci7dNqh1CjnvMbzrMerSeL',
  issuanceDate: '2020-03-10T04:24:12Z',
  credentialSubject: { id: 'did:example:ebfeb1f712ebc6f1c276e12ec21' },
  proof: {
    type: 'Ed25519Signature2018',
    created: '2020-03-10T04:24:12Z',
    proofPurpose: 'assertionMethod',
    verificationMethod:
      'did:key:z6Mkk7yqnGF3YwTrLpqrW6PGsKci7dNqh1CjnvMbzrMerSeL#z6Mkk7yqnGF3YwTrLpqrW6PGsKci7dNqh1CjnvMbzrMerSeL',
    jws:
      'eyJhbGciOiJFZERTQSIsImI2NCI6ZmFsc2UsImNyaXQiOlsiYjY0Il19..nH9EoZEQLZdIG8zcFbmxk4fXhTDSveQ07Ulf1SjAWZxh1ojbDPo0H67JGEG64F-lHJYxiVehzqwPWTd1-9IBAw',
  },
};

const contexts: any = {
  [cred.constants.CREDENTIALS_CONTEXT_V1_URL]: cred.contexts.get(
    cred.constants.CREDENTIALS_CONTEXT_V1_URL
  ),
};

const documentLoader = (iri: string) => {
  if (contexts[iri]) {
    return { document: contexts[iri] };
  }
  const message = 'Document loader does not support IRI: ' + iri;
  console.error(message);
  throw new Error(message);
};

const res = await check({
  input,
  documentLoader,
});
// res.ok === true
Validation with JSON Schema
const schema = {
  title: 'W3C Verifiable Credential',
  type: 'object',
  properties: {
    '@context': {
      oneOf: [
        {
          type: 'string',
        },
        {
          type: 'array',
        },
      ],
    },
    id: {
      type: 'string',
    },
    type: {
      oneOf: [
        {
          type: 'string',
        },
        {
          type: 'array',
        },
      ],
    },
    issuer: {
      oneOf: [
        {
          type: 'string',
        },
        {
          type: 'object',
          properties: {
            id: {
              type: 'string',
            },
          },
        },
      ],
    },
    issuanceDate: { type: 'string' },
    credentialSubject: {
      oneOf: [
        {
          type: 'string',
        },
        {
          type: 'object',
          properties: {
            id: {
              type: 'string',
            },
          },
          additionalProperties: false,
        },
      ],
    },
    proof: {
      type: 'object',
      properties: {
        type: {
          oneOf: [
            {
              type: 'string',
            },
            {
              type: 'array',
            },
          ],
        },
        created: {
          type: 'string',
        },
        proofPurpose: {
          type: 'string',
        },
        verificationMethod: {
          type: 'string',
        },
        jws: {
          type: 'string',
        },
      },
      additionalProperties: false,
    },
  },
  required: ['@context'],
  additionalProperties: false,
};

const res = await check({
  input,
  schema,
  documentLoader,
});
// res.ok === true
JSON-LD to JSON Schema
import { jsonldToSchema } from '@transmute/jsonld-schema';

const person = {
  '@context': {
    Person: {
      '@id': 'https://schema.org/Person',
    },
    firstName: {
      '@id': 'https://schema.org/givenName',
    },
    lastName: {
      '@id': 'https://schema.org/familyName',
    },
    email: {
      '@id': 'https://schema.org/email',
    },
    phoneNumber: {
      '@id': 'https://schema.org/telephone',
    },
    jobTitle: {
      '@id': 'https://schema.org/jobTitle',
    },
    worksFor: {
      '@id': 'https://schema.org/worksFor',
    },
  },
  type: 'Person',
  firstName: 'John',
  lastName: 'Smith',
  email: '[email protected]',
  phoneNumber: '555-555-5555',
  jobTitle: 'Director of Quality',
  worksFor: ['https://example.com/organizations/123'],
};

const schema = jsonldToSchema(person, {
  baseUrl: 'https://w3id.org/traceability/schemas',
});
JSON Schemas to JSON-LD Context
import { schemasToContext } from '@transmute/jsonld-schema';
const schemas = [
  {
    $id: 'https://w3id.org/traceability/schemas/Person.json',
    $comment: '{"term": "Person", "@id": "https://schema.org/Person"}',
    title: 'Person',
    description: 'A person',
    type: 'object',
    properties: {
      '@context': {
        type: 'array',
      },
      type: {
        oneOf: [
          {
            type: 'string',
          },
          {
            type: 'array',
          },
        ],
      },
      firstName: {
        $comment:
          '{"term": "firstName", "@id": "https://schema.org/givenName"}',
        title: 'First Name',
        description: "Person's first name.",
        type: 'string',
      },
      lastName: {
        $comment:
          '{"term": "lastName", "@id": "https://schema.org/familyName"}',
        title: 'Last Name',
        description: "Person's last name.",
        type: 'string',
      },
      email: {
        $comment: '{"term": "email", "@id": "https://schema.org/email"}',
        title: "Person's Email Address",
        description: "Person's email address.",
        type: 'string',
      },
      phoneNumber: {
        $comment:
          '{"term": "phoneNumber", "@id": "https://schema.org/telephone"}',
        title: 'Phone Number',
        description: "Person's contact phone number.",
        type: 'string',
      },
      worksFor: {
        $comment: '{"term": "worksFor", "@id": "https://schema.org/worksFor"}',
        title: 'Works For',
        description: 'Company which employs the person.',
        $ref: 'https://w3id.org/traceability/schemas/Organization.json',
      },
      jobTitle: {
        $comment: '{"term": "jobTitle", "@id": "https://schema.org/jobTitle"}',
        title: 'Job Title',
        description: "Person's job title.",
        type: 'string',
      },
    },
    additionalProperties: false,
  },
];
const context = schemasToContext(schemas);
// {
//     '@context': {
//       '@version': 1.1,
//       id: '@id',
//       type: '@type',
//       Person: {
//         '@id': 'https://schema.org/Person',
//         '@context': {
//           firstName: {
//             '@id': 'https://schema.org/givenName',
//           },
//           lastName: {
//             '@id': 'https://schema.org/familyName',
//           },
//           email: {
//             '@id': 'https://schema.org/email',
//           },
//           phoneNumber: {
//             '@id': 'https://schema.org/telephone',
//           },
//           worksFor: {
//             '@id': 'https://schema.org/worksFor',
//           },
//           jobTitle: {
//             '@id': 'https://schema.org/jobTitle',
//           },
//         },
//       },
//     },
//   }

Related Projects

We use this approach to help generate the traceability vocab.