Skip to content

Commit 6d87faf

Browse files
committed
refactor(cli): tidy config schema path helpers
1 parent 22bea23 commit 6d87faf

2 files changed

Lines changed: 45 additions & 33 deletions

File tree

src/cli/config-cli.test.ts

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,27 @@ function createPluginMetadataSnapshot(
181181
};
182182
}
183183

184+
function configRecordWithRequireMentionSchema() {
185+
return {
186+
type: "object",
187+
additionalProperties: {
188+
type: "object",
189+
properties: {
190+
requireMention: { type: "boolean" },
191+
},
192+
},
193+
};
194+
}
195+
196+
function configChannelSchemaWithRecord(recordKey: string) {
197+
return {
198+
type: "object",
199+
properties: {
200+
[recordKey]: configRecordWithRequireMentionSchema(),
201+
},
202+
};
203+
}
204+
184205
function setConfigMutationShapeSchema() {
185206
mockReadBestEffortRuntimeConfigSchema.mockResolvedValue({
186207
schema: {
@@ -205,34 +226,8 @@ function setConfigMutationShapeSchema() {
205226
channels: {
206227
type: "object",
207228
properties: {
208-
discord: {
209-
type: "object",
210-
properties: {
211-
guilds: {
212-
type: "object",
213-
additionalProperties: {
214-
type: "object",
215-
properties: {
216-
requireMention: { type: "boolean" },
217-
},
218-
},
219-
},
220-
},
221-
},
222-
telegram: {
223-
type: "object",
224-
properties: {
225-
groups: {
226-
type: "object",
227-
additionalProperties: {
228-
type: "object",
229-
properties: {
230-
requireMention: { type: "boolean" },
231-
},
232-
},
233-
},
234-
},
235-
},
229+
discord: configChannelSchemaWithRecord("guilds"),
230+
telegram: configChannelSchemaWithRecord("groups"),
236231
},
237232
},
238233
},

src/cli/config-cli.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,14 @@ function schemaTypes(schema: JsonSchemaRecord): Set<string> {
488488
return new Set();
489489
}
490490

491-
function schemaAlternatives(schema: JsonSchemaRecord): JsonSchemaRecord[] {
491+
function schemaAlternatives(
492+
schema: JsonSchemaRecord,
493+
seen = new Set<JsonSchemaRecord>(),
494+
): JsonSchemaRecord[] {
495+
if (seen.has(schema)) {
496+
return [];
497+
}
498+
seen.add(schema);
492499
const alternatives: JsonSchemaRecord[] = [schema];
493500
for (const key of ["anyOf", "oneOf", "allOf"] as const) {
494501
const entries = schema[key];
@@ -497,15 +504,19 @@ function schemaAlternatives(schema: JsonSchemaRecord): JsonSchemaRecord[] {
497504
}
498505
for (const entry of entries) {
499506
if (isSchemaRecord(entry)) {
500-
alternatives.push(...schemaAlternatives(entry));
507+
alternatives.push(...schemaAlternatives(entry, seen));
501508
}
502509
}
503510
}
504511
return alternatives;
505512
}
506513

507514
function schemaLooksArray(schema: JsonSchemaRecord): boolean {
508-
return schemaTypes(schema).has("array") || isSchemaRecord(schema.items);
515+
return (
516+
schemaTypes(schema).has("array") ||
517+
isSchemaRecord(schema.items) ||
518+
Array.isArray(schema.items)
519+
);
509520
}
510521

511522
function schemaLooksObject(schema: JsonSchemaRecord): boolean {
@@ -522,8 +533,14 @@ function propertySchema(schema: JsonSchemaRecord, segment: PathSegment): JsonSch
522533
const schemas: JsonSchemaRecord[] = [];
523534
for (const alternative of schemaAlternatives(schema)) {
524535
if (schemaLooksArray(alternative)) {
525-
if (isIndexSegment(segment) && isSchemaRecord(alternative.items)) {
526-
schemas.push(alternative.items);
536+
if (isIndexSegment(segment)) {
537+
const index = Number.parseInt(segment, 10);
538+
const indexedItem = Array.isArray(alternative.items)
539+
? alternative.items[index]
540+
: alternative.items;
541+
if (isSchemaRecord(indexedItem)) {
542+
schemas.push(indexedItem);
543+
}
527544
}
528545
continue;
529546
}

0 commit comments

Comments
 (0)