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

Function validateUnion

lib/vocabularies/code.ts:135–168  ·  view source on GitHub ↗
(cxt: KeywordCxt)

Source from the content-addressed store, hash-verified

133}
134
135export function validateUnion(cxt: KeywordCxt): void {
136 const {gen, schema, keyword, it} = cxt
137 /* istanbul ignore if */
138 if (!Array.isArray(schema)) throw new Error("ajv implementation error")
139 const alwaysValid = schema.some((sch: AnySchema) => alwaysValidSchema(it, sch))
140 if (alwaysValid && !it.opts.unevaluated) return
141
142 const valid = gen.let("valid", false)
143 const schValid = gen.name("_valid")
144
145 gen.block(() =>
146 schema.forEach((_sch: AnySchema, i: number) => {
147 const schCxt = cxt.subschema(
148 {
149 keyword,
150 schemaProp: i,
151 compositeRule: true,
152 },
153 schValid
154 )
155 gen.assign(valid, _`${valid} || ${schValid}`)
156 const merged = cxt.mergeValidEvaluated(schCxt, schValid)
157 // can short-circuit if `unevaluatedProperties/Items` not supported (opts.unevaluated !== true)
158 // or if all properties and items were evaluated (it.props === true && it.items === true)
159 if (!merged) gen.if(not(valid))
160 })
161 )
162
163 cxt.result(
164 valid,
165 () => cxt.reset(),
166 () => cxt.error(true)
167 )
168}

Callers

nothing calls this directly

Calls 13

alwaysValidSchemaFunction · 0.90
_Function · 0.90
notFunction · 0.90
letMethod · 0.80
blockMethod · 0.80
subschemaMethod · 0.80
assignMethod · 0.80
mergeValidEvaluatedMethod · 0.80
ifMethod · 0.80
resultMethod · 0.80
resetMethod · 0.80
errorMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…