项目作者: alexkravets

项目描述 :
Advanced JSON schema manipulation and validation library.
高级语言: JavaScript
项目地址: git://github.com/alexkravets/schema.git
创建时间: 2020-05-17T02:31:05Z
项目社区:https://github.com/alexkravets/schema

开源协议:

下载


@kravc/schema

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

Get Started

Install npm dependency:

  1. npm i --save @kravc/schema
  1. const { Schema, Validator } = require('@kravc/schema')
  2. const userSchema = new Schema({
  3. firstName: { required: true },
  4. lastName: { required: true }
  5. }, 'User')
  6. const profileSchema = userSchema
  7. .extend({
  8. status: {
  9. enum: [ 'Pending', 'Active' ],
  10. default: 'Pending'
  11. }
  12. }, 'Profile')
  13. const validator = new Validator([ profileSchema ])
  14. const profile = validator.validate({ firstName: 'John', lastName: 'Doe' }, 'Profile')
  15. console.log(profile)

Expected output:

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

Other Schema and Validator usage examples:

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:

  1. const { Schema } = require('@kravc/schema')
  2. const accountSchema = new Schema({
  3. id: { required: true },
  4. username: { required: true },
  5. createdAt: { format: 'date-time', required: true },
  6. dateOfBirth: { format: 'date' }
  7. }, 'Account')

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

  1. const { CredentialFactory } = require('@kravc/schema')
  2. 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:

  1. const holder = 'did:HOLDER_ID'
  2. const username = 'USER'
  3. const createdAt = new Date().toISOString()
  4. const credentialId = 'https://example.com/credentials/CREDENTIAL_ID'
  5. const subject = {
  6. id: holder,
  7. username,
  8. createdAt
  9. }
  10. const credential = factory.createCredential(credentialId, holder, subject)
  11. console.log(JSON.stringify(credential, null, 2))

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

  1. {
  2. "@context": [
  3. "https://www.w3.org/2018/credentials/v1",
  4. {
  5. "AccountV1": {
  6. "@id": "https://example.com/schema/AccountV1"
  7. },
  8. "Account": {
  9. "@id": "https://example.com/schema/AccountV1#Account",
  10. "@context": {
  11. "@vocab": "https://example.com/schema/AccountV1#",
  12. "@version": 1.1,
  13. "@protected": true,
  14. "schema": "https://schema.org/",
  15. "username": {
  16. "@id": "username"
  17. },
  18. "createdAt": {
  19. "@id": "createdAt",
  20. "@type": "schema:DateTime"
  21. },
  22. "dateOfBirth": {
  23. "@id": "dateOfBirth",
  24. "@type": "schema:Date"
  25. }
  26. }
  27. }
  28. }
  29. ],
  30. "id": "https://example.com/credentials/CREDENTIAL_ID",
  31. "type": [
  32. "VerifiableCredential",
  33. "AccountV1"
  34. ],
  35. "holder": "did:HOLDER_ID",
  36. "credentialSubject": {
  37. "id": "did:HOLDER_ID",
  38. "username": "USER",
  39. "createdAt": "2020-11-11T11:11:11.111Z",
  40. "type": "Account"
  41. }
  42. }

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

Other CredentialFactory examples: