šŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
Sign inDemoInstall
Socket

@kravc/schema

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@kravc/schema

Advanced JSON schema manipulation and validation library.

2.7.4
latest
Source
npm
Version published
Maintainers
1
Created
Source

@kravc/schema

Advanced JSON schema manipulation and validation library based on z-schema.

Get Started

Install npm dependency:

npm i --save @kravc/schema
const { Schema, Validator } = require('@kravc/schema')

const userSchema = new Schema({
  firstName: { required: true },
  lastName:  { required: true }
}, 'User')

const profileSchema = userSchema
  .extend({
    status: {
      enum: [ 'Pending', 'Active' ],
      default: 'Pending'
    }
  }, 'Profile')

const validator = new Validator([ profileSchema ])

const profile = validator.validate({ firstName: 'John', lastName: 'Doe' }, 'Profile')
console.log(profile)

Expected output:

{ firstName: 'John', lastName: 'Doe', status: 'Pending' }

Other Schema and Validator usage examples:

  • Schema
  • Validator

Verifiable Credentials

Class CredentialFactory allows to build a verifiable credential with embeded linked data context. Common json schema types and formats (integer, date-time, etc.) are mapped to schema.org types.

Define schema for a credential subject:

const { Schema } = require('@kravc/schema')

const accountSchema = new Schema({
  id:          { required: true },
  username:    { required: true },
  createdAt:   { format: 'date-time', required: true },
  dateOfBirth: { format: 'date' }
}, 'Account')

Initialize credential factory by providing credential URI and credential subject schemas:

const { CredentialFactory } = require('@kravc/schema')

const factory = new CredentialFactory('https://example.com/schema/AccountV1', [ accountSchema ])

Create a credential for a specific subject, createCredential method validates the input and populates any defaults defined by schema:

const holder    = 'did:HOLDER_ID'
const username  = 'USER'
const createdAt =  new Date().toISOString()
const credentialId = 'https://example.com/credentials/CREDENTIAL_ID'

const subject = {
  id: holder,
  username,
  createdAt
}

const credential = factory.createCredential(credentialId, holder, subject)
console.log(JSON.stringify(credential, null, 2))

Expected JSON-LD output (could be verified using JSON-LD Playground):

{
  "@context": [
    "https://www.w3.org/2018/credentials/v1",
    {
      "AccountV1": {
        "@id": "https://example.com/schema/AccountV1"
      },
      "Account": {
        "@id": "https://example.com/schema/AccountV1#Account",
        "@context": {
          "@vocab": "https://example.com/schema/AccountV1#",
          "@version": 1.1,
          "@protected": true,
          "schema": "https://schema.org/",
          "username": {
            "@id": "username"
          },
          "createdAt": {
            "@id": "createdAt",
            "@type": "schema:DateTime"
          },
          "dateOfBirth": {
            "@id": "dateOfBirth",
            "@type": "schema:Date"
          }
        }
      }
    }
  ],
  "id": "https://example.com/credentials/CREDENTIAL_ID",
  "type": [
    "VerifiableCredential",
    "AccountV1"
  ],
  "holder": "did:HOLDER_ID",
  "credentialSubject": {
    "id": "did:HOLDER_ID",
    "username": "USER",
    "createdAt": "2020-11-11T11:11:11.111Z",
    "type": "Account"
  }
}

Attributes issuer, issuanceDate and proof are intentionally skipped and to be set by issuing function (e.g @kravc/identity).

Other CredentialFactory examples:

Keywords

JSON

FAQs

Package last updated on 15 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts