ajv#KeywordCxt TypeScript Examples
The following examples show how to use
ajv#KeywordCxt.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: limit.ts From ajv-formats with MIT License | 5 votes |
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"],
}