1
1
import type { Field } from './field/type'
2
2
import type { JsfObjectSchema , JsfSchema , NonBooleanJsfSchema , ObjectValue , SchemaValue } from './types'
3
3
import type { ValidationOptions } from './validation/schema'
4
+ import { buildFieldSchema } from './field/schema'
4
5
import { validateSchema } from './validation/schema'
5
6
import { isObjectValue } from './validation/util'
6
7
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
- }
19
8
/**
20
9
* Updates field visibility based on JSON schema conditional rules
21
10
* @param fields - The fields to update
22
11
* @param values - The current form values
23
12
* @param schema - The JSON schema definition
24
13
* @param options - Validation options
25
14
*/
26
- export function updateFieldVisibility (
15
+ export function mutateFields (
27
16
fields : Field [ ] ,
28
17
values : SchemaValue ,
29
18
schema : JsfObjectSchema ,
@@ -33,9 +22,6 @@ export function updateFieldVisibility(
33
22
return
34
23
}
35
24
36
- // Reseting fields visibility to the default before re-calculating it
37
- resetVisibility ( fields )
38
-
39
25
// Apply rules to current level of fields
40
26
applySchemaRules ( fields , values , schema , options )
41
27
@@ -125,21 +111,23 @@ function applySchemaRules(
125
111
for ( const { rule, matches } of conditionalRules ) {
126
112
// If the rule matches, process the then branch
127
113
if ( matches && rule . then ) {
128
- processBranch ( fields , rule . then )
114
+ processBranch ( fields , values , rule . then , options )
129
115
}
130
116
// If the rule doesn't match, process the else branch
131
117
else if ( ! matches && rule . else ) {
132
- processBranch ( fields , rule . else )
118
+ processBranch ( fields , values , rule . else , options )
133
119
}
134
120
}
135
121
}
136
122
137
123
/**
138
124
* Processes a branch of a conditional rule, updating the visibility of fields based on the branch's schema
139
125
* @param fields - The fields to process
126
+ * @param values - The current form values
140
127
* @param branch - The branch (schema representing and then/else) to process
128
+ * @param options - Validation options
141
129
*/
142
- function processBranch ( fields : Field [ ] , branch : JsfSchema ) {
130
+ function processBranch ( fields : Field [ ] , values : SchemaValue , branch : JsfSchema , options : ValidationOptions = { } ) {
143
131
if ( branch . properties ) {
144
132
// Cycle through each property in the schema and search for any (possibly nested)
145
133
// 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) {
148
136
const field = fields . find ( e => e . name === fieldName )
149
137
if ( field ) {
150
138
if ( field ?. fields ) {
151
- processBranch ( field . fields , fieldSchema )
139
+ processBranch ( field . fields , values , fieldSchema )
152
140
}
153
141
else if ( fieldSchema === false ) {
154
142
field . isVisible = false
155
143
}
156
144
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
+ }
158
154
}
159
155
}
160
156
}
161
157
}
158
+
159
+ // Apply rules to the branch
160
+ applySchemaRules ( fields , values , branch as JsfObjectSchema , options )
162
161
}
0 commit comments