📄 README.md

← 返回目录

ajv-formats

JSON Schema formats for Ajv

![Build Status](https://travis-ci.org/ajv-validator/ajv-formats) ![npm](https://www.npmjs.com/package/ajv-formats) ![Gitter](https://gitter.im/ajv-validator/ajv) ![GitHub Sponsors](https://github.com/sponsors/epoberezkin)

Usage

// ESM/TypeScript import
import Ajv from "ajv"
import addFormats from "ajv-formats"
// Node.js require:
const Ajv = require("ajv")
const addFormats = require("ajv-formats")

const ajv = new Ajv() addFormats(ajv)

Formats

The package defines these formats:

- _date_: full-date according to RFC3339.

type: "string", format: "date", formatMinimum: "2016-02-06", formatExclusiveMaximum: "2016-12-27", }

const validDataList = ["2016-02-06", "2016-12-26"]

const invalidDataList = ["2016-02-05", "2016-12-27", "abc"]

Options

Options can be passed via the second parameter. Options value can be

1. The list of format names that will be added to ajv instance:

addFormats(ajv, ["date", "time"])

Please note: when ajv encounters an undefined format it throws exception (unless ajv instance was configured with strict: false option). To allow specific undefined formats they have to be passed to ajv instance via formats option with true value:

const ajv = new Ajv((formats: {date: true, time: true})) // to ignore "date" and "time" formats in schemas.

2. Format validation mode (default is "full") with optional list of format names and keywords option to add additional format comparison keywords:

addFormats(ajv, {mode: "fast"})

or

addFormats(ajv, {mode: "fast", formats: ["date", "time"], keywords: true})

In "fast" mode the following formats are simplified: "date", "time", "date-time", "iso-time", "iso-date-time", "uri", "uri-reference", "email". For example, "date", "time" and "date-time" do not validate ranges in "fast" mode, only string structure, and other formats have simplified regular expressions.

Tests

npm install
git submodule update --init
npm test

License

MIT