source: imaps-frontend/node_modules/ajv/lib/vocabularies/jtd/ref.ts@ 79a0317

main
Last change on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.3 KB
Line 
1import type {CodeKeywordDefinition, AnySchemaObject} from "../../types"
2import type {KeywordCxt} from "../../compile/validate"
3import {compileSchema, SchemaEnv} from "../../compile"
4import {_, not, nil, stringify} from "../../compile/codegen"
5import MissingRefError from "../../compile/ref_error"
6import N from "../../compile/names"
7import {getValidate, callRef} from "../core/ref"
8import {checkMetadata} from "./metadata"
9
10const def: CodeKeywordDefinition = {
11 keyword: "ref",
12 schemaType: "string",
13 code(cxt: KeywordCxt) {
14 checkMetadata(cxt)
15 const {gen, data, schema: ref, parentSchema, it} = cxt
16 const {
17 schemaEnv: {root},
18 } = it
19 const valid = gen.name("valid")
20 if (parentSchema.nullable) {
21 gen.var(valid, _`${data} === null`)
22 gen.if(not(valid), validateJtdRef)
23 } else {
24 gen.var(valid, false)
25 validateJtdRef()
26 }
27 cxt.ok(valid)
28
29 function validateJtdRef(): void {
30 const refSchema = (root.schema as AnySchemaObject).definitions?.[ref]
31 if (!refSchema) {
32 throw new MissingRefError(it.opts.uriResolver, "", ref, `No definition ${ref}`)
33 }
34 if (hasRef(refSchema) || !it.opts.inlineRefs) callValidate(refSchema)
35 else inlineRefSchema(refSchema)
36 }
37
38 function callValidate(schema: AnySchemaObject): void {
39 const sch = compileSchema.call(
40 it.self,
41 new SchemaEnv({schema, root, schemaPath: `/definitions/${ref}`})
42 )
43 const v = getValidate(cxt, sch)
44 const errsCount = gen.const("_errs", N.errors)
45 callRef(cxt, v, sch, sch.$async)
46 gen.assign(valid, _`${errsCount} === ${N.errors}`)
47 }
48
49 function inlineRefSchema(schema: AnySchemaObject): void {
50 const schName = gen.scopeValue(
51 "schema",
52 it.opts.code.source === true ? {ref: schema, code: stringify(schema)} : {ref: schema}
53 )
54 cxt.subschema(
55 {
56 schema,
57 dataTypes: [],
58 schemaPath: nil,
59 topSchemaRef: schName,
60 errSchemaPath: `/definitions/${ref}`,
61 },
62 valid
63 )
64 }
65 },
66}
67
68export function hasRef(schema: AnySchemaObject): boolean {
69 for (const key in schema) {
70 let sch: AnySchemaObject
71 if (key === "ref" || (typeof (sch = schema[key]) == "object" && hasRef(sch))) return true
72 }
73 return false
74}
75
76export default def
Note: See TracBrowser for help on using the repository browser.