3.0.0 • Published 2 years ago

@bluejay/schema v3.0.0

Weekly downloads
133
License
MIT
Repository
github
Last release
2 years ago

Schema

npm npm npm

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

Requirements

  • node >= 8.6, tested with:
    • node@8.6.0
    • node@12.8.1
  • typescript >= 4.0, tested with:
    • typescript@4.0.2

Installation

npm i @bluejay/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 '@bluejay/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 '@bluejay/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 '@bluejay/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 '@bluejay/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'
    }
  } 
};

Documentation

See Github Pages.

3.0.0

2 years ago

3.0.0-beta.0

4 years ago

2.0.0-alpha.6

4 years ago

2.0.0-alpha.5

4 years ago

2.0.0-alpha.4

4 years ago

2.0.0-alpha.3

4 years ago

2.0.0-alpha.2

4 years ago

1.10.0

4 years ago

1.9.5

5 years ago

1.9.4

6 years ago

1.9.3

6 years ago

1.9.2

6 years ago

1.9.1

6 years ago

1.9.0

6 years ago

1.8.0

6 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.1

6 years ago

1.3.1

6 years ago

1.4.0

6 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.0

7 years ago