Skip to content

Commit d328d9e

Browse files
fix(ui): Add helper function to cast string to array before yup validation (#86)
* Add helper validation function to cast string to array before validating it against a given schema * Simplify arrow function * Fix incorrect typeof validation * Remove empty line
1 parent 1626d8c commit d328d9e

File tree

1 file changed

+24
-3
lines changed
  • ui/src/settings/segmenters/components/form/validation

1 file changed

+24
-3
lines changed

ui/src/settings/segmenters/components/form/validation/schema.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,34 @@ const optionsSchema = yup
5555
}
5656
);
5757

58+
const validateArrayString = (arraySchema, arrayName) => yup
59+
.string()
60+
.test(
61+
`${arrayName} valid JSON array`,
62+
`${arrayName} must be a valid JSON array`,
63+
(array) => {
64+
if (array !== "") {
65+
try {
66+
var parsedArray = JSON.parse(array);
67+
if (typeof parsedArray != "object" || !Array.isArray(parsedArray)) {
68+
return false;
69+
}
70+
return arraySchema
71+
.validateSync(parsedArray);
72+
} catch (e) {
73+
return false;
74+
}
75+
}
76+
return true;
77+
}
78+
);
79+
5880
const constraintSchema = yup.object().shape({
59-
pre_requisites: yup
60-
.array(preRequisiteSchema)
81+
pre_requisites: validateArrayString(yup.array(preRequisiteSchema), "Pre-requisites")
6182
.typeError(
6283
"Constraint pre-requisites must be a valid array of pre-requisite objects"
6384
),
64-
allowed_values: segmenterValuesSchema,
85+
allowed_values: validateArrayString(segmenterValuesSchema, "Allowed values"),
6586
options: optionsSchema,
6687
});
6788

0 commit comments

Comments
 (0)