MCPcopy
hub / github.com/ajv-validator/ajv / code

Function code

lib/vocabularies/jtd/enum.ts:18–42  ·  view source on GitHub ↗
(cxt: KeywordCxt)

Source from the content-addressed store, hash-verified

16 schemaType: "array",
17 error,
18 code(cxt: KeywordCxt) {
19 checkMetadata(cxt)
20 const {gen, data, schema, schemaValue, parentSchema, it} = cxt
21 if (schema.length === 0) throw new Error("enum must have non-empty array")
22 if (schema.length !== new Set(schema).size) throw new Error("enum items must be unique")
23 let valid: Code
24 const isString = _`typeof ${data} == "string"`
25 if (schema.length >= it.opts.loopEnum) {
26 let cond: Code
27 ;[valid, cond] = checkNullable(cxt, isString)
28 gen.if(cond, loopEnum)
29 } else {
30 /* istanbul ignore if */
31 if (!Array.isArray(schema)) throw new Error("ajv implementation error")
32 valid = and(isString, or(...schema.map((value: string) => _`${data} === ${value}`)))
33 if (parentSchema.nullable) valid = or(_`${data} === null`, valid)
34 }
35 cxt.pass(valid)
36
37 function loopEnum(): void {
38 gen.forOf("v", schemaValue as Code, (v) =>
39 gen.if(_`${valid} = ${data} === ${v}`, () => gen.break())
40 )
41 }
42 },
43}
44
45export default def

Callers

nothing calls this directly

Calls 7

checkMetadataFunction · 0.90
_Function · 0.90
checkNullableFunction · 0.90
andFunction · 0.90
orFunction · 0.90
ifMethod · 0.80
passMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…