$
This commit is contained in:
232
node_modules/ajv-formats/src/formats.ts
generated
vendored
Normal file
232
node_modules/ajv-formats/src/formats.ts
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
62
node_modules/ajv-formats/src/index.ts
generated
vendored
Normal file
62
node_modules/ajv-formats/src/index.ts
generated
vendored
Normal file
@@ -0,0 +1,62 @@
|
||||
import {
|
||||
DefinedFormats,
|
||||
FormatMode,
|
||||
FormatName,
|
||||
formatNames,
|
||||
fastFormats,
|
||||
fullFormats,
|
||||
} from "./formats"
|
||||
import formatLimit from "./limit"
|
||||
import type Ajv from "ajv"
|
||||
import type {Plugin, Format} from "ajv"
|
||||
import {_, Name} from "ajv/dist/compile/codegen"
|
||||
|
||||
export {FormatMode, FormatName} from "./formats"
|
||||
export {LimitFormatError} from "./limit"
|
||||
export interface FormatOptions {
|
||||
mode?: FormatMode
|
||||
formats?: FormatName[]
|
||||
keywords?: boolean
|
||||
}
|
||||
|
||||
export type FormatsPluginOptions = FormatName[] | FormatOptions
|
||||
|
||||
export interface FormatsPlugin extends Plugin<FormatsPluginOptions> {
|
||||
get: (format: FormatName, mode?: FormatMode) => Format
|
||||
}
|
||||
|
||||
const fullName = new Name("fullFormats")
|
||||
const fastName = new Name("fastFormats")
|
||||
|
||||
const formatsPlugin: FormatsPlugin = (
|
||||
ajv: Ajv,
|
||||
opts: FormatsPluginOptions = {keywords: true}
|
||||
): Ajv => {
|
||||
if (Array.isArray(opts)) {
|
||||
addFormats(ajv, opts, fullFormats, fullName)
|
||||
return ajv
|
||||
}
|
||||
const [formats, exportName] =
|
||||
opts.mode === "fast" ? [fastFormats, fastName] : [fullFormats, fullName]
|
||||
const list = opts.formats || formatNames
|
||||
addFormats(ajv, list, formats, exportName)
|
||||
if (opts.keywords) formatLimit(ajv)
|
||||
return ajv
|
||||
}
|
||||
|
||||
formatsPlugin.get = (name: FormatName, mode: FormatMode = "full"): Format => {
|
||||
const formats = mode === "fast" ? fastFormats : fullFormats
|
||||
const f = formats[name]
|
||||
if (!f) throw new Error(`Unknown format "${name}"`)
|
||||
return f
|
||||
}
|
||||
|
||||
function addFormats(ajv: Ajv, list: FormatName[], fs: DefinedFormats, exportName: Name): void {
|
||||
ajv.opts.code.formats ??= _`require("ajv-formats/dist/formats").${exportName}`
|
||||
for (const f of list) ajv.addFormat(f, fs[f])
|
||||
}
|
||||
|
||||
module.exports = exports = formatsPlugin
|
||||
Object.defineProperty(exports, "__esModule", {value: true})
|
||||
|
||||
export default formatsPlugin
|
99
node_modules/ajv-formats/src/limit.ts
generated
vendored
Normal file
99
node_modules/ajv-formats/src/limit.ts
generated
vendored
Normal file
@@ -0,0 +1,99 @@
|
||||
import type Ajv from "ajv"
|
||||
import type {
|
||||
Plugin,
|
||||
CodeKeywordDefinition,
|
||||
KeywordErrorDefinition,
|
||||
Code,
|
||||
Name,
|
||||
ErrorObject,
|
||||
} from "ajv"
|
||||
import type {AddedFormat} from "ajv/dist/types"
|
||||
import type {Rule} from "ajv/dist/compile/rules"
|
||||
import {KeywordCxt} from "ajv"
|
||||
import {_, str, or, getProperty, operators} from "ajv/dist/compile/codegen"
|
||||
|
||||
type Kwd = "formatMaximum" | "formatMinimum" | "formatExclusiveMaximum" | "formatExclusiveMinimum"
|
||||
|
||||
type Comparison = "<=" | ">=" | "<" | ">"
|
||||
|
||||
const ops = operators
|
||||
|
||||
const KWDs: {[K in Kwd]: {okStr: Comparison; ok: Code; fail: Code}} = {
|
||||
formatMaximum: {okStr: "<=", ok: ops.LTE, fail: ops.GT},
|
||||
formatMinimum: {okStr: ">=", ok: ops.GTE, fail: ops.LT},
|
||||
formatExclusiveMaximum: {okStr: "<", ok: ops.LT, fail: ops.GTE},
|
||||
formatExclusiveMinimum: {okStr: ">", ok: ops.GT, fail: ops.LTE},
|
||||
}
|
||||
|
||||
export type LimitFormatError = ErrorObject<Kwd, {limit: string; comparison: Comparison}>
|
||||
|
||||
const error: KeywordErrorDefinition = {
|
||||
message: ({keyword, schemaCode}) => str`should be ${KWDs[keyword as Kwd].okStr} ${schemaCode}`,
|
||||
params: ({keyword, schemaCode}) =>
|
||||
_`{comparison: ${KWDs[keyword as Kwd].okStr}, limit: ${schemaCode}}`,
|
||||
}
|
||||
|
||||
export const formatLimitDefinition: CodeKeywordDefinition = {
|
||||
keyword: Object.keys(KWDs),
|
||||
type: "string",
|
||||
schemaType: "string",
|
||||
$data: true,
|
||||
error,
|
||||
code(cxt) {
|
||||
const {gen, data, schemaCode, keyword, it} = cxt
|
||||
const {opts, self} = it
|
||||
if (!opts.validateFormats) return
|
||||
|
||||
const fCxt = new KeywordCxt(it, (self.RULES.all.format as Rule).definition, "format")
|
||||
if (fCxt.$data) validate$DataFormat()
|
||||
else validateFormat()
|
||||
|
||||
function validate$DataFormat(): void {
|
||||
const fmts = gen.scopeValue("formats", {
|
||||
ref: self.formats,
|
||||
code: opts.code.formats,
|
||||
})
|
||||
const fmt = gen.const("fmt", _`${fmts}[${fCxt.schemaCode}]`)
|
||||
cxt.fail$data(
|
||||
or(
|
||||
_`typeof ${fmt} != "object"`,
|
||||
_`${fmt} instanceof RegExp`,
|
||||
_`typeof ${fmt}.compare != "function"`,
|
||||
compareCode(fmt)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
function validateFormat(): void {
|
||||
const format = fCxt.schema as string
|
||||
const fmtDef: AddedFormat | undefined = self.formats[format]
|
||||
if (!fmtDef || fmtDef === true) return
|
||||
if (
|
||||
typeof fmtDef != "object" ||
|
||||
fmtDef instanceof RegExp ||
|
||||
typeof fmtDef.compare != "function"
|
||||
) {
|
||||
throw new Error(`"${keyword}": format "${format}" does not define "compare" function`)
|
||||
}
|
||||
const fmt = gen.scopeValue("formats", {
|
||||
key: format,
|
||||
ref: fmtDef,
|
||||
code: opts.code.formats ? _`${opts.code.formats}${getProperty(format)}` : undefined,
|
||||
})
|
||||
|
||||
cxt.fail$data(compareCode(fmt))
|
||||
}
|
||||
|
||||
function compareCode(fmt: Name): Code {
|
||||
return _`${fmt}.compare(${data}, ${schemaCode}) ${KWDs[keyword as Kwd].fail} 0`
|
||||
}
|
||||
},
|
||||
dependencies: ["format"],
|
||||
}
|
||||
|
||||
const formatLimitPlugin: Plugin<undefined> = (ajv: Ajv): Ajv => {
|
||||
ajv.addKeyword(formatLimitDefinition)
|
||||
return ajv
|
||||
}
|
||||
|
||||
export default formatLimitPlugin
|
Reference in New Issue
Block a user