0.3.1 • Published 4 years ago

skeema v0.3.1

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

skeema NPM Coverage

JSON schema utilities.

Table of Contents

Installation

npm

npm install skeema

yarn

yarn add skeema

Documentation

First if you are unfamiliar with JSON schema you should read up on it first.

Schema helpers

The following methods are meant to help you build a schema, with built in Flow types and runtime validation. If you use Flow for type checking then Flow should catch errors for you, otherwise you'll still get runtime errors when these methods are executed.

Here is an example of how to import all of the following methods:

import {
  allOf,
  anyOf,
  array,
  boolean,
  constant,
  integer,
  nil,
  not,
  number,
  object,
  oneOf,
  string
} from 'skeema'

allOf(schemas, rest)

schemas is an optional argument which should be an array of schemas.

rest is an optional argument which when present should be an object with the following attributes:

AttributeRequiredTypeDescription
$commentNostringUseful for leaving notes for maintainer of schema
descriptionNostringLong explanation about purpose of data described by schema
examplesNoArray<any>A place to provide examples that validate against schema
titleNostringShort explanation about purpose of data described by schema

anyOf(schemas, rest)

schemas is an optional argument which should be an array of schemas.

rest is an optional argument which when present should be an object with the following attributes:

AttributeRequiredTypeDescription
$commentNostringUseful for leaving notes for maintainer of schema
descriptionNostringLong explanation about purpose of data described by schema
examplesNoArray<any>A place to provide examples that validate against schema
titleNostringShort explanation about purpose of data described by schema

array(schema)

schema is an optional argument which when present should be an object with the following attributes:

AttributeRequiredTypeDescription
$commentNostringUseful for leaving notes for maintainer of schema
additionalItemsNoboolean or SchemaWhether or not to allow additional items and their schema
containsNoSchemaSchema one or more items should adhere to
descriptionNostringLong explanation about purpose of data described by schema
examplesNoArray<Array<any>>A place to provide examples that validate against schema
itemsNoSchema or Array<Schema>Schema items should adhere to
maxItemsNonumberMaximum number of items allowed
minItemsNonumberMinimum number of items allowed
titleNostringShort explanation about purpose of data described by schema
typeNo"boolean"This is added automatically but can still be included
uniqueItemsNobooleanWhether or not to ensure each item is unique

boolean(schema)

schema is an optional argument which when present should be an object with the following attributes:

AttributeRequiredTypeDescription
$commentNostringUseful for leaving notes for maintainer of schema
descriptionNostringLong explanation about purpose of data described by schema
examplesNoArray<boolean>A place to provide examples that validate against schema
titleNostringShort explanation about purpose of data described by schema
typeNo"boolean"This is added automatically but can still be included

constant(value, rest)

value is a required argument which can be anything (a string, number, object with deeply nested attributes, etc).

rest is an optional argument which when present should be an object with the following attributes:

AttributeRequiredTypeDescription
$commentNostringUseful for leaving notes for maintainer of schema
descriptionNostringLong explanation about purpose of data described by schema
examplesNoArray<any>A place to provide examples that validate against schema
titleNostringShort explanation about purpose of data described by schema

integer(schema)

schema is an optional argument which when present should be an object with the following attributes:

AttributeRequiredTypeDescription
$commentNostringUseful for leaving notes for maintainer of schema
descriptionNostringLong explanation about purpose of data described by schema
examplesNoArray<number>A place to provide examples that validate against schema
exclusiveMaximumNobooleanWhether or not maximum is exclusive
exclusiveMinimumNobooleanWhether or not minimum is exclusive
maximumNonumberThe maximum value the integer should be (must be an integer)
minimumNonumberThe minimum value the integer should be (must be an integer)
multipleOfNonumberWhat the number should e a multiple of (must be an integer)
titleNostringShort explanation about purpose of data described by schema
typeNo"integer"This is added automatically but can still be included

nil(schema)

schema is an optional argument which when present should be an object with the following attributes:

AttributeRequiredTypeDescription
$commentNostringUseful for leaving notes for maintainer of schema
descriptionNostringLong explanation about purpose of data described by schema
examplesNoArray<null>A place to provide examples that validate against schema
titleNostringShort explanation about purpose of data described by schema
typeNo"null"This is added automatically but can still be included

not(schema, rest)

schemas is a required argument which should be a schema.

rest is an optional argument which when present should be an object with the following attributes:

AttributeRequiredTypeDescription
$commentNostringUseful for leaving notes for maintainer of schema
descriptionNostringLong explanation about purpose of data described by schema
examplesNoArray<any>A place to provide examples that validate against schema
titleNostringShort explanation about purpose of data described by schema

number(schema)

schema is an optional argument which when present should be an object with the following attributes:

AttributeRequiredTypeDescription
$commentNostringUseful for leaving notes for maintainer of schema
descriptionNostringLong explanation about purpose of data described by schema
examplesNoArray<number>A place to provide examples that validate against schema
exclusiveMaximumNobooleanWhether or not maximum is exclusive
exclusiveMinimumNobooleanWhether or not minimum is exclusive
maximumNonumberThe maximum value the number should be
minimumNonumberThe minimum value the number should be
multipleOfNonumberWhat the number should e a multiple of
titleNostringShort explanation about purpose of data described by schema
typeNo"number"This is added automatically but can still be included

object(schema)

schema is a required argument which when present should be an object with the following attributes:

AttributeRequiredTypeDescription
$commentNostringUseful for leaving notes for maintainer of schema
additionalPropertiesNoboolean or SchemaWhether or not to allow additional properties and their schema
descriptionNostringLong explanation about purpose of data described by schema
examplesNoArray<Object>A place to provide examples that validate against schema
maxPropertiesNonumberThe maximum number of properties object may contain
minPropertiesNonumberThe minimum number of properties object may contain
patternPropertiesNoObjectMap of regex keys to schemas for validating properties against
propertiesYesObjectMap of property names to schemas
propertyNamesNoObjectSchema to validate property names against
requiredNoArray<string>List of required properties
titleNostringShort explanation about purpose of data described by schema
typeNo"object"This is added automatically but can still be included

oneOf(schemas, rest)

schemas is an optional argument which should be an array of schemas.

rest is an optional argument which when present should be an object with the following attributes:

AttributeRequiredTypeDescription
$commentNostringUseful for leaving notes for maintainer of schema
descriptionNostringLong explanation about purpose of data described by schema
examplesNoArray<any>A place to provide examples that validate against schema
titleNostringShort explanation about purpose of data described by schema

string(schema)

schema is an optional argument which when present should be an object with the following attributes:

AttributeRequiredTypeDescription
$commentNostringUseful for leaving notes for maintainer of schema
descriptionNostringLong explanation about purpose of data described by schema
enumNoArray<string>List of valid string values
examplesNoArray<string>A place to provide examples that validate against schema
formatNostringFormat that the string should match
maxLengthNonumberThe maximum length the string should be
minLengthNonumberThe minimum length the string should be
patternNostringA regular expression pattern the string should match
titleNostringShort explanation about purpose of data described by schema
typeNo"string"This is added automatically but can still be included

Validation

In you'd like to validate a JSON schema, whether it was created via skeema's helper methods or is just purely a JSON object, you can use the validateSchema export. The rsult is an object containg both errors and warnings attributes, which are both arrays of objects that contain the attributes message and path.

Example

import {integer, object, string, validateSchema} from 'skeema'

const schema = object({
  properties: {
    bar: string(),
    foo: integer(),
  },
})

const result = validateSchema(schema)

result.errors.forEach(({message, path}) => {
  console.error(`${path}: ${message}`)
})

result.warnings.forEach(({message, path}) => {
  console.warn(`${path}: ${message}`)
})

Code of Conduct

Please see the code of conduct.

Contributing

Please see the contributing guide.

License

MIT