Repository navigation
feat(form): support recursive object, array, and $ref schemas - #25
colinjlacy merged 2 commits into
Conversation
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>
| 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)), | ||
| ); |
There was a problem hiding this comment.
Oof, that's a lot of logic. Could you add a comment to explain what the reasoning is?
There was a problem hiding this comment.
Added a comment explaining the canonical path lookup and top-level bare-name fallback in 2e68434.
| } | ||
|
|
||
| function buildEnumOptions( | ||
| pSchema: JsonSchemaProperty, |
There was a problem hiding this comment.
nit: maybe instead of pSchema it could be schemaProp?
| ); | ||
|
|
||
| return pSchema.enum.map(val => { | ||
| const sVal = String(val); |
There was a problem hiding this comment.
nit: maybe instead of sVal it could be strVal?
| if ( | ||
| Array.isArray(unionBranches) && | ||
| !descriptor.children && | ||
| !descriptor.itemDescriptor && | ||
| !descriptor.mapValueDescriptor | ||
| ) { |
There was a problem hiding this comment.
Another area where a comment would help in understanding everything that's going on here.
There was a problem hiding this comment.
Added comments here (and on resolveSchema / inferFieldType) in 2e68434 explaining why this guard skips unions when a concrete structure already exists.
| @@ -43,7 +43,7 @@ export interface ExtensionFieldValue { | |||
| } | |||
|
|
|||
| export interface JsonSchemaProperty { | |||
There was a problem hiding this comment.
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 🤔
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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$defsnot 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! 😁 👍
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Yes, agreed. Let's take it in the next PR. Opened an issue: #27
colinjlacy
left a comment
There was a problem hiding this comment.
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>
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
objectproperties intochildren(preserving nestedrequiredandconstValue, e.g.api.httpinterface.spec) andadditionalPropertiesschemas intomapValueDescriptor(e.g.aws.sqsinterface.tags).itemsviaitemDescriptorandoneOf/anyOfbranches viavariants, giving variant descriptors unique#idxkeys while keeping canonical data paths for childfieldValuesmatching (e.g.aws.s3interface.operations[].role).$refresolution & cycle guard: Resolves#/$defs/...and#/definitions/...pointers (including wheninterfaceitself is a$refsorequiredfields stay intact), picks the first non-null type for nullable arrays like['null', 'integer'], and terminates self/indirect cycles withisRecursiveRef+recursiveRef(e.g.simpleSchema).src/form/form.test.tscovering nestedapi.httpobjects, recursive$refcycle guards,aws.s3union variants +fieldValuesmatching,aws.sqsmap values, nullable types, and$refinterfacerequired.Verification
tsc -p tsconfig.build.json(0errors) and ran Jest (2suites,17/17tests passing).extractConditionFormsAPI and flat descriptors remain unchanged; chained extension dependencies (spec.dependencies) are deferred to the next PR to keep this diff small.