Giter Site home page Giter Site logo

schema's Introduction

Schema

npm npm npm

Typed, composable JSON schemas. This module consumes and produces JSON schemas with little to no processing.

Installation

npm i @tselect/schema [--save]

Disclaimer

This module takes the following positions:

  • additionalProperties for object schemas is false unless you specify a value
  • additionalItems for array schemas is false unless you specify a value

Motivation

Writing JSON schemas for large applications can be pretty time consuming. Having to type double quotes and "additionalProperties" for each new object by hand is no fun.

This module has been created with the intention of reducing the overhead of describing JSON by providing simple wrappers and utilities to manage common use cases with easiness.

It is also fully typed and will allow you to benefit from your favorite IDE's auto-completion, making typing a lot easier.

Usage

Building a schema

import { object, email } from '@tselect/schema';

const schema = object({
  foo: email()
});

Produces:

schema = {
  type: 'object',
  additionalProperties: false, // Default, see disclaimer
  properties: {
    foo: {
      type: 'string',
      format: 'email'
    }
  }
}

Manipulating a schema

import { object, email, integer, omitProperties } from '@tselect/schema';

const schema = object({
  foo: email(),
  bar: integer()
}, {
  required: ['foo', 'bar']
});

const modified = omitProperties(schema, ['bar']);

Produces:

modified = {
 type: 'object',
 required: ['foo'], // Only foo is kept as required
 additionalProperties: false, // Default, see disclaimer
 properties: {
   foo: { // No bar property anymore
     type: 'string',
     format: 'email'
   }
 }
}

Nullable types

import { string } from '@tselect/schema';

const schema = string({ nullable: true });

Produces:

schema = {
  type: ['string', 'null']
}

JSON schema compatibility

Any JSON schema you already wrote can be manipulated using this module.

import { object } from '@tselect/schema';

const schema = object({}, {
  type: 'object',
  required: ['foo'],
  additionalProperties: true,
  properties: {
    foo: {
      type: 'string',
      format: 'email'
    }
  }
}) 

Produces the exact same schema :

schema = {
  type: 'object',
  required: ['foo'],
  additionalProperties: true,
  properties: {
    foo: {
      type: 'string',
      format: 'email'
    }
  } 
};

schema's People

Contributors

sylvainestevez avatar

Stargazers

Ivan Dmitriev avatar bitlab avatar Misha avatar Noah Laux avatar yinz avatar

Watchers

James Cloos avatar  avatar

schema's Issues

๐Ÿ› Insufficient isJSONSchema check

The isJSONSchema function ( https://github.com/bluebirds-blue-jay/inversify-controller/blob/master/src/utils/is-json-schema.ts ) is insufficient and delivers a false return value when passing in at least anyOf or oneOf schema.

The check is used in at least 3 locations (listed end of issue) like this:

const jsonSchema = isJSONSchema(options) ? options : options.jsonSchema;

This results in an undefined jsonSchema being used when passing in said anyOf or oneOf schemas.

Very poor fix: (Turn the condition around... ๐Ÿ™ƒ )
const jsonSchema = isJSONSchema(options.jsonSchema) ? options.jsonSchema : options;

Proper fix:
Have the isJSONSchema return true for all types of Schemas (at least but not limited to anyOf and oneOf).

https://github.com/bluebirds-blue-jay/inversify-controller/blob/master/src/decorators/body.ts#L14
https://github.com/bluebirds-blue-jay/inversify-controller/blob/master/src/decorators/query.ts#L14
https://github.com/bluebirds-blue-jay/inversify-controller/blob/master/src/decorators/params.ts#L14

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.