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

Comparing version 0.3.1 to 0.3.2

2

package.json
{
"name": "ajv",
"version": "0.3.1",
"version": "0.3.2",
"description": "Another JSON schema Validator",

@@ -5,0 +5,0 @@ "main": "lib/ajv.js",

# ajv - Another JSON Schema Validator
One of the fastest JSON Schema validators. It uses [doT templates](https://github.com/olado/doT) to generate super-fast validating functions.
One of the fastest JSON Schema validators for node.js. It uses [doT templates](https://github.com/olado/doT) to generate super-fast validating functions.

@@ -11,3 +11,3 @@

- all validation keywords
- full support of remote refs (remote schemas have to be pre-loaded)
- full support of remote refs (remote schemas have to be added with `addSchema` or compiled to be available)
- correct string lengths for strings with unicode pairs (can be turned off)

@@ -19,10 +19,2 @@ - formats defined by JSON Schema draft 4 standard (can be turned off)

## TODO
- resolve missing remote refs when schemas are added
- custom formats (via options)
- schema validation before compilation
- bundle compiled templates (doT will be dev dependency)
## Install

@@ -38,3 +30,4 @@

```
var ajv = require('ajv')(options);
var Ajv = require('ajv');
var ajv = Ajv(); // options can be passed
var validate = ajv.compile(schema);

@@ -53,5 +46,52 @@ var valid = validate(data);

or
```
// ...
ajv.addSchema(schema, 'mySchema');
var valid = ajv.validate('mySchema', data);
// ...
```
ajv compiles schemas to functions and caches them in both cases (using stringified schema as a key - using [json-stable-stringify](https://github.com/substack/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.
## API
### Ajv(Object options) -> Object
Create ajv instance.
### .compile(Object schema) -> Function<Object data>
Generate validating function and cache compiled schema for future use.
### .validate(Object schema|String key|String ref, data) -> Boolean
Validate data using passed schema (it will be compiled and cached).
Instead of the schema you can use the key that was previously passed to `addSchema` or a previously resolved reference.
### .addSchema(Array schemas|Object schema [, String key]) -> Function<Object data>
Add and compile schema(s). It does the same as `.compile` with two differences:
- array of schemas can be passed (schemas should have ids), the second parameter will be ignored.
- key can be passed that can be used to reference the schema and will be used as the schema id if there is no id inside the schema. If the key is not passed, the schema id will be used as the key.
Once the schema added it and all the references inside it can be referenced in other schemas and used to validate data.
In the current version all the referenced schemas should be added before the schema that uses them is compiled, so the circular references are not supported.
### .getSchema(String key) -> Function<Object data>
Retrieve schema previously added with `addSchema`. Validating function has `schema` property with the reference to the original schema.
## Options

@@ -63,3 +103,3 @@

- _meta_: add [meta-schema](http://json-schema.org/documentation.html) so it can be used by other schemas (true by default).
- _uniqueItems_: validate `uniqueItems` (true by default).
- _uniqueItems_: validate `uniqueItems` keyword (true by default).
- _unicode_: calculate correct length of strings with unicode pairs (true by default). Pass `false` to use `.length` of strings that is faster, but gives "incorrect" lengths of strings with unicode pairs - each unicode pair is counted as two characters.

@@ -66,0 +106,0 @@ - _beautify_: format the generated function with [js-beautify](https://github.com/beautify-web/js-beautify) (the validating function is generated without line-breaks). `npm install js-beautify` to use this option. `true` or js-beautify options can be passed.

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