Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

@punchcard/shape-jsonschema

0.3.0 • Public • Published

@punchcard/shape-jsonschema

This library maps types in the Punchcard Shape type-system to a JSON Schema.

Defining Types

First, create a Record using the Shape type-sytem:

class MyType extends Record({
  key: string
    .apply(MinLength(1)),
  count: integer
    .apply(Minimum(0)),
  rating: number,
  array: array(string),
  map: map(string),
  set: set(string)
}) {}

Map the record to a JSON schema document

const schema = require('@punchcard/shape-jsonschema').JsonSchema.of(MyType);

The type (and value) of the JSON schema is dynamically created and retains all constraint information:

const schema: {
  type: 'object',
  properties: {
    key: {
      type: 'string',
      maxLength: 1
    },
    count: {
      type: 'integer',
      minimum: 0,
      exclusiveMinimum: false
    },
    rating: {
      type: 'number'
    },
    array: {
      type: 'array',
      items: {
        type: 'string'
      }
    },
    set: {
      type: 'array',
      items: {
        type: 'string'
      },
      uniqueItems: true
    },
    map: {
      type: 'object',
      properties: {},
      allowAdditionalProperties: true,
      additionalProperties: {
        type: 'string'
      }
    },
  }
} = JsonSchema.of(MyType);

The only data that can not be retained is the required property since it is not possible to map union types to tuples. The value would have those keys that are not annotated with Optional.

Install

npm i @punchcard/shape-jsonschema

DownloadsWeekly Downloads

11

Version

0.3.0

License

Apache-2.0

Unpacked Size

44.1 kB

Total Files

16

Last publish

Collaborators

  • avatar