Last commit july 5th

This commit is contained in:
2024-07-05 13:46:23 +02:00
parent dad0d86e8c
commit b0e4dfbb76
24982 changed files with 2621219 additions and 413 deletions

View File

@@ -0,0 +1,7 @@
import type {Vocabulary} from "../../types"
import unevaluatedProperties from "./unevaluatedProperties"
import unevaluatedItems from "./unevaluatedItems"
const unevaluated: Vocabulary = [unevaluatedProperties, unevaluatedItems]
export default unevaluated

View File

@@ -0,0 +1,47 @@
import type {
CodeKeywordDefinition,
ErrorObject,
KeywordErrorDefinition,
AnySchema,
} from "../../types"
import type {KeywordCxt} from "../../compile/validate"
import {_, str, not, Name} from "../../compile/codegen"
import {alwaysValidSchema, Type} from "../../compile/util"
export type UnevaluatedItemsError = ErrorObject<"unevaluatedItems", {limit: number}, AnySchema>
const error: KeywordErrorDefinition = {
message: ({params: {len}}) => str`must NOT have more than ${len} items`,
params: ({params: {len}}) => _`{limit: ${len}}`,
}
const def: CodeKeywordDefinition = {
keyword: "unevaluatedItems",
type: "array",
schemaType: ["boolean", "object"],
error,
code(cxt: KeywordCxt) {
const {gen, schema, data, it} = cxt
const items = it.items || 0
if (items === true) return
const len = gen.const("len", _`${data}.length`)
if (schema === false) {
cxt.setParams({len: items})
cxt.fail(_`${len} > ${items}`)
} else if (typeof schema == "object" && !alwaysValidSchema(it, schema)) {
const valid = gen.var("valid", _`${len} <= ${items}`)
gen.if(not(valid), () => validateItems(valid, items))
cxt.ok(valid)
}
it.items = true
function validateItems(valid: Name, from: Name | number): void {
gen.forRange("i", from, len, (i) => {
cxt.subschema({keyword: "unevaluatedItems", dataProp: i, dataPropType: Type.Num}, valid)
if (!it.allErrors) gen.if(not(valid), () => gen.break())
})
}
},
}
export default def

View File

@@ -0,0 +1,85 @@
import type {
CodeKeywordDefinition,
KeywordErrorDefinition,
ErrorObject,
AnySchema,
} from "../../types"
import {_, not, and, Name, Code} from "../../compile/codegen"
import {alwaysValidSchema, Type} from "../../compile/util"
import N from "../../compile/names"
export type UnevaluatedPropertiesError = ErrorObject<
"unevaluatedProperties",
{unevaluatedProperty: string},
AnySchema
>
const error: KeywordErrorDefinition = {
message: "must NOT have unevaluated properties",
params: ({params}) => _`{unevaluatedProperty: ${params.unevaluatedProperty}}`,
}
const def: CodeKeywordDefinition = {
keyword: "unevaluatedProperties",
type: "object",
schemaType: ["boolean", "object"],
trackErrors: true,
error,
code(cxt) {
const {gen, schema, data, errsCount, it} = cxt
/* istanbul ignore if */
if (!errsCount) throw new Error("ajv implementation error")
const {allErrors, props} = it
if (props instanceof Name) {
gen.if(_`${props} !== true`, () =>
gen.forIn("key", data, (key: Name) =>
gen.if(unevaluatedDynamic(props, key), () => unevaluatedPropCode(key))
)
)
} else if (props !== true) {
gen.forIn("key", data, (key: Name) =>
props === undefined
? unevaluatedPropCode(key)
: gen.if(unevaluatedStatic(props, key), () => unevaluatedPropCode(key))
)
}
it.props = true
cxt.ok(_`${errsCount} === ${N.errors}`)
function unevaluatedPropCode(key: Name): void {
if (schema === false) {
cxt.setParams({unevaluatedProperty: key})
cxt.error()
if (!allErrors) gen.break()
return
}
if (!alwaysValidSchema(it, schema)) {
const valid = gen.name("valid")
cxt.subschema(
{
keyword: "unevaluatedProperties",
dataProp: key,
dataPropType: Type.Str,
},
valid
)
if (!allErrors) gen.if(not(valid), () => gen.break())
}
}
function unevaluatedDynamic(evaluatedProps: Name, key: Name): Code {
return _`!${evaluatedProps} || !${evaluatedProps}[${key}]`
}
function unevaluatedStatic(evaluatedProps: {[K in string]?: true}, key: Name): Code {
const ps: Code[] = []
for (const p in evaluatedProps) {
if (evaluatedProps[p] === true) ps.push(_`${key} !== ${p}`)
}
return and(...ps)
}
},
}
export default def