Socket
Socket
Sign inDemoInstall

ajv

Package Overview
Dependencies
68
Maintainers
1
Versions
351
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
98M
increased by5.39%
Maintainers
1
Install size
289 kB
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

TODO

  • refs (internal, remote with addSchema)
  • 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 result = validate(data);

or

var result = ajv.validate(schema, data);

Compiles and caches in both cases, so that the next time the same schema is used (not necessarily the same object instance) it won't be compiled again.

Options

  • allErrors: if true, jv will continue validating all rules collecting all errors (false by default)
  • verbose: include the reference to the validated data in the errors (false by default)
  • format: if false, the formats won't be validated (true by default)
  • unicode: if false, the lengths of strings with unicode pairs will be incorrect (true by default)

Keywords

FAQs

Last updated on 30 May 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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc