GithubHelp home page GithubHelp logo

skeeler-json-schema-draft-6's Introduction

skeeler-json-schema-draft-6

Build Status Coverage Status

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

skeeler-json-schema-draft-6's People

Contributors

cap32 avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.