ajv-keywords
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
ajv | 8.12.0 | 214.96 kB | MIT | prod peer |
Ajv-keywords is a custom JSON-Schema keywords utility for Ajv validator. This utility comes handy to extend the functionality of Ajv, which is a fast JSON schema validator. Custom keywords provided by ajv-keywords package include typeof
, instanceof
, range
, regex
, among others, which offer extended validation mechanisms, hence providing users with more robust ways of validating JSON data.
Using Ajv-keywords in your project involves a few steps. First, you'll need to install it using npm with the following command: npm install ajv-keywords
. Next, in your JavaScript file, you'll require Ajv and ajv-keywords and create an instance of Ajv. You can then add all available keywords from ajv-keywords. Below is a basic example of how to validate a JSON using ajv-keywords:
const Ajv = require("ajv")
const ajv = new Ajv()
require("ajv-keywords")(ajv)
// Use extra keywords from ajv-keywords in your schema
const schema = { instanceof: 'RegExp' }
// Validate
ajv.validate(schema, /.*/) // returns true
ajv.validate(schema, '.*') // returns false
You can also add specific keywords instead of all available ones, for instance, adding the "instanceof" keyword would look like this:
require("ajv-keywords")(ajv, "instanceof")
Or multiple keywords like so:
require("ajv-keywords")(ajv, ["typeof", "instanceof"])
The documentation for ajv-keywords can be found on its GitHub repository README
file: https://github.com/epoberezkin/ajv-keywords. This document provides comprehensive information about all available keywords, installation instructions, usage examples and options, among other details. Following the examples and explanations provided in these docs will give you a deeper understanding of how to leverage ajv-keywords in JSON validation tasks.