npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

skeema

v0.3.1

Published

JSON schema utilities

Downloads

5

Readme

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:

| Attribute | Required | Type | Description | | ------------- | -------- | --------------- | ----------------------------------------------------------- | | $comment | No | string | Useful for leaving notes for maintainer of schema | | description | No | string | Long explanation about purpose of data described by schema | | examples | No | Array<any> | A place to provide examples that validate against schema | | title | No | string | Short 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:

| Attribute | Required | Type | Description | | ------------- | -------- | --------------- | ----------------------------------------------------------- | | $comment | No | string | Useful for leaving notes for maintainer of schema | | description | No | string | Long explanation about purpose of data described by schema | | examples | No | Array<any> | A place to provide examples that validate against schema | | title | No | string | Short 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:

| Attribute | Required | Type | Description | | ----------------- | -------- | --------------------------- | ----------------------------------------------------------- | | $comment | No | string | Useful for leaving notes for maintainer of schema | | additionalItems | No | boolean or Schema | Whether or not to allow additional items and their schema | | contains | No | Schema | Schema one or more items should adhere to | | description | No | string | Long explanation about purpose of data described by schema | | examples | No | Array<Array<any>> | A place to provide examples that validate against schema | | items | No | Schema or Array<Schema> | Schema items should adhere to | | maxItems | No | number | Maximum number of items allowed | | minItems | No | number | Minimum number of items allowed | | title | No | string | Short explanation about purpose of data described by schema | | type | No | "boolean" | This is added automatically but can still be included | | uniqueItems | No | boolean | Whether 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:

| Attribute | Required | Type | Description | | ------------- | -------- | ---------------- | ----------------------------------------------------------- | | $comment | No | string | Useful for leaving notes for maintainer of schema | | description | No | string | Long explanation about purpose of data described by schema | | examples | No | Array<boolean> | A place to provide examples that validate against schema | | title | No | string | Short explanation about purpose of data described by schema | | type | No | "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:

| Attribute | Required | Type | Description | | ------------- | -------- | --------------- | ----------------------------------------------------------- | | $comment | No | string | Useful for leaving notes for maintainer of schema | | description | No | string | Long explanation about purpose of data described by schema | | examples | No | Array<any> | A place to provide examples that validate against schema | | title | No | string | Short 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:

| Attribute | Required | Type | Description | | ------------------ | -------- | --------------- | ------------------------------------------------------------ | | $comment | No | string | Useful for leaving notes for maintainer of schema | | description | No | string | Long explanation about purpose of data described by schema | | examples | No | Array<number> | A place to provide examples that validate against schema | | exclusiveMaximum | No | boolean | Whether or not maximum is exclusive | | exclusiveMinimum | No | boolean | Whether or not minimum is exclusive | | maximum | No | number | The maximum value the integer should be (must be an integer) | | minimum | No | number | The minimum value the integer should be (must be an integer) | | multipleOf | No | number | What the number should e a multiple of (must be an integer) | | title | No | string | Short explanation about purpose of data described by schema | | type | No | "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:

| Attribute | Required | Type | Description | | ------------- | -------- | --------------- | ----------------------------------------------------------- | | $comment | No | string | Useful for leaving notes for maintainer of schema | | description | No | string | Long explanation about purpose of data described by schema | | examples | No | Array<null> | A place to provide examples that validate against schema | | title | No | string | Short explanation about purpose of data described by schema | | type | No | "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:

| Attribute | Required | Type | Description | | ------------- | -------- | --------------- | ----------------------------------------------------------- | | $comment | No | string | Useful for leaving notes for maintainer of schema | | description | No | string | Long explanation about purpose of data described by schema | | examples | No | Array<any> | A place to provide examples that validate against schema | | title | No | string | Short 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:

| Attribute | Required | Type | Description | | ------------------ | -------- | --------------- | ----------------------------------------------------------- | | $comment | No | string | Useful for leaving notes for maintainer of schema | | description | No | string | Long explanation about purpose of data described by schema | | examples | No | Array<number> | A place to provide examples that validate against schema | | exclusiveMaximum | No | boolean | Whether or not maximum is exclusive | | exclusiveMinimum | No | boolean | Whether or not minimum is exclusive | | maximum | No | number | The maximum value the number should be | | minimum | No | number | The minimum value the number should be | | multipleOf | No | number | What the number should e a multiple of | | title | No | string | Short explanation about purpose of data described by schema | | type | No | "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:

| Attribute | Required | Type | Description | | ---------------------- | -------- | --------------------- | -------------------------------------------------------------- | | $comment | No | string | Useful for leaving notes for maintainer of schema | | additionalProperties | No | boolean or Schema | Whether or not to allow additional properties and their schema | | description | No | string | Long explanation about purpose of data described by schema | | examples | No | Array<Object> | A place to provide examples that validate against schema | | maxProperties | No | number | The maximum number of properties object may contain | | minProperties | No | number | The minimum number of properties object may contain | | patternProperties | No | Object | Map of regex keys to schemas for validating properties against | | properties | Yes | Object | Map of property names to schemas | | propertyNames | No | Object | Schema to validate property names against | | required | No | Array<string> | List of required properties | | title | No | string | Short explanation about purpose of data described by schema | | type | No | "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:

| Attribute | Required | Type | Description | | ------------- | -------- | --------------- | ----------------------------------------------------------- | | $comment | No | string | Useful for leaving notes for maintainer of schema | | description | No | string | Long explanation about purpose of data described by schema | | examples | No | Array<any> | A place to provide examples that validate against schema | | title | No | string | Short 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:

| Attribute | Required | Type | Description | | ------------- | -------- | --------------- | ----------------------------------------------------------- | | $comment | No | string | Useful for leaving notes for maintainer of schema | | description | No | string | Long explanation about purpose of data described by schema | | enum | No | Array<string> | List of valid string values | | examples | No | Array<string> | A place to provide examples that validate against schema | | format | No | string | Format that the string should match | | maxLength | No | number | The maximum length the string should be | | minLength | No | number | The minimum length the string should be | | pattern | No | string | A regular expression pattern the string should match | | title | No | string | Short explanation about purpose of data described by schema | | type | No | "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