You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
5.9 KiB

3 months ago
  1. # ajv-formats
  2. JSON Schema formats for Ajv
  3. [![Build Status](https://travis-ci.org/ajv-validator/ajv-formats.svg?branch=master)](https://travis-ci.org/ajv-validator/ajv-formats)
  4. [![npm](https://img.shields.io/npm/v/ajv-formats.svg)](https://www.npmjs.com/package/ajv-formats)
  5. [![Gitter](https://img.shields.io/gitter/room/ajv-validator/ajv.svg)](https://gitter.im/ajv-validator/ajv)
  6. [![GitHub Sponsors](https://img.shields.io/badge/$-sponsors-brightgreen)](https://github.com/sponsors/epoberezkin)
  7. ## Usage
  8. ```javascript
  9. // ESM/TypeScript import
  10. import Ajv from "ajv"
  11. import addFormats from "ajv-formats"
  12. // Node.js require:
  13. const Ajv = require("ajv")
  14. const addFormats = require("ajv-formats")
  15. const ajv = new Ajv()
  16. addFormats(ajv)
  17. ```
  18. ## Formats
  19. The package defines these formats:
  20. - _date_: full-date according to [RFC3339](http://tools.ietf.org/html/rfc3339#section-5.6).
  21. - _time_: time with optional time-zone.
  22. - _date-time_: date-time from the same source (time-zone is mandatory).
  23. - _duration_: duration from [RFC3339](https://tools.ietf.org/html/rfc3339#appendix-A)
  24. - _uri_: full URI.
  25. - _uri-reference_: URI reference, including full and relative URIs.
  26. - _uri-template_: URI template according to [RFC6570](https://tools.ietf.org/html/rfc6570)
  27. - _url_ (deprecated): [URL record](https://url.spec.whatwg.org/#concept-url).
  28. - _email_: email address.
  29. - _hostname_: host name according to [RFC1034](http://tools.ietf.org/html/rfc1034#section-3.5).
  30. - _ipv4_: IP address v4.
  31. - _ipv6_: IP address v6.
  32. - _regex_: tests whether a string is a valid regular expression by passing it to RegExp constructor.
  33. - _uuid_: Universally Unique IDentifier according to [RFC4122](http://tools.ietf.org/html/rfc4122).
  34. - _json-pointer_: JSON-pointer according to [RFC6901](https://tools.ietf.org/html/rfc6901).
  35. - _relative-json-pointer_: relative JSON-pointer according to [this draft](http://tools.ietf.org/html/draft-luff-relative-json-pointer-00).
  36. - _byte_: base64 encoded data according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
  37. - _int32_: signed 32 bits integer according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
  38. - _int64_: signed 64 bits according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
  39. - _float_: float according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
  40. - _double_: double according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
  41. - _password_: password string according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
  42. - _binary_: binary string according to the [openApi 3.0.0 specification](https://spec.openapis.org/oas/v3.0.0#data-types)
  43. See regular expressions used for format validation and the sources that were used in [formats.ts](https://github.com/ajv-validator/ajv-formats/blob/master/src/formats.ts).
  44. **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](https://github.com/luzlab/ajv-formats-draft2019) plugin.
  45. ## Keywords to compare values: `formatMaximum` / `formatMinimum` and `formatExclusiveMaximum` / `formatExclusiveMinimum`
  46. These keywords allow to define minimum/maximum constraints when the format keyword defines ordering (`compare` function in format definition).
  47. These keywords are added to ajv instance when ajv-formats is used without options or with option `keywords: true`.
  48. These keywords apply only to strings. If the data is not a string, the validation succeeds.
  49. The value of keywords `formatMaximum`/`formatMinimum` and `formatExclusiveMaximum`/`formatExclusiveMinimum` should be a string or [\$data reference](https://github.com/ajv-validator/ajv/blob/master/docs/validation.md#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.
  50. 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](https://github.com/ajv-validator/ajv/blob/master/docs/api.md#api-addformat) method.
  51. ```javascript
  52. require("ajv-formats")(ajv)
  53. const schema = {
  54. type: "string",
  55. format: "date",
  56. formatMinimum: "2016-02-06",
  57. formatExclusiveMaximum: "2016-12-27",
  58. }
  59. const validDataList = ["2016-02-06", "2016-12-26"]
  60. const invalidDataList = ["2016-02-05", "2016-12-27", "abc"]
  61. ```
  62. ## Options
  63. Options can be passed via the second parameter. Options value can be
  64. 1. The list of format names that will be added to ajv instance:
  65. ```javascript
  66. addFormats(ajv, ["date", "time"])
  67. ```
  68. **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:
  69. ```javascript
  70. const ajv = new Ajv((formats: {date: true, time: true})) // to ignore "date" and "time" formats in schemas.
  71. ```
  72. 2. Format validation mode (default is `"full"`) with optional list of format names and `keywords` option to add additional format comparison keywords:
  73. ```javascript
  74. addFormats(ajv, {mode: "fast"})
  75. ```
  76. or
  77. ```javascript
  78. addFormats(ajv, {mode: "fast", formats: ["date", "time"], keywords: true})
  79. ```
  80. 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.
  81. ## Tests
  82. ```bash
  83. npm install
  84. git submodule update --init
  85. npm test
  86. ```
  87. ## License
  88. [MIT](https://github.com/ajv-validator/ajv-formats/blob/master/LICENSE)