ajv-formats
Advanced tools
Comparing version 0.3.4 to 0.4.0
@@ -0,0 +0,0 @@ import type { Format } from "ajv"; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -7,2 +7,3 @@ import { FormatMode, FormatName } from "./formats"; | ||
formats?: FormatName[]; | ||
keywords?: boolean; | ||
} | ||
@@ -9,0 +10,0 @@ export declare type FormatsPluginOptions = FormatName[] | FormatOptions; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const formats_1 = require("./formats"); | ||
const formatsPlugin = (ajv, opts = {}) => { | ||
if (Array.isArray(opts)) | ||
return addFormats(ajv, opts, formats_1.fullFormats); | ||
const limit_1 = __importDefault(require("./limit")); | ||
const formatsPlugin = (ajv, opts = { keywords: true }) => { | ||
if (Array.isArray(opts)) { | ||
addFormats(ajv, opts, formats_1.fullFormats); | ||
return ajv; | ||
} | ||
const formats = opts.mode === "fast" ? formats_1.fastFormats : formats_1.fullFormats; | ||
const list = opts.formats || formats_1.formatNames; | ||
return addFormats(ajv, list, formats); | ||
addFormats(ajv, list, formats); | ||
if (opts.keywords) | ||
limit_1.default(ajv); | ||
return ajv; | ||
}; | ||
@@ -21,3 +30,2 @@ formatsPlugin.get = (name, mode = "full") => { | ||
ajv.addFormat(f, fs[f]); | ||
return ajv; | ||
} | ||
@@ -24,0 +32,0 @@ exports.default = formatsPlugin; |
{ | ||
"name": "ajv-formats", | ||
"version": "0.3.4", | ||
"version": "0.4.0", | ||
"description": "Format validation for Ajv v7 (WIP)", | ||
@@ -38,3 +38,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"ajv": "^7.0.0-beta.0" | ||
"ajv": "^7.0.0-beta.2" | ||
}, | ||
@@ -47,3 +47,3 @@ "devDependencies": { | ||
"@typescript-eslint/parser": "^3.7.0", | ||
"ajv": "^7.0.0-beta.0", | ||
"ajv": "^7.0.0-beta.2", | ||
"eslint": "^7.5.0", | ||
@@ -50,0 +50,0 @@ "eslint-config-prettier": "^6.11.0", |
@@ -44,2 +44,29 @@ # ajv-formats | ||
## 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](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. | ||
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. | ||
```javascript | ||
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 | ||
@@ -61,3 +88,3 @@ | ||
2. Format validation mode (default is `"full"`) with optional list of format names: | ||
2. Format validation mode (default is `"full"`) with optional list of format names and `keywords` option to add additional format comparison keywords: | ||
@@ -71,3 +98,3 @@ ```javascript | ||
```javascript | ||
addFormats(ajv, {mode: "fast", formats: ["date", "time"]}) | ||
addFormats(ajv, {mode: "fast", formats: ["date", "time"], keywords: true}) | ||
``` | ||
@@ -74,0 +101,0 @@ |
@@ -9,2 +9,3 @@ import { | ||
} from "./formats" | ||
import formatLimit from "./limit" | ||
import type Ajv from "ajv" | ||
@@ -17,2 +18,3 @@ import type {Plugin, Format} from "ajv" | ||
formats?: FormatName[] | ||
keywords?: boolean | ||
} | ||
@@ -26,7 +28,15 @@ | ||
const formatsPlugin: FormatsPlugin = (ajv: Ajv, opts: FormatsPluginOptions = {}): Ajv => { | ||
if (Array.isArray(opts)) return addFormats(ajv, opts, fullFormats) | ||
const formatsPlugin: FormatsPlugin = ( | ||
ajv: Ajv, | ||
opts: FormatsPluginOptions = {keywords: true} | ||
): Ajv => { | ||
if (Array.isArray(opts)) { | ||
addFormats(ajv, opts, fullFormats) | ||
return ajv | ||
} | ||
const formats = opts.mode === "fast" ? fastFormats : fullFormats | ||
const list = opts.formats || formatNames | ||
return addFormats(ajv, list, formats) | ||
addFormats(ajv, list, formats) | ||
if (opts.keywords) formatLimit(ajv) | ||
return ajv | ||
} | ||
@@ -41,5 +51,4 @@ | ||
function addFormats(ajv: Ajv, list: FormatName[], fs: DefinedFormats): Ajv { | ||
function addFormats(ajv: Ajv, list: FormatName[], fs: DefinedFormats): void { | ||
for (const f of list) ajv.addFormat(f, fs[f]) | ||
return ajv | ||
} | ||
@@ -46,0 +55,0 @@ |
Sorry, the diff of this file is not supported yet
47944
15
613
112
Updatedajv@^7.0.0-beta.2