Skip to content

feat(form): support recursive object, array, and $ref schemas - #25

Merged
colinjlacy merged 2 commits into
runtimeconditions:mainfrom
abhinavkdeval08-design:feat/recursive-schema-16
Dec 2, 2026
Merged

colinjlacy merged 2 commits into
runtimeconditions:mainfrom
abhinavkdeval08-design:feat/recursive-schema-16

Conversation

@abhinavkdeval08-design

Copy link
Copy Markdown
Member

Follows up on #23 (Part of #16) to handle recursive schema shapes inside extension definitions, keeping this PR focused on single-schema traversal before tackling chained extensions (spec.dependencies / env-configuration) in the next PR.

What changed

  • Nested objects & maps: Recursively extracts object properties into children (preserving nested required and constValue, e.g. api.http interface.spec) and additionalProperties schemas into mapValueDescriptor (e.g. aws.sqs interface.tags).
  • Arrays & union variants: Extracts array items via itemDescriptor and oneOf / anyOf branches via variants, giving variant descriptors unique #idx keys while keeping canonical data paths for child fieldValues matching (e.g. aws.s3 interface.operations[].role).
  • Local $ref resolution & cycle guard: Resolves #/$defs/... and #/definitions/... pointers (including when interface itself is a $ref so required fields stay intact), picks the first non-null type for nullable arrays like ['null', 'integer'], and terminates self/indirect cycles with isRecursiveRef + recursiveRef (e.g. simpleSchema).
  • Tests: Added unit tests in src/form/form.test.ts covering nested api.http objects, recursive $ref cycle guards, aws.s3 union variants + fieldValues matching, aws.sqs map values, nullable types, and $ref interface required.

Verification

  • Built with tsc -p tsconfig.build.json (0 errors) and ran Jest (2 suites, 17/17 tests passing).
  • Existing extractConditionForms API and flat descriptors remain unchanged; chained extension dependencies (spec.dependencies) are deferred to the next PR to keep this diff small.

Follows up on runtimeconditions#23 by recursively extracting nested object properties, array item shapes, map values (additionalProperties), union variants, and local $ref definitions with cycle protection.

Chained extensions (spec.dependencies / env-configuration) are intentionally deferred to a separate follow-up PR to keep this change focused.

Signed-off-by: Abhinav Deval <abhinavkdeval08@gmail.com>
Comment thread src/form/catalog.ts
Comment on lines +254 to +259
const fieldValueMatch = ctx.ext.spec.fieldValues?.find(
fv =>
fv.targetKind === ctx.schemaItem.appliesToKind &&
fv.targetType === ctx.schemaItem.appliesToInterfaceType &&
(fv.field === path || ((path === `interface.${name}` || path === name) && fv.field === name)),
);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oof, that's a lot of logic. Could you add a comment to explain what the reasoning is?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment explaining the canonical path lookup and top-level bare-name fallback in 2e68434.

Comment thread src/form/catalog.ts Outdated
}

function buildEnumOptions(
pSchema: JsonSchemaProperty,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe instead of pSchema it could be schemaProp?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed in 2e68434.

Comment thread src/form/catalog.ts Outdated
);

return pSchema.enum.map(val => {
const sVal = String(val);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe instead of sVal it could be strVal?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed in 2e68434.

Comment thread src/form/catalog.ts
Comment on lines +375 to +380
if (
Array.isArray(unionBranches) &&
!descriptor.children &&
!descriptor.itemDescriptor &&
!descriptor.mapValueDescriptor
) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another area where a comment would help in understanding everything that's going on here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added comments here (and on resolveSchema / inferFieldType) in 2e68434 explaining why this guard skips unions when a concrete structure already exists.

Comment thread src/form/types.ts
@@ -43,7 +43,7 @@ export interface ExtensionFieldValue {
}

export interface JsonSchemaProperty {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's already a type library in the JSON schema universe that we could use instead of forming our own types. Or if our use case is specific enough that we need to maintain our own 🤔

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call — @types/json-schema (JSONSchema7) would cover most of this without adding runtime weight. Kept our own type for now since it's deliberately loose ([key: string]: unknown) and we only read a narrow subset (plus $defs). Happy to swap it in as a small follow-up if you'd prefer — want that in this PR or separate?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This raises some questions:

  • "since it's deliberately loose ([key: string]: unknown) " -> why is it deliberately loose? if we have a reason that's fine, I'm just curious
  • "we only read a narrow subset" -> would that matter, since the types would be brought in as a compile-time library, and wouldn't impact the end-result dist package?
  • "plus $defs" -> are $defs not in @types/json-schema?

I would need to know the answer to these questions before I can determine if it should be in this PR or not

Also, thanks for adding those comments! 😁 👍

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No good reason, honestly. The index signature carried over from the first version so keywords we don't model would still type-check — it wasn't a deliberate design choice. You're right on the other two: type-only deps don't affect runtime JS output, and JSONSchema7 does include $defs (and definitions), so I was wrong there.

One thing to note: because our exported types (ExtensionSchemaItem, ConditionalRule) reference it and tsconfig emits .d.ts files, @types/json-schema would go in dependencies rather than devDependencies (still zero runtime code).

I'd suggest a separate small follow-up since switching to JSONSchema7 means narrowing boolean schemas (JSONSchema7Definition) and handling the wider enum type, which would add noise to this diff. Happy to do it in this PR if you prefer though!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, agreed. Let's take it in the next PR. Opened an issue: #27

@colinjlacy colinjlacy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some things to consider. Functionally it looks solid 👍

Most pressing thing is probably just adding comments to help explain the through path to the multi-faceted logic branches.

Signed-off-by: Abhinav Deval <abhinavkdeval08@gmail.com>
@colinjlacy
colinjlacy merged commit 5155a4f into runtimeconditions:main Dec 2, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants