Repository navigation
feat(form): add decoupled extension schema parser #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
colinjlacy
merged 3 commits into
runtimeconditions:main
from
abhinavkdeval08-design:feat/form-condition-declarations-16
Nov 30, 2026
+667
−0
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
ea458fd
feat(form): add decoupled extension schema parser and profile YAML ge…
abhinavkdeval08-design 466f8a3
refactor(form): simplify label formatting, make version optional, and…
abhinavkdeval08-design 020e954
refactor(form): drop generator from this PR and use raw field names a…
abhinavkdeval08-design File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,266 @@ | ||
| import { | ||
| ConditionalRule, | ||
| ExtractedConditionForm, | ||
| FormEnumOption, | ||
| FormFieldDescriptor, | ||
| FormFieldType, | ||
| JsonSchemaProperty, | ||
| RuntimeConditionsExtensionDefinition, | ||
| } from './types'; | ||
|
|
||
| /** | ||
| * Returns property names as field labels as-is, preserving raw field | ||
| * names for developer familiarity. | ||
| */ | ||
| export function formatFieldLabel(propName: string): string { | ||
| return propName || ''; | ||
| } | ||
|
|
||
| /** | ||
| * Validates and parses a raw extension object into a type-safe definition. | ||
| */ | ||
| export function parseExtensionDefinition( | ||
| raw: unknown, | ||
| ): RuntimeConditionsExtensionDefinition { | ||
| if (!raw || typeof raw !== 'object') { | ||
| throw new Error('Extension definition must be a valid non-null object'); | ||
| } | ||
|
|
||
| const def = raw as Partial<RuntimeConditionsExtensionDefinition>; | ||
|
|
||
| if (def.kind !== 'RuntimeConditionsExtensionDefinition') { | ||
| throw new Error( | ||
| `Invalid extension kind: expected "RuntimeConditionsExtensionDefinition", got "${def.kind}"`, | ||
| ); | ||
| } | ||
|
|
||
| if (!def.metadata?.id) { | ||
| throw new Error('Extension definition missing metadata.id'); | ||
| } | ||
|
|
||
| if (!def.spec?.kinds || !Array.isArray(def.spec.kinds)) { | ||
| throw new Error('Extension definition missing spec.kinds array'); | ||
| } | ||
|
|
||
| if (!def.spec?.interfaceTypes || !Array.isArray(def.spec.interfaceTypes)) { | ||
| throw new Error('Extension definition missing spec.interfaceTypes array'); | ||
| } | ||
|
|
||
| if (!def.spec?.schemas || !Array.isArray(def.spec.schemas)) { | ||
| throw new Error('Extension definition missing spec.schemas array'); | ||
| } | ||
|
|
||
| return def as RuntimeConditionsExtensionDefinition; | ||
| } | ||
|
|
||
| /** | ||
| * Collects statically required field names (unconditionally required across all inputs). | ||
| * Excludes conditional (if/then) requirements to prevent false rejections. | ||
| */ | ||
| function collectStaticRequiredFields( | ||
| schema: JsonSchemaProperty, | ||
| targetScope: 'interface' | 'condition', | ||
| ): Set<string> { | ||
| const reqs = new Set<string>(); | ||
|
|
||
| if (targetScope === 'condition') { | ||
| if (Array.isArray(schema.required)) { | ||
| schema.required.forEach(r => reqs.add(r)); | ||
| } | ||
| } else { | ||
| const iface = schema.properties?.interface as JsonSchemaProperty | undefined; | ||
| if (iface && Array.isArray(iface.required)) { | ||
| iface.required.forEach(r => reqs.add(r)); | ||
| } | ||
| } | ||
|
|
||
| return reqs; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts conditional rules (if/then blocks) from root schema and allOf blocks. | ||
| */ | ||
| function extractConditionalRules(schema: JsonSchemaProperty): ConditionalRule[] { | ||
| const rules: ConditionalRule[] = []; | ||
|
|
||
| if (schema.if && schema.then) { | ||
| rules.push({ if: schema.if, then: schema.then }); | ||
| } | ||
|
|
||
| if (Array.isArray(schema.allOf)) { | ||
| for (const sub of schema.allOf) { | ||
| if (sub.if && sub.then) { | ||
| rules.push({ if: sub.if, then: sub.then }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Also check interface-level if/then if present | ||
| const iface = schema.properties?.interface as JsonSchemaProperty | undefined; | ||
| if (iface) { | ||
| if (iface.if && iface.then) { | ||
| rules.push({ if: iface.if, then: iface.then }); | ||
| } | ||
| if (Array.isArray(iface.allOf)) { | ||
| for (const sub of iface.allOf) { | ||
| if (sub.if && sub.then) { | ||
| rules.push({ if: sub.if, then: sub.then }); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return rules; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts form field descriptors from an extension definition. | ||
| * Covers interface fields, condition-level fields (e.g. env, configuration), | ||
| * enums with descriptions, and dynamic validation metadata. | ||
| */ | ||
| export function extractConditionForms( | ||
| ext: RuntimeConditionsExtensionDefinition, | ||
| ): ExtractedConditionForm[] { | ||
| const result: ExtractedConditionForm[] = []; | ||
|
|
||
| for (const schemaItem of ext.spec.schemas) { | ||
| const kindObj = ext.spec.kinds.find( | ||
| k => k.name === schemaItem.appliesToKind, | ||
| ); | ||
| const ifaceTypeObj = ext.spec.interfaceTypes.find( | ||
| i => | ||
| i.name === schemaItem.appliesToInterfaceType && | ||
| i.targetKind === schemaItem.appliesToKind, | ||
| ); | ||
|
|
||
| const fields: FormFieldDescriptor[] = []; | ||
| const schemaObj = schemaItem.schema ?? {}; | ||
| const conditionalRules = extractConditionalRules(schemaObj); | ||
|
|
||
| // 1. Extract Interface fields (if interface property exists) | ||
| const ifaceSchema = schemaObj.properties?.interface as JsonSchemaProperty | undefined; | ||
| const ifaceProps = ifaceSchema?.properties ?? {}; | ||
| const ifaceRequiredSet = collectStaticRequiredFields(schemaObj, 'interface'); | ||
|
|
||
| for (const [propName, propVal] of Object.entries(ifaceProps)) { | ||
| if (propVal && typeof propVal === 'object' && 'const' in propVal) { | ||
| continue; | ||
| } | ||
|
|
||
| const pSchema = (propVal || {}) as JsonSchemaProperty; | ||
| const isRequired = ifaceRequiredSet.has(propName); | ||
|
|
||
| let fieldType: FormFieldType = 'string'; | ||
| let enumOptions: FormEnumOption[] | undefined; | ||
|
|
||
| if (Array.isArray(pSchema.enum)) { | ||
| fieldType = 'enum'; | ||
| const fieldValueMatch = ext.spec.fieldValues?.find( | ||
| fv => | ||
| fv.targetKind === schemaItem.appliesToKind && | ||
| fv.targetType === schemaItem.appliesToInterfaceType && | ||
| (fv.field === `interface.${propName}` || fv.field === propName), | ||
| ); | ||
|
|
||
| enumOptions = pSchema.enum.map(val => { | ||
| const sVal = String(val); | ||
| const desc = fieldValueMatch?.valueDescriptions?.[sVal]; | ||
| return { | ||
| value: sVal, | ||
| label: sVal.charAt(0).toUpperCase() + sVal.slice(1), | ||
| description: desc, | ||
| }; | ||
| }); | ||
| } else if (pSchema.type) { | ||
| fieldType = pSchema.type as FormFieldType; | ||
| } | ||
|
|
||
| const fieldDoc = ext.spec.interfaceFields?.find( | ||
| f => | ||
| f.targetKind === schemaItem.appliesToKind && | ||
| f.targetType === schemaItem.appliesToInterfaceType && | ||
| f.name === propName, | ||
| ); | ||
|
|
||
| const description = | ||
| fieldDoc?.description || | ||
| pSchema.description || | ||
| `Specifies ${propName}`; | ||
|
|
||
| fields.push({ | ||
| name: propName, | ||
| path: `interface.${propName}`, | ||
| location: 'interface', | ||
| label: formatFieldLabel(propName), | ||
| description, | ||
| type: fieldType, | ||
| required: isRequired, | ||
| minimum: pSchema.minimum, | ||
| maximum: pSchema.maximum, | ||
| pattern: typeof pSchema.pattern === 'string' ? pSchema.pattern : undefined, | ||
| enumOptions, | ||
| }); | ||
| } | ||
|
|
||
| // 2. Extract Condition-level fields (e.g. env, configuration) | ||
| const rootProps = schemaObj.properties ?? {}; | ||
| const conditionRequiredSet = collectStaticRequiredFields(schemaObj, 'condition'); | ||
|
|
||
| for (const [propName, propVal] of Object.entries(rootProps)) { | ||
| if (['kind', 'interface', 'name', 'optional'].includes(propName)) { | ||
| continue; | ||
| } | ||
|
|
||
| const pSchema = (propVal || {}) as JsonSchemaProperty; | ||
| const isRequired = conditionRequiredSet.has(propName); | ||
|
|
||
| let fieldType: FormFieldType = 'string'; | ||
| if (pSchema.type) { | ||
| fieldType = pSchema.type as FormFieldType; | ||
| } | ||
|
abhinavkdeval08-design marked this conversation as resolved.
Resolved
|
||
|
|
||
| // Look up the extension's documentation for this condition-level field. A conditionFields | ||
| // entry documents this property when its name matches AND it targets this schema's kind and | ||
| // interface type. An entry that omits appliesToKinds / appliesToInterfaceTypes is untargeted | ||
| // and so applies to every kind / interface type. | ||
| const condDoc = ext.spec.conditionFields?.find( | ||
| cf => | ||
| cf.name === propName && | ||
| (!cf.appliesToKinds || cf.appliesToKinds.includes(schemaItem.appliesToKind)) && | ||
| (!cf.appliesToInterfaceTypes || cf.appliesToInterfaceTypes.includes(schemaItem.appliesToInterfaceType)), | ||
| ); | ||
|
abhinavkdeval08-design marked this conversation as resolved.
Resolved
|
||
|
|
||
| const description = | ||
| condDoc?.description || | ||
| pSchema.description || | ||
| `Condition property ${propName}`; | ||
|
|
||
| fields.push({ | ||
| name: propName, | ||
| path: propName, | ||
| location: 'condition', | ||
| label: formatFieldLabel(propName), | ||
| description, | ||
| type: fieldType, | ||
| required: isRequired, | ||
| minimum: pSchema.minimum, | ||
| maximum: pSchema.maximum, | ||
| pattern: typeof pSchema.pattern === 'string' ? pSchema.pattern : undefined, | ||
| }); | ||
| } | ||
|
|
||
| result.push({ | ||
| extensionId: ext.metadata.id, | ||
| version: ext.metadata.version, | ||
| kind: schemaItem.appliesToKind, | ||
| kindDescription: kindObj?.description, | ||
| interfaceType: schemaItem.appliesToInterfaceType, | ||
| interfaceTypeDescription: ifaceTypeObj?.description, | ||
| schemaId: schemaItem.id, | ||
| fields, | ||
| conditionalRules, | ||
| }); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.