@sinclair/typebox
Version:
Json Schema Type Builder with Static Type Resolution for TypeScript
106 lines (104 loc) • 4.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Deref = void 0;
const type_1 = require("../clone/type");
const index_1 = require("../discard/index");
const value_1 = require("../guard/value");
// ------------------------------------------------------------------
// TypeGuard
// ------------------------------------------------------------------
const type_2 = require("../guard/type");
function FromRest(schema, references) {
return schema.map((schema) => Deref(schema, references));
}
// prettier-ignore
function FromProperties(properties, references) {
return globalThis.Object.getOwnPropertyNames(properties).reduce((acc, key) => {
return { ...acc, [key]: Deref(properties[key], references) };
}, {});
}
// prettier-ignore
function FromConstructor(schema, references) {
schema.parameters = FromRest(schema.parameters, references);
schema.returns = Deref(schema.returns, references);
return schema;
}
// prettier-ignore
function FromFunction(schema, references) {
schema.parameters = FromRest(schema.parameters, references);
schema.returns = Deref(schema.returns, references);
return schema;
}
// prettier-ignore
function FromIntersect(schema, references) {
schema.allOf = FromRest(schema.allOf, references);
return schema;
}
// prettier-ignore
function FromUnion(schema, references) {
schema.anyOf = FromRest(schema.anyOf, references);
return schema;
}
// prettier-ignore
function FromTuple(schema, references) {
if ((0, value_1.IsUndefined)(schema.items))
return schema;
schema.items = FromRest(schema.items, references);
return schema;
}
// prettier-ignore
function FromArray(schema, references) {
schema.items = Deref(schema.items, references);
return schema;
}
// prettier-ignore
function FromObject(schema, references) {
schema.properties = FromProperties(schema.properties, references);
return schema;
}
// prettier-ignore
function FromPromise(schema, references) {
schema.item = Deref(schema.item, references);
return schema;
}
// prettier-ignore
function FromAsyncIterator(schema, references) {
schema.items = Deref(schema.items, references);
return schema;
}
// prettier-ignore
function FromIterator(schema, references) {
schema.items = Deref(schema.items, references);
return schema;
}
// prettier-ignore
function FromRef(schema, references) {
const target = references.find(remote => remote.$id === schema.$ref);
if (target === undefined)
throw Error(`Unable to dereference schema with $id ${schema.$ref}`);
const discard = (0, index_1.Discard)(target, ['$id']);
return Deref(discard, references);
}
// prettier-ignore
function DerefResolve(schema, references) {
return ((0, type_2.IsConstructor)(schema) ? FromConstructor(schema, references) :
(0, type_2.IsFunction)(schema) ? FromFunction(schema, references) :
(0, type_2.IsIntersect)(schema) ? FromIntersect(schema, references) :
(0, type_2.IsUnion)(schema) ? FromUnion(schema, references) :
(0, type_2.IsTuple)(schema) ? FromTuple(schema, references) :
(0, type_2.IsArray)(schema) ? FromArray(schema, references) :
(0, type_2.IsObject)(schema) ? FromObject(schema, references) :
(0, type_2.IsPromise)(schema) ? FromPromise(schema, references) :
(0, type_2.IsAsyncIterator)(schema) ? FromAsyncIterator(schema, references) :
(0, type_2.IsIterator)(schema) ? FromIterator(schema, references) :
(0, type_2.IsRef)(schema) ? FromRef(schema, references) :
schema);
}
// ------------------------------------------------------------------
// TDeref
// ------------------------------------------------------------------
/** `[Json]` Creates a dereferenced type */
function Deref(schema, references) {
return DerefResolve((0, type_1.CloneType)(schema), (0, type_1.CloneRest)(references));
}
exports.Deref = Deref;