Files
NapCatQQ/.pnpm-store/v3/files/dd/4f4f0750dda62d161d3ed83977305dc93ec92cd35e5a450efe7e55ace870600187fd0b541c7ad3959828e768dac16c7507b094337596cd8877ccdc60897f1d
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

30 lines
1.2 KiB
Plaintext

import { TypeBoxError } from '../../type/error/index.mjs';
import { Kind } from '../../type/symbols/index.mjs';
import { IsString } from '../guard/guard.mjs';
export class TypeDereferenceError extends TypeBoxError {
constructor(schema) {
super(`Unable to dereference schema with $id '${schema.$ref}'`);
this.schema = schema;
}
}
function Resolve(schema, references) {
const target = references.find((target) => target.$id === schema.$ref);
if (target === undefined)
throw new TypeDereferenceError(schema);
return Deref(target, references);
}
/** `[Internal]` Pushes a schema onto references if the schema has an $id and does not exist on references */
export function Pushref(schema, references) {
if (!IsString(schema.$id) || references.some((target) => target.$id === schema.$id))
return references;
references.push(schema);
return references;
}
/** `[Internal]` Dereferences a schema from the references array or throws if not found */
export function Deref(schema, references) {
// prettier-ignore
return (schema[Kind] === 'This' || schema[Kind] === 'Ref')
? Resolve(schema, references)
: schema;
}