Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
J

json-gate

> 编程语言
Open source

A friendly, fast JSON schema validator

100 stars0 likes2 views
WebsiteGitHub

About

A friendly, fast JSON schema validator

json-gate

json-gate validates JSON objects against a JSON schema. In other words, it makes sure an object conforms to the type and structure that your code expects. For example, a server can use it to ensure input received from a client conforms to the API.

The JSON schema can also help with documentation and collaboration. Copy it to your API document and everybody should understand exactly what is expected.

json-gate likes you! It is...

  • Intuitive - API is super easy to use.
  • Produces human-friendly, detailed error messages - for both you and your customers.
  • Fast - Your CPU won't have time to blink.
  • Well documented, in case you don't feel like digging into the IETF specifications. Just keep reading, it's all here.
  • Conforms to standard - no relearning, no obligation, no surprises.
  • Both synchronous and asynchronous modes - your choice.

What's a JSON schema?

JSON Schema is a proposed Internet draft defining a JSON media type (application/schema+json) with the following goals:

  • Validation - You can use JSON Schema to validate your JSON data.
  • Documentation - You can extract documentation information from a JSON Schema, match this documentation to your data, and use that information for user interaction.
  • Hyperlinking - You can pair your JSON data with the defining JSON Schema to build hyperlinks into parts of that JSON data.

json-gate supports most of JSON Schema Draft 3, minus the hyperlinking and hyper schema parts.

Example

var createSchema = require('json-gate').createSchema;

var schema = createSchema({
	type: 'object',
	properties: {
		query: {
			type: 'string',
            minLength: 1,
            maxLength: 64,
			required: true
		},
		maxResults: {
			type: 'integer',
			maximum: 20,
			default: 10
		}
	},
    additionalProperties: false
});

try {
    schema.validate(input);
} catch(err) {
    return res.send(400, err); // 400 Bad Request
}

This schema is explained below in the Hello, schema section.

Installation

$ npm install json-gate

Usage

json-gate.createSchema(jsonSchema)

This function gets a JSON Schema definition and returns a new Schema object. It verifies that the schema is valid. If the latter is malformed an error will be thrown pinpointing the problem.

Schema.validate(jsonObject)

This function gets a JSON object and validates it against the schema. If the JSON object does not conform to the schema an error will be thrown (or returned, see Synchronous/Asynchronous below).

The function stops after encountering an error. It does not return multiple errors. The function does not return a value. Be aware that the input JSON object may be edited in-place if the default attribute is used.

Errors

The error messages are human-friendly and detailed. For example: "JSON object property 'user.password': length is 3 when it should be at least 6". Ready to be shrink-wrapped in a 400 Bad Request response and shipped to the client!

Equaly helpful error messages are produced in the case of a malformed schema, to assist you during development. For example: "Schema property 'num': 'exclusiveMaximum' attribute is a number when it should be a boolean".

Synchronous/Asynchronous

Schema.validate can be called in two ways, to suit your needs:

  • Synchronously - as in the example above, with one parameter. As already stated, it returns nothing if the object checks out. Otherwise it throws an error.
  • Asynchronously - by providing a 2nd parameter: a callback function. The callback function gets two arguments: error and result (the original JSON object, which may be modified).

It should be noted that the JSON object passed to the callback function is the same as the input JSON object. It is only passed for convenience. Any default values used will affect the original JSON object, even when calling asynchronously.

Hello, schema

A JSON schema is defined using a JavaScript object containing various attributes.

Let's start by analyzing the schema given in the example above. What does it say about the JSON object?

  • The JSON object should be an object (as opposed to an array).
  • It should have a property named query, which should be a string of 1 to 64 characters.
  • It may optionaly have a property named maxResults, which should be an integer with a maximum value of 20.
  • If maxResults is missing, it will be generated with a value of 10.
  • Additional properties (other than query and maxResults) are not allowed.

JSON Schema properties can be nested: objects and arrays include other attributes, which may themselves be objects and arrays. Notice that objects' properties are unordered, whereas array items are ordered.

See Attributes section below to learn about more possibilities.

Attributes

Below are all the supported attributes.

Terminology: in this section, instance refers to a JSON value (object or property) that the schema will be describing and validating.

type

Defines the expected instance type. It can take one of two forms:

  • Simple type - any of the following: 'string', 'number', 'integer', 'boolean', 'object', 'array', 'null' or 'any'.
  • Union type - an array of simple types and/or schemas. The instance type should be one of the types in the array.

Example - instance should be either a string or null:

type: ['string', 'null']

The default is 'any'.

disallow

Defines the disallowed instance type. This is the opposite of type. It can take one of two forms:

  • Simple type - any of the following: 'string', 'number', 'integer', 'boolean', 'object', 'array', 'null' or 'any'.
  • Union type - an array of simple types and/or schemas. The instance type should not be any of the types in the array. For example, if type is ['string', 'null'] then the instance may be neither a string nor null.

required

A boolean indicating whether an instance is mandatory (true) or optional (false).

Example with a mandatory property and an optional one:

{
    type: 'object',
    properties: {
        query: {
            type: 'string',
            required: true
        },
        safeSearch: {
            type: 'string',
            enum: ['off', 'moderate', 'strict']
        }
    }
}

The default is false (optional).

default

Defines the default value of the instance when the instance is undefined.

Example:

maxResults: {
    type: 'integer',
    default: 10
}

The JSON object is edited in-place. In other words, the default values are set to the original JSON object, not a returned copy.

enum

An array containing all possible values. The instance must equal one of the values.

Example:

enum: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']

properties

Applies only to instances of type 'object'.

Defines the properties of the instance object. The properties attribute is an object, matching properties to their valid schema.

Example:

{
    type: 'object',
    properties: {
        name: { type: 'string' },
        age: { type: 'integer' }
    }
}

The default is an empty object.

Note: Properties are considered unordered, the order of the instance properties may be in any order.

patternProperties

Applies only to instances of type 'object'.

This attribute is similar to the properties attribute, but the keys are regular expression patterns instead of property names. Any instance property whose name fits a pattern must be valid against the appropriate schema.

Example:

{
    type: 'object',
    patternProperties: {
        '^sz': { type: 'string' },
        '^n': { type: 'number' },
        '^b': { type: 'boolean' }
    }
}

Note that using this attribute may cause instances to be validated more than once:

  • If a property name is defined by properties and also matches a pattern in patternProperties.
  • If a property name matches more than one pattern in patternProperties.

additionalProperties

Applies only to instances of type 'object'.

Defines a schema for all properties that are not explicitly defined by properties and do not match any pattern in patternProperties. It can take one of two forms:

  • Schema - all the additional properties must be valid against the schema.
  • False - additional properties are not allowed.

Example:

{
    type: 'object',
    properties: {
        id: 'integer',
        name: 'string'
    },
    patternProperties: {
        '^_debug': { type: 'any' }
    },
    additionalProperties: false
}

The default is an empty schema, which allows any value for additional properties.

dependencies

Applies only to instances of type 'object'.

This attribute is an object, matching properties to their requirements. If the instance object has a property with the same name as a property in the dependencies attribute object, then the instance must comply with the requirement.

The requirement can take one of three forms:

  • Simple dependency - a string. The instance object must have a property with the same name as the requirement. Example: dependencies: { start: 'finish' }. Means that if an instance has a property 'start', it must also have a property 'finish'.
  • Simple dependency tuple - an array of strings. The instance object must have a property with the same name as each string in the requirement's array. Example: dependencies: { towel: [ 'toothbrush', 'soap', 'space suit' ]}. Means that if an instance has a property 'towel', it must also have properties 'toothbrush', 'soap' and 'space suit'.
  • Schema dependency - a schema. The instance object must be valid against the schema. Example: dependencies: { 'cuba-libre': { properties: { glass: { format: 'highball', required: true }}}} Means that if an instance has a property 'cuba-libre', it must also have a property 'glass' with a 'highball' format.

items

Applies only to instances of type 'array'.

Defines the items of the instance array. It can take one of two forms:

  • Schema - all the items in the array must be valid against the schema.
  • Tuple typing - an array of schemas. Each position in the instance array must conform to the schema in the corresponding position for this array. The instance array is not required to contain all defined items. Additional items are allowed, disallowed, or constrained by the additionalItems attribute.

additionalItems

Applies only to instances of type 'array', and only together with the tuple typing form of the items attribute.

additionalItems defines the behavior when there are more items in the instance array than in the items array. It can take one of two forms:

  • Schema - all the additional items must be valid against the schema.
  • False - additional items are not allowed.

Example - a string, an integer and the rest are booleans:

{
    type: 'array',
    items: [
        { type: 'string' },
        { type: 'integer' }
    ],
    additionalItems: { type: 'boolean' }
}

The default is an empty schema, which allows additional items of any type.

minItems

Applies only to instances of type 'array'.

Defines the minimum number of values in an array.

maxItems

Applies only to instances of type 'array'.

Defines the maximum number of values in an array.

uniqueItems

Applies only to instances of type 'array'.

A boolean that indicates whether all items in the array instance must be unique (contains no two identical values).

minLength

Applies only to instances of type 'string'.

Defines the minimum length of the string.

maxLength

Applies only to instances of type 'string'.

Defines the maximum length of the string.

pattern

Applies only to instances

Issues· 12 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言