Socket
Socket
Sign inDemoInstall

ajv

Package Overview
Dependencies
68
Maintainers
1
Versions
354
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    ajv

Another JSON schema Validator


Version published
Weekly downloads
104M
increased by1.27%
Maintainers
1
Created
Weekly downloads
 

Package description

What is ajv?

The ajv npm package is a fast JSON Schema validator that allows you to validate JSON data against a JSON schema. It supports the latest JSON Schema draft-07 and has several extensions. It can be used for data validation, data sanitization, and to ensure that JSON documents comply with a predefined schema.

What are ajv's main functionalities?

Validate data against a JSON Schema

This feature allows you to compile a JSON Schema and use it to validate JSON data. If the data does not conform to the schema, the errors can be logged or handled as needed.

{"const Ajv = require('ajv');
const ajv = new Ajv();
const schema = {
  "type": "object",
  "properties": {
    "foo": {"type": "integer"},
    "bar": {"type": "string"}
  },
  "required": ["foo"]
};
const validate = ajv.compile(schema);
const valid = validate({foo: 1, bar: 'abc'});
if (!valid) console.log(validate.errors);"}

Add custom keywords

Ajv allows you to define custom keywords for a JSON Schema, which can be used to create custom validation rules that are not defined in the JSON Schema specification.

{"const Ajv = require('ajv');
const ajv = new Ajv();
ajv.addKeyword('even', {
  validate: function(schema, data) {
    return data % 2 === 0;
  }
});
const schema = {"even": true};
const validate = ajv.compile(schema);
const valid = validate(2); // true
const invalid = validate(3); // false"}

Asynchronous validation

Ajv supports asynchronous schema compilation, which is useful when your JSON Schema depends on other schemas that need to be fetched remotely.

{"const Ajv = require('ajv');
const ajv = new Ajv({loadSchema: loadExternalSchema});
// Assume loadExternalSchema is a function that loads a schema asynchronously
ajv.compileAsync(schema).then(function(validate) {
  const valid = validate(data);
  if (!valid) console.log(validate.errors);
}).catch(function(err) {
  console.error('Failed to compile schema:', err);
});"}

Other packages similar to ajv

Readme

Source

ajv - Another JSON schema Validator

One of the fastest JSON schema validators. It uses doT templates to generate super-fast validating functions.

TODO

  • remote refs
  • custom formats (via options)
  • schema validation before compilation
  • bundle compiled templates (doT will be dev dependency)

Install

npm install ajv

Usage

var ajv = require('ajv')(options);
var validate = ajv.compile(schema);
var valid = validate(data);
if (!valid) console.log(validate.errors);

or

// ...
var valid = ajv.validate(schema, data);
// ...

ajv compiles schemas to functions and caches them in both cases (using stringified schema as a key - using json-stable-stringify), so that the next time the same schema is used (not necessarily the same object instance) it won't be compiled again.

Options

  • allErrors: check all rules collecting all errors. Default is to return after the first error.
  • verbose: include the reference to the validated data in the errors.
  • format: validate formats (true by default).
  • uniqueItems: validate uniqueItems (true by default).
  • unicode: calculate correct length of strings with unicode pairs (true by default - string lengths are calculated correctly but it is slower). Pass false to use string.length that is faster, but gives "incorrect" lengths of strings with unicode pairs - each unicode pair is counted as two characters.
  • beautify: format the generated function with js-beautify. npm install js-beautify to use this option.

Tests

git submodule update --init
npm test

Keywords

FAQs

Last updated on 06 Jun 2015

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc