ajv-formats
Advanced tools
Comparing version 0.5.0 to 0.6.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.formatNames = exports.fastFormats = exports.fullFormats = void 0; | ||
function fmtDef(validate, compare) { | ||
return { validate, compare }; | ||
} | ||
exports.fullFormats = { | ||
// date: http://tools.ietf.org/html/rfc3339#section-5.6 | ||
date, | ||
date: fmtDef(date, compareDate), | ||
// date-time: http://tools.ietf.org/html/rfc3339#section-5.6 | ||
time, | ||
"date-time": date_time, | ||
time: fmtDef(time, compareTime), | ||
"date-time": fmtDef(date_time, compareDateTime), | ||
// duration: https://tools.ietf.org/html/rfc3339#appendix-A | ||
@@ -36,5 +39,5 @@ duration: /^P(?!$)((\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?|(\d+W)?)$/, | ||
...exports.fullFormats, | ||
date: /^\d\d\d\d-[0-1]\d-[0-3]\d$/, | ||
time: /^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)?$/i, | ||
"date-time": /^\d\d\d\d-[0-1]\d-[0-3]\d[t\s](?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i, | ||
date: fmtDef(/^\d\d\d\d-[0-1]\d-[0-3]\d$/, compareDate), | ||
time: fmtDef(/^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)?$/i, compareTime), | ||
"date-time": fmtDef(/^\d\d\d\d-[0-1]\d-[0-3]\d[t\s](?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i, compareDateTime), | ||
// uri: https://github.com/mafintosh/is-my-json-valid/blob/master/formats.js | ||
@@ -45,3 +48,3 @@ uri: /^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/)?[^\s]*$/i, | ||
// http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address#answer-8829363 | ||
// http://www.w3.org/TR/html5/forms.html#valid-e-mail-address (search for 'willful violation') | ||
// http://www.w3.org/TR/html5/forms.html#valid-e-mail-address (search for 'wilful violation') | ||
email: /^[a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$/i, | ||
@@ -69,2 +72,11 @@ }; | ||
} | ||
function compareDate(d1, d2) { | ||
if (!(d1 && d2)) | ||
return undefined; | ||
if (d1 > d2) | ||
return 1; | ||
if (d1 < d2) | ||
return -1; | ||
return 0; | ||
} | ||
const TIME = /^(\d\d):(\d\d):(\d\d)(\.\d+)?(z|[+-]\d\d(?::?\d\d)?)?$/i; | ||
@@ -83,2 +95,17 @@ function time(str, withTimeZone) { | ||
} | ||
function compareTime(t1, t2) { | ||
if (!(t1 && t2)) | ||
return undefined; | ||
const a1 = TIME.exec(t1); | ||
const a2 = TIME.exec(t2); | ||
if (!(a1 && a2)) | ||
return undefined; | ||
t1 = a1[1] + a1[2] + a1[3] + (a1[4] || ""); | ||
t2 = a2[1] + a2[2] + a2[3] + (a2[4] || ""); | ||
if (t1 > t2) | ||
return 1; | ||
if (t1 < t2) | ||
return -1; | ||
return 0; | ||
} | ||
const DATE_TIME_SEPARATOR = /t|\s/i; | ||
@@ -90,2 +117,12 @@ function date_time(str) { | ||
} | ||
function compareDateTime(dt1, dt2) { | ||
if (!(dt1 && dt2)) | ||
return undefined; | ||
const [d1, t1] = dt1.split(DATE_TIME_SEPARATOR); | ||
const [d2, t2] = dt2.split(DATE_TIME_SEPARATOR); | ||
const res = compareDate(d1, d2); | ||
if (res === undefined) | ||
return undefined; | ||
return res || compareTime(t1, t2); | ||
} | ||
const NOT_URI_FRAGMENT = /\/|:/; | ||
@@ -92,0 +129,0 @@ const URI = /^(?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)?(?:#(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?$/i; |
"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 limit_1 = __importDefault(require("./limit")); | ||
const limit_1 = require("./limit"); | ||
const formatsPlugin = (ajv, opts = { keywords: true }) => { | ||
@@ -9,0 +6,0 @@ if (Array.isArray(opts)) { |
@@ -1,14 +0,4 @@ | ||
import type Ajv from "ajv"; | ||
import type { Plugin, CodeKeywordDefinition } from "ajv"; | ||
export declare const COMPARE_FORMATS: { | ||
date: typeof compareDate; | ||
time: typeof compareTime; | ||
"date-time": typeof compareDateTime; | ||
}; | ||
export declare const formatLimitDefinition: CodeKeywordDefinition; | ||
declare const formatLimitPlugin: Plugin<undefined>; | ||
export default formatLimitPlugin; | ||
export declare function extendFormats(ajv: Ajv): void; | ||
declare function compareDate(d1: string, d2: string): number | undefined; | ||
declare function compareTime(t1: string, t2: string): number | undefined; | ||
declare function compareDateTime(dt1: string, dt2: string): number | undefined; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.extendFormats = exports.formatLimitDefinition = exports.COMPARE_FORMATS = void 0; | ||
const context_1 = __importDefault(require("ajv/dist/compile/context")); | ||
exports.formatLimitDefinition = void 0; | ||
const context_1 = require("ajv/dist/compile/context"); | ||
const codegen_1 = require("ajv/dist/compile/codegen"); | ||
@@ -16,9 +13,2 @@ const ops = codegen_1.operators; | ||
}; | ||
const TIME = /^(\d\d):(\d\d):(\d\d)(\.\d+)?(z|[+-]\d\d:\d\d)?$/i; | ||
const DATE_TIME_SEPARATOR = /t|\s/i; | ||
exports.COMPARE_FORMATS = { | ||
date: compareDate, | ||
time: compareTime, | ||
"date-time": compareDateTime, | ||
}; | ||
const error = { | ||
@@ -77,54 +67,5 @@ message: ({ keyword, schemaCode }) => codegen_1.str `should be ${KWDs[keyword].okStr} ${schemaCode}`, | ||
ajv.addKeyword(exports.formatLimitDefinition); | ||
extendFormats(ajv); | ||
return ajv; | ||
}; | ||
exports.default = formatLimitPlugin; | ||
function extendFormats(ajv) { | ||
const { formats } = ajv; | ||
let name; | ||
for (name in exports.COMPARE_FORMATS) { | ||
let format = formats[name]; | ||
// the last condition is needed if it's RegExp from another window | ||
if (typeof format != "object" || format instanceof RegExp || !format.validate) { | ||
format = formats[name] = { validate: format }; | ||
} | ||
if (!format.compare) | ||
format.compare = exports.COMPARE_FORMATS[name]; | ||
} | ||
} | ||
exports.extendFormats = extendFormats; | ||
function compareDate(d1, d2) { | ||
if (!(d1 && d2)) | ||
return undefined; | ||
if (d1 > d2) | ||
return 1; | ||
if (d1 < d2) | ||
return -1; | ||
return 0; | ||
} | ||
function compareTime(t1, t2) { | ||
if (!(t1 && t2)) | ||
return undefined; | ||
const a1 = TIME.exec(t1); | ||
const a2 = TIME.exec(t2); | ||
if (!(a1 && a2)) | ||
return undefined; | ||
t1 = a1[1] + a1[2] + a1[3] + (a1[4] || ""); | ||
t2 = a2[1] + a2[2] + a2[3] + (a2[4] || ""); | ||
if (t1 > t2) | ||
return 1; | ||
if (t1 < t2) | ||
return -1; | ||
return 0; | ||
} | ||
function compareDateTime(dt1, dt2) { | ||
if (!(dt1 && dt2)) | ||
return undefined; | ||
const [d1, t1] = dt1.split(DATE_TIME_SEPARATOR); | ||
const [d2, t2] = dt2.split(DATE_TIME_SEPARATOR); | ||
const res = compareDate(d1, d2); | ||
if (res === undefined) | ||
return undefined; | ||
return res || compareTime(t1, t2); | ||
} | ||
//# sourceMappingURL=limit.js.map |
{ | ||
"name": "ajv-formats", | ||
"version": "0.5.0", | ||
"description": "Format validation for Ajv v7 (WIP)", | ||
"version": "0.6.0", | ||
"description": "Format validation for Ajv v7", | ||
"main": "dist/index.js", | ||
@@ -38,6 +38,6 @@ "types": "dist/index.d.ts", | ||
"dependencies": { | ||
"ajv": "^7.0.0-beta.2" | ||
"ajv": "^7.0.0-beta.7" | ||
}, | ||
"devDependencies": { | ||
"@ajv-validator/config": "^0.2.3", | ||
"@ajv-validator/config": "^0.3.0", | ||
"@types/jest": "^26.0.5", | ||
@@ -47,3 +47,2 @@ "@types/node": "^14.10.1", | ||
"@typescript-eslint/parser": "^3.7.0", | ||
"ajv": "^7.0.0-beta.2", | ||
"eslint": "^7.5.0", | ||
@@ -50,0 +49,0 @@ "eslint-config-prettier": "^6.11.0", |
@@ -1,2 +0,3 @@ | ||
import type {Format} from "ajv" | ||
import type {Format, FormatDefinition} from "ajv" | ||
import {FormatValidator, FormatCompare} from "ajv/dist/types" | ||
@@ -28,8 +29,15 @@ export type FormatMode = "fast" | "full" | ||
function fmtDef( | ||
validate: RegExp | FormatValidator<string>, | ||
compare: FormatCompare<string> | ||
): FormatDefinition<string> { | ||
return {validate, compare} | ||
} | ||
export const fullFormats: DefinedFormats = { | ||
// date: http://tools.ietf.org/html/rfc3339#section-5.6 | ||
date, | ||
date: fmtDef(date, compareDate), | ||
// date-time: http://tools.ietf.org/html/rfc3339#section-5.6 | ||
time, | ||
"date-time": date_time, | ||
time: fmtDef(time, compareTime), | ||
"date-time": fmtDef(date_time, compareDateTime), | ||
// duration: https://tools.ietf.org/html/rfc3339#appendix-A | ||
@@ -62,5 +70,11 @@ duration: /^P(?!$)((\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?|(\d+W)?)$/, | ||
...fullFormats, | ||
date: /^\d\d\d\d-[0-1]\d-[0-3]\d$/, | ||
time: /^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)?$/i, | ||
"date-time": /^\d\d\d\d-[0-1]\d-[0-3]\d[t\s](?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i, | ||
date: fmtDef(/^\d\d\d\d-[0-1]\d-[0-3]\d$/, compareDate), | ||
time: fmtDef( | ||
/^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)?$/i, | ||
compareTime | ||
), | ||
"date-time": fmtDef( | ||
/^\d\d\d\d-[0-1]\d-[0-3]\d[t\s](?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i, | ||
compareDateTime | ||
), | ||
// uri: https://github.com/mafintosh/is-my-json-valid/blob/master/formats.js | ||
@@ -71,3 +85,3 @@ uri: /^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/)?[^\s]*$/i, | ||
// http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address#answer-8829363 | ||
// http://www.w3.org/TR/html5/forms.html#valid-e-mail-address (search for 'willful violation') | ||
// http://www.w3.org/TR/html5/forms.html#valid-e-mail-address (search for 'wilful violation') | ||
email: /^[a-z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?)*$/i, | ||
@@ -101,2 +115,9 @@ } | ||
function compareDate(d1: string, d2: string): number | undefined { | ||
if (!(d1 && d2)) return undefined | ||
if (d1 > d2) return 1 | ||
if (d1 < d2) return -1 | ||
return 0 | ||
} | ||
const TIME = /^(\d\d):(\d\d):(\d\d)(\.\d+)?(z|[+-]\d\d(?::?\d\d)?)?$/i | ||
@@ -119,2 +140,14 @@ | ||
function compareTime(t1: string, t2: string): number | undefined { | ||
if (!(t1 && t2)) return undefined | ||
const a1 = TIME.exec(t1) | ||
const a2 = TIME.exec(t2) | ||
if (!(a1 && a2)) return undefined | ||
t1 = a1[1] + a1[2] + a1[3] + (a1[4] || "") | ||
t2 = a2[1] + a2[2] + a2[3] + (a2[4] || "") | ||
if (t1 > t2) return 1 | ||
if (t1 < t2) return -1 | ||
return 0 | ||
} | ||
const DATE_TIME_SEPARATOR = /t|\s/i | ||
@@ -127,2 +160,11 @@ function date_time(str: string): boolean { | ||
function compareDateTime(dt1: string, dt2: string): number | undefined { | ||
if (!(dt1 && dt2)) return undefined | ||
const [d1, t1] = dt1.split(DATE_TIME_SEPARATOR) | ||
const [d2, t2] = dt2.split(DATE_TIME_SEPARATOR) | ||
const res = compareDate(d1, d2) | ||
if (res === undefined) return undefined | ||
return res || compareTime(t1, t2) | ||
} | ||
const NOT_URI_FRAGMENT = /\/|:/ | ||
@@ -129,0 +171,0 @@ const URI = /^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:]|%[0-9a-f]{2})*@)?(?:\[(?:(?:(?:(?:[0-9a-f]{1,4}:){6}|::(?:[0-9a-f]{1,4}:){5}|(?:[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){4}|(?:(?:[0-9a-f]{1,4}:){0,1}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){3}|(?:(?:[0-9a-f]{1,4}:){0,2}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){2}|(?:(?:[0-9a-f]{1,4}:){0,3}[0-9a-f]{1,4})?::[0-9a-f]{1,4}:|(?:(?:[0-9a-f]{1,4}:){0,4}[0-9a-f]{1,4})?::)(?:[0-9a-f]{1,4}:[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?))|(?:(?:[0-9a-f]{1,4}:){0,5}[0-9a-f]{1,4})?::[0-9a-f]{1,4}|(?:(?:[0-9a-f]{1,4}:){0,6}[0-9a-f]{1,4})?::)|[Vv][0-9a-f]+\.[a-z0-9\-._~!$&'()*+,;=:]+)\]|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)|(?:[a-z0-9\-._~!$&'()*+,;=]|%[0-9a-f]{2})*)(?::\d*)?(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*|\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)?|(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)(?:\?(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?(?:#(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?$/i |
import type Ajv from "ajv" | ||
import type {Plugin, CodeKeywordDefinition, KeywordErrorDefinition, Code, Name} from "ajv" | ||
import type {AddedFormat, FormatValidator} from "ajv/dist/types" | ||
import type {AddedFormat} from "ajv/dist/types" | ||
import type {Rule} from "ajv/dist/compile/rules" | ||
@@ -21,11 +21,2 @@ import KeywordCxt from "ajv/dist/compile/context" | ||
const TIME = /^(\d\d):(\d\d):(\d\d)(\.\d+)?(z|[+-]\d\d:\d\d)?$/i | ||
const DATE_TIME_SEPARATOR = /t|\s/i | ||
export const COMPARE_FORMATS = { | ||
date: compareDate, | ||
time: compareTime, | ||
"date-time": compareDateTime, | ||
} | ||
const error: KeywordErrorDefinition = { | ||
@@ -97,3 +88,2 @@ message: ({keyword, schemaCode}) => str`should be ${KWDs[keyword as Kwd].okStr} ${schemaCode}`, | ||
ajv.addKeyword(formatLimitDefinition) | ||
extendFormats(ajv) | ||
return ajv | ||
@@ -103,42 +93,1 @@ } | ||
export default formatLimitPlugin | ||
export function extendFormats(ajv: Ajv): void { | ||
const {formats} = ajv | ||
let name: keyof typeof COMPARE_FORMATS | ||
for (name in COMPARE_FORMATS) { | ||
let format = formats[name] | ||
// the last condition is needed if it's RegExp from another window | ||
if (typeof format != "object" || format instanceof RegExp || !format.validate) { | ||
format = formats[name] = {validate: format as RegExp | FormatValidator<string>} | ||
} | ||
if (!format.compare) format.compare = COMPARE_FORMATS[name] | ||
} | ||
} | ||
function compareDate(d1: string, d2: string): number | undefined { | ||
if (!(d1 && d2)) return undefined | ||
if (d1 > d2) return 1 | ||
if (d1 < d2) return -1 | ||
return 0 | ||
} | ||
function compareTime(t1: string, t2: string): number | undefined { | ||
if (!(t1 && t2)) return undefined | ||
const a1 = TIME.exec(t1) | ||
const a2 = TIME.exec(t2) | ||
if (!(a1 && a2)) return undefined | ||
t1 = a1[1] + a1[2] + a1[3] + (a1[4] || "") | ||
t2 = a2[1] + a2[2] + a2[3] + (a2[4] || "") | ||
if (t1 > t2) return 1 | ||
if (t1 < t2) return -1 | ||
return 0 | ||
} | ||
function compareDateTime(dt1: string, dt2: string): number | undefined { | ||
if (!(dt1 && dt2)) return undefined | ||
const [d1, t1] = dt1.split(DATE_TIME_SEPARATOR) | ||
const [d2, t2] = dt2.split(DATE_TIME_SEPARATOR) | ||
const res = compareDate(d1, d2) | ||
if (res === undefined) return undefined | ||
return res || compareTime(t1, t2) | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14
46280
576
Updatedajv@^7.0.0-beta.7