11import type { Field } from './field/type'
22import type { JsfObjectSchema , JsfSchema , NonBooleanJsfSchema , ObjectValue , SchemaValue } from './types'
33import type { ValidationOptions } from './validation/schema'
4+ import { buildFieldSchema } from './field/schema'
45import { validateSchema } from './validation/schema'
56import { isObjectValue } from './validation/util'
67
7- /**
8- * Resets the visibility of all fields to true
9- * @param fields - The fields to reset
10- */
11- function resetVisibility ( fields : Field [ ] ) {
12- for ( const field of fields ) {
13- field . isVisible = true
14- if ( field . fields ) {
15- resetVisibility ( field . fields )
16- }
17- }
18- }
198/**
209 * Updates field visibility based on JSON schema conditional rules
2110 * @param fields - The fields to update
2211 * @param values - The current form values
2312 * @param schema - The JSON schema definition
2413 * @param options - Validation options
2514 */
26- export function updateFieldVisibility (
15+ export function mutateFields (
2716 fields : Field [ ] ,
2817 values : SchemaValue ,
2918 schema : JsfObjectSchema ,
@@ -33,9 +22,6 @@ export function updateFieldVisibility(
3322 return
3423 }
3524
36- // Reseting fields visibility to the default before re-calculating it
37- resetVisibility ( fields )
38-
3925 // Apply rules to current level of fields
4026 applySchemaRules ( fields , values , schema , options )
4127
@@ -125,21 +111,23 @@ function applySchemaRules(
125111 for ( const { rule, matches } of conditionalRules ) {
126112 // If the rule matches, process the then branch
127113 if ( matches && rule . then ) {
128- processBranch ( fields , rule . then )
114+ processBranch ( fields , values , rule . then , options )
129115 }
130116 // If the rule doesn't match, process the else branch
131117 else if ( ! matches && rule . else ) {
132- processBranch ( fields , rule . else )
118+ processBranch ( fields , values , rule . else , options )
133119 }
134120 }
135121}
136122
137123/**
138124 * Processes a branch of a conditional rule, updating the visibility of fields based on the branch's schema
139125 * @param fields - The fields to process
126+ * @param values - The current form values
140127 * @param branch - The branch (schema representing and then/else) to process
128+ * @param options - Validation options
141129 */
142- function processBranch ( fields : Field [ ] , branch : JsfSchema ) {
130+ function processBranch ( fields : Field [ ] , values : SchemaValue , branch : JsfSchema , options : ValidationOptions = { } ) {
143131 if ( branch . properties ) {
144132 // Cycle through each property in the schema and search for any (possibly nested)
145133 // fields that have a false boolean schema. If found, set the field's visibility to false
@@ -148,15 +136,26 @@ function processBranch(fields: Field[], branch: JsfSchema) {
148136 const field = fields . find ( e => e . name === fieldName )
149137 if ( field ) {
150138 if ( field ?. fields ) {
151- processBranch ( field . fields , fieldSchema )
139+ processBranch ( field . fields , values , fieldSchema )
152140 }
153141 else if ( fieldSchema === false ) {
154142 field . isVisible = false
155143 }
156144 else {
157- field . isVisible = true
145+ // If the field has properties being declared on this branch, we need to update the field
146+ // with the new properties
147+ const newField = buildFieldSchema ( fieldSchema as JsfObjectSchema , fieldName , true )
148+ for ( const key in newField ) {
149+ // We don't want to override the type property
150+ if ( ! [ 'type' ] . includes ( key ) ) {
151+ field [ key ] = newField [ key ]
152+ }
153+ }
158154 }
159155 }
160156 }
161157 }
158+
159+ // Apply rules to the branch
160+ applySchemaRules ( fields , values , branch as JsfObjectSchema , options )
162161}
0 commit comments