refactor(cli): tidy config schema path helpers

This commit is contained in:
Altay
2026-05-22 00:02:08 +03:00
parent 3a12849e79
commit cb55b4a40d
2 changed files with 45 additions and 33 deletions

View File

@@ -181,6 +181,27 @@ function createPluginMetadataSnapshot(
};
}
function configRecordWithRequireMentionSchema() {
return {
type: "object",
additionalProperties: {
type: "object",
properties: {
requireMention: { type: "boolean" },
},
},
};
}
function configChannelSchemaWithRecord(recordKey: string) {
return {
type: "object",
properties: {
[recordKey]: configRecordWithRequireMentionSchema(),
},
};
}
function setConfigMutationShapeSchema() {
mockReadBestEffortRuntimeConfigSchema.mockResolvedValue({
schema: {
@@ -205,34 +226,8 @@ function setConfigMutationShapeSchema() {
channels: {
type: "object",
properties: {
discord: {
type: "object",
properties: {
guilds: {
type: "object",
additionalProperties: {
type: "object",
properties: {
requireMention: { type: "boolean" },
},
},
},
},
},
telegram: {
type: "object",
properties: {
groups: {
type: "object",
additionalProperties: {
type: "object",
properties: {
requireMention: { type: "boolean" },
},
},
},
},
},
discord: configChannelSchemaWithRecord("guilds"),
telegram: configChannelSchemaWithRecord("groups"),
},
},
},

View File

@@ -488,7 +488,14 @@ function schemaTypes(schema: JsonSchemaRecord): Set<string> {
return new Set();
}
function schemaAlternatives(schema: JsonSchemaRecord): JsonSchemaRecord[] {
function schemaAlternatives(
schema: JsonSchemaRecord,
seen = new Set<JsonSchemaRecord>(),
): JsonSchemaRecord[] {
if (seen.has(schema)) {
return [];
}
seen.add(schema);
const alternatives: JsonSchemaRecord[] = [schema];
for (const key of ["anyOf", "oneOf", "allOf"] as const) {
const entries = schema[key];
@@ -497,7 +504,7 @@ function schemaAlternatives(schema: JsonSchemaRecord): JsonSchemaRecord[] {
}
for (const entry of entries) {
if (isSchemaRecord(entry)) {
alternatives.push(...schemaAlternatives(entry));
alternatives.push(...schemaAlternatives(entry, seen));
}
}
}
@@ -505,7 +512,11 @@ function schemaAlternatives(schema: JsonSchemaRecord): JsonSchemaRecord[] {
}
function schemaLooksArray(schema: JsonSchemaRecord): boolean {
return schemaTypes(schema).has("array") || isSchemaRecord(schema.items);
return (
schemaTypes(schema).has("array") ||
isSchemaRecord(schema.items) ||
Array.isArray(schema.items)
);
}
function schemaLooksObject(schema: JsonSchemaRecord): boolean {
@@ -522,8 +533,14 @@ function propertySchema(schema: JsonSchemaRecord, segment: PathSegment): JsonSch
const schemas: JsonSchemaRecord[] = [];
for (const alternative of schemaAlternatives(schema)) {
if (schemaLooksArray(alternative)) {
if (isIndexSegment(segment) && isSchemaRecord(alternative.items)) {
schemas.push(alternative.items);
if (isIndexSegment(segment)) {
const index = Number.parseInt(segment, 10);
const indexedItem = Array.isArray(alternative.items)
? alternative.items[index]
: alternative.items;
if (isSchemaRecord(indexedItem)) {
schemas.push(indexedItem);
}
}
continue;
}