0.0.0 • Published 6 years ago

skeeler-json-schema-draft-6 v0.0.0

Weekly downloads
4
License
MIT
Repository
github
Last release
6 years ago

skeeler-json-schema-draft-6

Build Status Coverage Status

WIP JSON Schema Draft 6 plugin for Skeeler

Table of Contents

Simple Example

import Skeeler from 'skeeler';
import SkeelerJSONSchemaDraftV6 from 'skeeler-json-schema-draft-6';

const types = Skeeler.use('json', new SkeelerJSONSchemaDraftV6()).getTypes();

const mySkeeler = new Skeeler({
  foo: types.string.required,
  bar: types.number.exclusiveMinimum(0),
  baz: types.anyOf([
    types.object({
      qux: types.number,
      quux: types.boolean,
    }),
    types.enum(['corge', 'grault']),
    types.array(types.string),
  ]).required,
});

export default mySkeeler.export('json');
Equals to JSON Schema Draft 6
export default {
  type: 'object',
  properties: {
    foo: {
      type: 'string',
    },
    bar: {
      type: 'number',
      exclusiveMinimum: 0,
    },
    baz: {
      anyOf: [
        {
          type: 'object',
          properties: {
            qux: {
              type: 'number',
            },
            quux: {
              type: 'boolean',
            },
          },
        },
        {
          enum: ['corge', 'grault'],
        },
        {
          type: 'array',
          items: {
            type: 'string',
          },
        },
      ],
    },
  },
  required: ['foo', 'baz'],
};

Options

By default, all the schema definitions will be compiled to properties in JSON schema, and the root type is object.

rootProps option

If you want to pass some attributes outside properties, you may use rootProps option.

Example
const skeeler = new Skeeler({});
const options = { rootProps: { title: 'awesome' } };
skeeler.export('json', options);

/**
 * output
 *
 * { type: 'object', properties: {}, title: 'awesome' }
 */

strict option

If you want to writing in native JSON Schema way, you may use strict option.

Example
const skeeler = new Skeeler({
  properties: {
    foo: types.string,
    bar: types.number,
  },
  required: ['foo', 'bar'],
});
const options = { strict: true };
skeeler.export('json', options);

/**
 * output
 *
 * {
 *  properties: {
 *    foo: { type: 'string' },
 *    bar: { type: 'number' },
 *  },
 *  required: ['foo', 'bar'],
 * }
 */
Or
const skeeler = new Skeeler(
  types
    .properties({
      foo: types.string,
      bar: types.number,
    })
    .required(['foo', 'bar']),
);
const options = { strict: true };
skeeler.export('json', options);

Keywords

Please checkout keywords.js

This plugin is friendly to ajv and supports all ajv keywords

Syntactic Sugars

array()

types.array(types.string)

equals to

types.array.items(types.string)

object()

types.object({ foo: types.string })

equals to

types.object.properties({ foo: types.string })

required

{ foo: types.required, bar: types.required }

will be compiled to

{ properties: { foo: {}, bar: {} }, required: ['foo', 'bar'] }

NOTE: Only work when strict option is NOT true

dependencies

{ foo: types.dependencies(['bar']) }

will be compiled to

{ properties: { foo: {} }, dependencies: { foo: ['bar'] } }

NOTE: Only work when strict option is NOT true

func

types.func

equals to

types.instanceof('Function')

NOTE: instanceof is a custom keyword for ajv

Related projects

License

MIT