Socket
Socket
Sign inDemoInstall

ajv-formats

Package Overview
Dependencies
Maintainers
2
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ajv-formats - npm Package Compare versions

Comparing version 1.4.1 to 1.5.0

dist/formats.d.ts

73

package.json
{
"name": "ajv-formats",
"version": "1.4.1",
"description": "Plugin for AJV that adds support for some of draft2019 formats.",
"main": "index.js",
"version": "1.5.0",
"description": "Format validation for Ajv v7",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"src/",
"dist/"
],
"scripts": {
"start": "node index.js",
"test": "mocha index.test.js",
"format": "prettier --write '**/*.{js,md}'"
"build": "tsc",
"prettier:write": "prettier --write \"./**/*.{md,json,yaml,js,ts}\"",
"prettier:check": "prettier --list-different \"./**/*.{md,json,yaml,js,ts}\"",
"eslint": "eslint --ext .ts ./src/**/*",
"test-spec": "jest",
"test-cov": "jest --coverage",
"test": "npm run prettier:check && npm run build && npm run eslint && npm run test-cov",
"ci-test": "npm run test"
},
"author": "Carlo Quinonez <carlo@machina.bio>",
"repository": {
"type": "git",
"url": "git+https://github.com/ajv-validator/ajv-formats.git"
},
"keywords": [
"Ajv",
"JSON-Schema",
"format",
"validation"
],
"author": "Evgeny Poberezkin",
"license": "MIT",
"repository": "https://github.com/luzlab/ajv-formats.git",
"bugs": {
"url": "https://github.com/ajv-validator/ajv-formats/issues"
},
"homepage": "https://github.com/ajv-validator/ajv-formats#readme",
"dependencies": {
"isemail": "^3.2.0",
"punycode": "^2.1.1",
"schemes": "^1.1.1",
"uri-js": "^4.2.2"
"ajv": "^7.0.0-beta.7"
},
"peerDependencies": {
"ajv": "*"
"devDependencies": {
"@ajv-validator/config": "^0.3.0",
"@types/jest": "^26.0.5",
"@types/node": "^14.10.1",
"@typescript-eslint/eslint-plugin": "^3.7.0",
"@typescript-eslint/parser": "^3.7.0",
"eslint": "^7.5.0",
"eslint-config-prettier": "^6.11.0",
"husky": "^4.2.5",
"jest": "^26.1.0",
"json-schema-test": "^2.0.0",
"lint-staged": "^10.2.11",
"prettier": "^2.0.5",
"ts-jest": "^26.1.3",
"typescript": "^4.0.0"
},
"devDependencies": {
"ajv": "^6.10.2",
"mocha": "^7.0.0",
"prettier": "^1.19.1"
"prettier": "@ajv-validator/config/prettierrc.json",
"husky": {
"hooks": {
"pre-commit": "lint-staged && npm test"
}
},
"lint-staged": {
"*.{md,json,yaml,js,ts}": "prettier --write"
}
}
# ajv-formats
Plugin for AJV that adds support for additional international formats and
formats added in draft2019.
JSON Schema formats for Ajv
Currently, `iri`, `iri-reference`, `idn-email`, `idn-hostname`, and `duration`
formats are supported. `duration` was added in draft 2019.
[![Build Status](https://travis-ci.org/ajv-validator/ajv-formats.svg?branch=master)](https://travis-ci.org/ajv-validator/ajv-formats)
[![npm](https://img.shields.io/npm/v/ajv-formats.svg)](https://www.npmjs.com/package/ajv-formats)
[![Gitter](https://img.shields.io/gitter/room/ajv-validator/ajv.svg)](https://gitter.im/ajv-validator/ajv)
[![GitHub Sponsors](https://img.shields.io/badge/$-sponsors-brightgreen)](https://github.com/sponsors/epoberezkin)
## Installation
## Usage
```sh
npm install ajv-formats
```javascript
import Ajv from "ajv"
import addFormats from "ajv-formats"
const ajv = new Ajv()
addFormats(ajv)
```
## Usage
## Formats
The default export is an `apply` function that patches an existing instance of
`ajv`.
The package defines these formats:
```js
const Ajv = require('ajv');
const apply = require('ajv-formats');
const ajv = new Ajv();
apply(ajv); // returns ajv instance, allowing chaining
- _date_: full-date according to [RFC3339](http://tools.ietf.org/html/rfc3339#section-5.6).
- _time_: time with optional time-zone.
- _date-time_: date-time from the same source (time-zone is mandatory).
- _duration_: duration from [RFC3339](https://tools.ietf.org/html/rfc3339#appendix-A)
- _uri_: full URI.
- _uri-reference_: URI reference, including full and relative URIs.
- _uri-template_: URI template according to [RFC6570](https://tools.ietf.org/html/rfc6570)
- _url_ (deprecated): [URL record](https://url.spec.whatwg.org/#concept-url).
- _email_: email address.
- _hostname_: host name according to [RFC1034](http://tools.ietf.org/html/rfc1034#section-3.5).
- _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](http://tools.ietf.org/html/rfc4122).
- _json-pointer_: JSON-pointer according to [RFC6901](https://tools.ietf.org/html/rfc6901).
- _relative-json-pointer_: relative JSON-pointer according to [this draft](http://tools.ietf.org/html/draft-luff-relative-json-pointer-00).
let schema = {
type: 'string',
format: 'idn-email',
};
ajv.validate(schema, 'квіточка@пошта.укр'); // returns true
```
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).
The `apply` function also accepts a second optional parameter to specify which
formats to add to the `ajv` instance.
**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.
```js
const Ajv = require('ajv');
const apply = require('ajv-formats');
const ajv = new Ajv();
## Keywords to compare values: `formatMaximum` / `formatMinimum` and `formatExclusiveMaximum` / `formatExclusiveMinimum`
// Install only the idn-email and iri formats
apply(ajv, { formats: ['idn-email', 'iri'] });
```
These keywords allow to define minimum/maximum constraints when the format keyword defines ordering (`compare` function in format definition).
The module also provides an alternate entrypoint `ajv-formats/formats` that
works with the `ajv` constructor to add the formats to new instances.
Rhese keywords are added to ajv instance when ajv-formats is used without options or with option `keywords: true`.
```js
const Ajv = require('ajv');
const formats = require('ajv-formats/formats');
const ajv = new Ajv({ formats });
These keywords apply only to strings. If the data is not a string, the validation succeeds.
let schema = {
type: 'string',
format: 'idn-email',
};
ajv.validate(schema, 'квіточка@пошта.укр'); // returns true
```
The value of keywords `formatMaximum`/`formatMinimum` and `formatExclusiveMaximum`/`formatExclusiveMinimum` should be a string or [\$data reference](https://github.com/ajv-validator/ajv/blob/v7-beta/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.
Using the `ajv-formats/formats` entry point also allows cherry picking formats.
Note the approach below only works for formats that don't contain a hypen `-` in
the name. This approach may yield smaller packed bundles since it allows
tree-shaking to remove unwanted validators and related dependencies.
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/v7-beta/docs/api.md#api-addformat) method.
```js
const Ajv = require('ajv');
const { duration, iri } = require('ajv-formats/formats');
const ajv = new Ajv({ formats: { duration, iri } });
```
```javascript
require("ajv-formats")(ajv)
## International formats
const schema = {
type: "string",
format: "date",
formatMinimum: "2016-02-06",
formatExclusiveMaximum: "2016-12-27",
}
The library also provides an `idn` export to load only the international formats
(ie. `iri`, `iri-reference`, `idn-hostname` and `idn-email`).
const validDataList = ["2016-02-06", "2016-12-26"]
```js
const Ajv = require('ajv');
const formats = require('ajv-formats/idn');
const ajv = new Ajv({ formats });
const invalidDataList = ["2016-02-05", "2016-12-27", "abc"]
```
## Formats
## Options
### iri
Options can be passed via the second parameter. Options value can be
The string is parsed with 'uri-js' and the scheme is checked against the list of
known IANA schemes. If it's a 'mailto' schemes, all of the `to:` addresses are
validated, otherwise we check there IRI includes a path and is an absolute
reference.
1. The list of format names that will be added to ajv instance:
### iri-reference
```javascript
addFormats(ajv, ["date", "time"])
```
All valid IRIs are valid. Fragments must have a valid path and of type
"relative", "same-document" or "uri". If there is a scheme, it must be valid.
**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:
Validating a IRI references is challenging since the syntax is so permissive.
Basically, any URL-safe string is a valid IRI syntactically. I struggled to find
[negative test cases](https://github.com/luzlab/ajv-formats/blob/master/index.test.js#L240)
when writing the unit tests for IRI-references. Consider:
```javascript
const ajv = new Ajv((formats: {date: true, time: true})) // to ignore "date" and "time" formats in schemas.
```
- `google.com` is NOT a valid IRI because it does not include a scheme.
- `file.txt` is a valid IRI-reference
- `/this:that` is a valid IRI-reference
- `this:that` is a NOT a valid IRI-reference
2. Format validation mode (default is `"full"`) with optional list of format names and `keywords` option to add additional format comparison keywords:
### idn-email
```javascript
addFormats(ajv, {mode: "fast"})
```
[`isemail`](https://www.npmjs.com/package/isemail) is used to check the validity
of the email.
or
### idn-hostname
```javascript
addFormats(ajv, {mode: "fast", formats: ["date", "time"], keywords: true})
```
The hostname is converted to ascii with punycode and checked for a valid tld.
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.
### duration
## Tests
The string is checked against a regex.
```bash
npm install
git submodule update --init
npm test
```
## License
[MIT](https://github.com/ajv-validator/ajv-formats/blob/master/LICENSE)

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc