Schema validation for JavaScript

Check that your data has the right shape.

Describe what a value should look like: an API request, a config file, a form. schiva tells you what is wrong with it, field by field, in messages you can show as they are. It is the fastest validator we have measured, and it has no dependencies.

import { ClosedSchema, String, Integer, ArrayOf } from 'schiva';

// 1. Describe the data
const person = new ClosedSchema({
  id: String(),
  age: Integer({ min: 18 }),
  tags: ArrayOf({ type: String(), isMandatory: false }),
});

// 2. Compile it once
const validate = person.compile();

// 3. Validate values
validate({ id: 1, age: 10, extra: 1 });

validate() returns the errors ([] when the value is valid):

  • id must be a string
  • age must be at least 18
  • Unexpected key: extra

How it works

Three steps, the same with both ways of writing a schema. Try them with the examples of the playground.

  1. 1

    Describe your data

    Write a schema: in JavaScript, with String(), Integer()...; or in JSON Schema, the standard format that OpenAPI and many tools use.

  2. 2

    Compile it once

    When your program starts, schiva turns the schema into a JavaScript function written for it. It takes well under a millisecond.

  3. 3

    Validate every value

    Call the function with a value. It returns the list of errors, each with the field it is about, or true/false if that is all you need.

What it is for

Anywhere data comes from outside your code and you need to trust its shape.

API requests

Reject a bad request body with a 400 and one message per wrong field.

Config files

Stop at startup with a clear message instead of failing later in odd ways.

Forms and user input

Error objects give the path of each field, to show the message next to it.

OpenAPI and JSON Schema

Use the schemas you already have or share: every draft from 04 to 2020-12.

Why schiva

Fast, correct and clear, with nothing else to install.

Fast

Faster than ajv, the most used validator, in every benchmark we run: about twice as fast validating, and more than 40 times faster preparing a schema.

Correct

Passes all 4,950 tests of the official JSON Schema test suite, for the five versions of the standard. No other library we measured passes them all.

Clear errors

Each error names the field and the problem, lines[3].price must be at least 0, ready to log or return. Error objects are there too, for code.

Two ways to write schemas

A short JavaScript syntax whose TypeScript types are inferred for you, or JSON Schema for schemas you share with other tools and languages.

Safe by default

A typo in a schema, like maxLenght, stops the program when it compiles, instead of being silently ignored.

Nothing else to install

No dependencies. Runs on Node.js 18 and later and in browsers, even where generating code is not allowed, by writing validators to files when building.

In numbers

Measured against ajv and nine other JSON Schema validators, each in its own process. The details are on the benchmarks page.

4,950official JSON Schema tests, all passed (ajv: 4,836)
2.1×ajv's speed validating the JSON Schema test suite
2.2×ajv's speed validating realistic payloads, on average
44×ajv's speed preparing (compiling) a schema
See every benchmark, library by library →