2 Object.defineProperty(exports, "__esModule", { value: true });
3 exports.hasRef = void 0;
4 const compile_1 = require("../../compile");
5 const codegen_1 = require("../../compile/codegen");
6 const ref_error_1 = require("../../compile/ref_error");
7 const names_1 = require("../../compile/names");
8 const ref_1 = require("../core/ref");
9 const metadata_1 = require("./metadata");
14 (0, metadata_1.checkMetadata)(cxt);
15 const { gen, data, schema: ref, parentSchema, it } = cxt;
16 const { schemaEnv: { root }, } = it;
17 const valid = gen.name("valid");
18 if (parentSchema.nullable) {
19 gen.var(valid, (0, codegen_1._) `${data} === null`);
20 gen.if((0, codegen_1.not)(valid), validateJtdRef);
23 gen.var(valid, false);
27 function validateJtdRef() {
29 const refSchema = (_a = root.schema.definitions) === null || _a === void 0 ? void 0 : _a[ref];
31 throw new ref_error_1.default("", ref, `No definition ${ref}`);
32 if (hasRef(refSchema) || !it.opts.inlineRefs)
33 callValidate(refSchema);
35 inlineRefSchema(refSchema);
37 function callValidate(schema) {
38 const sch = compile_1.compileSchema.call(it.self, new compile_1.SchemaEnv({ schema, root, schemaPath: `/definitions/${ref}` }));
39 const v = (0, ref_1.getValidate)(cxt, sch);
40 const errsCount = gen.const("_errs", names_1.default.errors);
41 (0, ref_1.callRef)(cxt, v, sch, sch.$async);
42 gen.assign(valid, (0, codegen_1._) `${errsCount} === ${names_1.default.errors}`);
44 function inlineRefSchema(schema) {
45 const schName = gen.scopeValue("schema", it.opts.code.source === true ? { ref: schema, code: (0, codegen_1.stringify)(schema) } : { ref: schema });
49 schemaPath: codegen_1.nil,
50 topSchemaRef: schName,
51 errSchemaPath: `/definitions/${ref}`,
56 function hasRef(schema) {
57 for (const key in schema) {
59 if (key === "ref" || (typeof (sch = schema[key]) == "object" && hasRef(sch)))
64 exports.hasRef = hasRef;
65 exports.default = def;
66 //# sourceMappingURL=ref.js.map