| 1 | import type {CodeKeywordDefinition, AnySchemaObject} from "../../types" |
| 2 | import type {KeywordCxt} from "../../compile/validate" |
| 3 | import {compileSchema, SchemaEnv} from "../../compile" |
| 4 | import {_, not, nil, stringify} from "../../compile/codegen" |
| 5 | import MissingRefError from "../../compile/ref_error" |
| 6 | import N from "../../compile/names" |
| 7 | import {getValidate, callRef} from "../core/ref" |
| 8 | import {checkMetadata} from "./metadata" |
| 9 | |
| 10 | const 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 | |
| 68 | export 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 | |
| 76 | export default def |