|
1 | 1 | import jsonLogic from 'json-logic-js'; |
2 | 2 |
|
| 3 | +import { buildYupSchema } from './yupSchema'; |
| 4 | + |
3 | 5 | /** |
4 | 6 | * Parses the JSON schema to extract the json-logic rules and returns an object |
5 | 7 | * containing the validation scopes, functions to retrieve the scopes, and evaluate the |
@@ -123,26 +125,81 @@ export function yupSchemaWithCustomJSONLogic({ field, logic, config, id }) { |
123 | 125 | ); |
124 | 126 | } |
125 | 127 |
|
| 128 | +const HANDLEBARS_REGEX = /\{\{([^{}]+)\}\}/g; |
| 129 | + |
| 130 | +function replaceHandlebarsTemplates({ |
| 131 | + value: toReplace, |
| 132 | + logic, |
| 133 | + formValues, |
| 134 | + parentID, |
| 135 | + name: fieldName, |
| 136 | +}) { |
| 137 | + if (typeof toReplace === 'string') { |
| 138 | + return toReplace.replace(HANDLEBARS_REGEX, (match, key) => { |
| 139 | + return logic.getScope(parentID).applyComputedValueInField(key.trim(), formValues, fieldName); |
| 140 | + }); |
| 141 | + } |
| 142 | + return toReplace; |
| 143 | +} |
| 144 | + |
126 | 145 | export function calculateComputedAttributes(fieldParams, { parentID = 'root' } = {}) { |
127 | | - return ({ logic, formValues }) => { |
| 146 | + return ({ logic, isRequired, config, formValues }) => { |
128 | 147 | const { computedAttributes } = fieldParams; |
129 | 148 | const attributes = Object.fromEntries( |
130 | 149 | Object.entries(computedAttributes) |
131 | 150 | .map(handleComputedAttribute(logic, formValues, parentID)) |
132 | 151 | .filter(([, value]) => value !== null) |
133 | 152 | ); |
134 | 153 |
|
135 | | - return attributes; |
| 154 | + return { |
| 155 | + ...attributes, |
| 156 | + schema: buildYupSchema( |
| 157 | + { ...fieldParams, ...attributes, required: isRequired }, |
| 158 | + config, |
| 159 | + logic |
| 160 | + ), |
| 161 | + }; |
136 | 162 | }; |
137 | 163 | } |
138 | 164 |
|
139 | 165 | function handleComputedAttribute(logic, formValues, parentID) { |
140 | 166 | return ([key, value]) => { |
141 | | - if (key === 'const') |
| 167 | + if (key === 'description') { |
| 168 | + return [key, replaceHandlebarsTemplates({ value, logic, formValues, parentID, name })]; |
| 169 | + } |
| 170 | + |
| 171 | + if (key === 'title') { |
| 172 | + return ['label', replaceHandlebarsTemplates({ value, logic, formValues, parentID, name })]; |
| 173 | + } |
| 174 | + |
| 175 | + if (key === 'const') { |
142 | 176 | return [key, logic.getScope(parentID).applyComputedValueInField(value, formValues)]; |
| 177 | + } |
| 178 | + |
| 179 | + if (key === 'x-jsf-errorMessage') { |
| 180 | + return [ |
| 181 | + 'errorMessage', |
| 182 | + handleNestedObjectForComputedValues(value, formValues, parentID, logic, name), |
| 183 | + ]; |
| 184 | + } |
143 | 185 |
|
144 | 186 | if (typeof value === 'string') { |
145 | 187 | return [key, logic.getScope(parentID).applyComputedValueInField(value, formValues)]; |
146 | 188 | } |
| 189 | + |
| 190 | + if (key === 'x-jsf-presentation' && value.statement) { |
| 191 | + return [ |
| 192 | + 'statement', |
| 193 | + handleNestedObjectForComputedValues(value.statement, formValues, parentID, logic, name), |
| 194 | + ]; |
| 195 | + } |
147 | 196 | }; |
148 | 197 | } |
| 198 | + |
| 199 | +function handleNestedObjectForComputedValues(values, formValues, parentID, logic, name) { |
| 200 | + return Object.fromEntries( |
| 201 | + Object.entries(values).map(([key, value]) => { |
| 202 | + return [key, replaceHandlebarsTemplates({ value, logic, formValues, parentID, name })]; |
| 203 | + }) |
| 204 | + ); |
| 205 | +} |
0 commit comments