@@ -16,10 +16,6 @@ import { buildYupSchema } from './yupSchema';
1616 * @returns {Object } An object containing:
1717 * - scopes {Map} - A Map of the validation scopes (with IDs as keys)
1818 * - getScope {Function} - Function to retrieve a scope by name/ID
19- * - validate {Function} - Function to evaluate a validation rule
20- * - applyValidationRuleInCondition {Function} - Evaluate a validation rule used in a condition
21- * - applyComputedValueInField {Function} - Evaluate a computed value rule for a field
22- * - applyComputedValueRuleInCondition {Function} - Evaluate a computed value rule used in a condition
2319 */
2420export function createValidationChecker ( schema ) {
2521 const scopes = new Map ( ) ;
@@ -50,6 +46,21 @@ export function createValidationChecker(schema) {
5046 } ;
5147}
5248
49+ /**
50+ * Creates a validation scope object for a schema.
51+ *
52+ * Builds maps of validations and computed values defined in the schema's
53+ * x-jsf-logic section. Includes functions to evaluate the rules.
54+ *
55+ * @param {Object } schema - The JSON schema
56+ * @returns {Object } The validation scope object containing:
57+ * - validationMap - Map of validation rules
58+ * - computedValuesMap - Map of computed value rules
59+ * - validate {Function} - Function to evaluate a validation rule
60+ * - applyValidationRuleInCondition {Function} - Evaluate a validation rule used in a condition
61+ * - applyComputedValueInField {Function} - Evaluate a computed value rule for a field
62+ * - applyComputedValueRuleInCondition {Function} - Evaluate a computed value rule used in a condition
63+ */
5364function createValidationsScope ( schema ) {
5465 const validationMap = new Map ( ) ;
5566 const computedValuesMap = new Map ( ) ;
@@ -162,6 +173,20 @@ export function yupSchemaWithCustomJSONLogic({ field, logic, config, id }) {
162173
163174const HANDLEBARS_REGEX = / \{ \{ ( [ ^ { } ] + ) \} \} / g;
164175
176+ /**
177+ * Replaces Handlebars templates in a value with computed values.
178+ *
179+ * Handles recursively replacing Handlebars templates "{{var}}" in strings
180+ * with computed values looked up from the validation logic.
181+ *
182+ * @param {Object } options - Options object
183+ * @param {* } options.value - The value to replace templates in
184+ * @param {Object } options.logic - The validation logic object
185+ * @param {Object } options.formValues - The current form values
186+ * @param {string } options.parentID - The ID of the validation scope
187+ * @param {string } options.name - The name of the field
188+ * @returns {* } The value with templates replaced with computed values
189+ */
165190function replaceHandlebarsTemplates ( {
166191 value : toReplace ,
167192 logic,
@@ -397,8 +422,18 @@ function throwIfUnknownOperator(operator, subRule, id) {
397422 }
398423}
399424
425+ const regexToGetIndices = / \. \d + \. / g; // eg. .0., .10.
426+
427+ /**
428+ * Removes array indices from a json schema path string.
429+ * Converts paths like "foo.0.bar" to "foo.bar".
430+ * This allows checking if a variable exists in an array item schema without needing the specific index.
431+ *
432+ * @param {string } path - The json schema path potentially containing array indices
433+ * @returns {string } The path with array indices removed
434+ */
400435function removeIndicesFromPath ( path ) {
401- const intermediatePath = path . replace ( / \. \d + \. / g , '.' ) ;
436+ const intermediatePath = path . replace ( regexToGetIndices , '.' ) ;
402437 return intermediatePath . replace ( / \. \d + $ / , '' ) ;
403438}
404439
0 commit comments