mirror of
https://github.com/morluto/rea.git
synced 2026-12-06 15:33:56 +08:00
refactor(schema): share definition child traversal (#17405)
This commit is contained in:
@@ -170,57 +170,28 @@ const exceedsJsonLengthStep = (
|
||||
const exceedsJsonLength = (value: unknown, limit: number): boolean =>
|
||||
exceedsJsonLengthStep(value, limit, { total: 0 });
|
||||
|
||||
const mapResolvesWithinDefinitions = (
|
||||
value: Record<string, unknown>,
|
||||
prefix: string,
|
||||
names: ReadonlySet<string>,
|
||||
const everySchemaChild = (
|
||||
node: Schema,
|
||||
visit: (child: unknown) => boolean,
|
||||
): boolean => {
|
||||
for (const name in value) {
|
||||
if (
|
||||
Object.hasOwn(value, name) &&
|
||||
!resolvesWithinDefinitions(value[name], prefix, names)
|
||||
)
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
const arrayResolvesWithinDefinitions = (
|
||||
items: readonly unknown[],
|
||||
prefix: string,
|
||||
names: ReadonlySet<string>,
|
||||
): boolean => {
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
if (!resolvesWithinDefinitions(items[i], prefix, names)) return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
const childrenResolveWithinDefinitions = (
|
||||
schema: Record<string, unknown>,
|
||||
prefix: string,
|
||||
names: ReadonlySet<string>,
|
||||
): boolean => {
|
||||
for (const key in schema) {
|
||||
if (!Object.hasOwn(schema, key)) continue;
|
||||
const value = schema[key];
|
||||
for (const key in node) {
|
||||
if (!Object.hasOwn(node, key)) continue;
|
||||
const value = node[key];
|
||||
if (schemaMaps.has(key)) {
|
||||
if (
|
||||
isRecord(value) &&
|
||||
!mapResolvesWithinDefinitions(value, prefix, names)
|
||||
)
|
||||
if (!isRecord(value)) continue;
|
||||
for (const name in value) {
|
||||
if (Object.hasOwn(value, name) && !visit(value[name])) return false;
|
||||
}
|
||||
} else {
|
||||
const singleton = schemaChildren.has(key);
|
||||
if (!singleton && !schemaArrays.has(key)) continue;
|
||||
if (Array.isArray(value)) {
|
||||
for (let i = 0; i < value.length; i++) {
|
||||
if (!visit(value[i])) return false;
|
||||
}
|
||||
} else if (singleton && !visit(value)) {
|
||||
return false;
|
||||
} else if (schemaArrays.has(key)) {
|
||||
if (
|
||||
Array.isArray(value) &&
|
||||
!arrayResolvesWithinDefinitions(value, prefix, names)
|
||||
)
|
||||
return false;
|
||||
} else if (schemaChildren.has(key)) {
|
||||
const ok = Array.isArray(value)
|
||||
? arrayResolvesWithinDefinitions(value, prefix, names)
|
||||
: resolvesWithinDefinitions(value, prefix, names);
|
||||
if (!ok) return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
@@ -231,19 +202,22 @@ const resolvesWithinDefinitions = (
|
||||
prefix: string,
|
||||
names: ReadonlySet<string>,
|
||||
): boolean => {
|
||||
if (!isRecord(schema)) return true;
|
||||
for (const keyword in schema) {
|
||||
if (!Object.hasOwn(schema, keyword)) continue;
|
||||
if (RESOLUTION_KEYWORDS.has(keyword)) return false;
|
||||
}
|
||||
if (Object.hasOwn(schema, "$ref")) {
|
||||
const reference = schema.$ref;
|
||||
if (typeof reference !== "string" || !reference.startsWith(prefix))
|
||||
return false;
|
||||
const name = reference.slice(prefix.length);
|
||||
if (!PLAIN_DEFINITION_NAME.test(name) || !names.has(name)) return false;
|
||||
}
|
||||
return childrenResolveWithinDefinitions(schema, prefix, names);
|
||||
const visit = (schema: unknown): boolean => {
|
||||
if (!isRecord(schema)) return true;
|
||||
for (const keyword in schema) {
|
||||
if (!Object.hasOwn(schema, keyword)) continue;
|
||||
if (RESOLUTION_KEYWORDS.has(keyword)) return false;
|
||||
}
|
||||
if (Object.hasOwn(schema, "$ref")) {
|
||||
const reference = schema.$ref;
|
||||
if (typeof reference !== "string" || !reference.startsWith(prefix))
|
||||
return false;
|
||||
const name = reference.slice(prefix.length);
|
||||
if (!PLAIN_DEFINITION_NAME.test(name) || !names.has(name)) return false;
|
||||
}
|
||||
return everySchemaChild(schema, visit);
|
||||
};
|
||||
return visit(schema);
|
||||
};
|
||||
|
||||
const hoistLargeAlternatives = (
|
||||
@@ -325,34 +299,16 @@ const collectDefinitionReferrers = (
|
||||
prefix: string,
|
||||
referrers: Map<string, Set<string>>,
|
||||
): void => {
|
||||
if (!isRecord(node)) return;
|
||||
if (Object.hasOwn(node, "$ref")) {
|
||||
const reference = node.$ref;
|
||||
if (typeof reference === "string" && reference.startsWith(prefix))
|
||||
addReferrer(referrers, reference.slice(prefix.length), caller);
|
||||
}
|
||||
for (const key in node) {
|
||||
if (!Object.hasOwn(node, key)) continue;
|
||||
const value = node[key];
|
||||
if (schemaMaps.has(key)) {
|
||||
if (!isRecord(value)) continue;
|
||||
for (const name in value) {
|
||||
if (Object.hasOwn(value, name))
|
||||
collectDefinitionReferrers(caller, value[name], prefix, referrers);
|
||||
}
|
||||
} else if (schemaArrays.has(key)) {
|
||||
if (!Array.isArray(value)) continue;
|
||||
for (let i = 0; i < value.length; i++)
|
||||
collectDefinitionReferrers(caller, value[i], prefix, referrers);
|
||||
} else if (schemaChildren.has(key)) {
|
||||
if (Array.isArray(value)) {
|
||||
for (let i = 0; i < value.length; i++)
|
||||
collectDefinitionReferrers(caller, value[i], prefix, referrers);
|
||||
} else {
|
||||
collectDefinitionReferrers(caller, value, prefix, referrers);
|
||||
}
|
||||
const visit = (node: unknown): boolean => {
|
||||
if (!isRecord(node)) return true;
|
||||
if (Object.hasOwn(node, "$ref")) {
|
||||
const reference = node.$ref;
|
||||
if (typeof reference === "string" && reference.startsWith(prefix))
|
||||
addReferrer(referrers, reference.slice(prefix.length), caller);
|
||||
}
|
||||
}
|
||||
return everySchemaChild(node, visit);
|
||||
};
|
||||
visit(node);
|
||||
};
|
||||
|
||||
const redirectWithAliases = (
|
||||
|
||||
Reference in New Issue
Block a user