2.1.1 • Published 2 years ago

ajv-formats v2.1.1

Weekly downloads
67,612
License
MIT
Repository
github
Last release
2 years ago

ajv-formats

JSON Schema formats for Ajv

Build Status npm Gitter GitHub Sponsors

Usage

import Ajv from "ajv"
import addFormats from "ajv-formats"

const ajv = new Ajv()
addFormats(ajv)

Formats

The package defines these formats:

  • date: full-date according to RFC3339.
  • time: time with optional time-zone.
  • date-time: date-time from the same source (time-zone is mandatory).
  • duration: duration from RFC3339
  • uri: full URI.
  • uri-reference: URI reference, including full and relative URIs.
  • uri-template: URI template according to RFC6570
  • url (deprecated): URL record.
  • email: email address.
  • hostname: host name according to RFC1034.
  • ipv4: IP address v4.
  • ipv6: IP address v6.
  • regex: tests whether a string is a valid regular expression by passing it to RegExp constructor.
  • uuid: Universally Unique IDentifier according to RFC4122.
  • json-pointer: JSON-pointer according to RFC6901.
  • relative-json-pointer: relative JSON-pointer according to this draft.

See regular expressions used for format validation and the sources that were used in formats.ts.

Please note: JSON Schema draft-07 also defines formats iri, iri-reference, idn-hostname and idn-email for URLs, hostnames and emails with international characters. These formats are available in ajv-formats-draft2019 plugin.

Keywords to compare values: formatMaximum / formatMinimum and formatExclusiveMaximum / formatExclusiveMinimum

These keywords allow to define minimum/maximum constraints when the format keyword defines ordering (compare function in format definition).

Rhese keywords are added to ajv instance when ajv-formats is used without options or with option keywords: true.

These keywords apply only to strings. If the data is not a string, the validation succeeds.

The value of keywords formatMaximum/formatMinimum and formatExclusiveMaximum/formatExclusiveMinimum should be a string or \$data reference. This value is the maximum (minimum) allowed value for the data to be valid as determined by format keyword. If format keyword is not present schema compilation will throw exception.

When these keyword are added, they also add comparison functions to formats "date", "time" and "date-time". User-defined formats also can have comparison functions. See addFormat method.

require("ajv-formats")(ajv)

const schema = {
  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.
  1. 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", "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

@angular-devkit/core@pubngo-stack/core@spaceone/design-system@stackmate/enginebitev.io@douglasneuroinformatics/common@segment/actions-core@omnigraph/json-schema@architect-io/cli@arthswap/widgets@openos.com/uniswap@lackadaisical/test-case-parserts-iptc-ninjs-type@iyoungorg/ts-iptc-ninjs-typedisposabletestabcdisposabletestabcdidentity-validations@filmcalendar/agents-core@serverless-devs/dk-validator@ruleenginejs/ruleengine-schemadcc-rules-generator@clevyr/global-watcher-schemas@chijs/core@conductive/validateenergy-kit-library@labshare/loopback-restone-engineautoapprove.titlesfixturelibrarylevibestliblevibestlib2levilibtest19levilibtest24levilibtest25levilibtest26levilibtest27levilibtest28levilibtest29@sudoplatform/sudo-password-manager-schemamosi-ajv-customtitles-workflow-serverepcis2-test.jsbnqczz_delonyelon@orcli/orca-cli@scow/lib-configgotin-uiajfg@puayhiang/open-attestation-cli@graphshieldhq/telemetrycclibyarntest@laminar-markets/sdk@nosana-ci/schema-validatorhyperpass-sdkasure.integrations.smb.internal-apithing-it-servergodspeed-nodenocodb-dailyserverlessopen-attestation-cli-testsklif-ui-kitsklif-apiunleash-servermembership-system@everything-registry/sub-chunk-1112@gmahechas/erp-common@gmahechas/erp-common-js@314oner_npm/universal-components-library@douglasneuroinformatics/ajvmui-vue3ond@exotjs/exotwebeepay-apip149-tabletest-rhombuz-libsklif-uizservices-cli@squidcloud/common@mindgrep/core@secvisogram/csaf-validator-lib@amsterdam/bmi-dms-upload@adobe/reactor-sandbox@apimda/runtime@app-config/schema@asd14/blocks@alius/rpc@alkuip/forms@altearius/cs-gen@alliage/rest-api@accordproject/concerto-codegen@accordproject/concerto-tools@accounter-toolkit/modern-israeli-bank-scrapers@accounter/modern-poalim-scraper@awboost/cfn-resource-schemas@automatisch/backend@autotelic/fasdentify@atek-cloud/adb-api@atek-cloud/api-broker@atek-cloud/atek@atek-cloud/node-rpc
3.0.0-rc.0

2 years ago

2.1.1

3 years ago

2.1.0

3 years ago

1.6.1

3 years ago

2.0.2

3 years ago

2.0.1

3 years ago

1.6.0

3 years ago

2.0.0

3 years ago

2.0.0-beta.2

3 years ago

2.0.0-beta.1

3 years ago

2.0.0-beta.3

3 years ago

2.0.0-beta.0

3 years ago

1.5.1

3 years ago

1.5.0

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.0

3 years ago

0.4.0

3 years ago

0.3.4

3 years ago

0.3.3

4 years ago

0.3.2

4 years ago

0.3.1

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago

0.0.1

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.2.0

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago