Skip to content

Commit

Permalink
fix(ui): Add helper function to cast string to array before yup valid…
Browse files Browse the repository at this point in the history
…ation (#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
  • Loading branch information
deadlycoconuts authored Aug 15, 2024
1 parent 1626d8c commit d328d9e
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions ui/src/settings/segmenters/components/form/validation/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,34 @@ const optionsSchema = yup
}
);

const validateArrayString = (arraySchema, arrayName) => yup
.string()
.test(
`${arrayName} valid JSON array`,
`${arrayName} must be a valid JSON array`,
(array) => {
if (array !== "") {
try {
var parsedArray = JSON.parse(array);
if (typeof parsedArray != "object" || !Array.isArray(parsedArray)) {
return false;
}
return arraySchema
.validateSync(parsedArray);
} catch (e) {
return false;
}
}
return true;
}
);

const constraintSchema = yup.object().shape({
pre_requisites: yup
.array(preRequisiteSchema)
pre_requisites: validateArrayString(yup.array(preRequisiteSchema), "Pre-requisites")
.typeError(
"Constraint pre-requisites must be a valid array of pre-requisite objects"
),
allowed_values: segmenterValuesSchema,
allowed_values: validateArrayString(segmenterValuesSchema, "Allowed values"),
options: optionsSchema,
});

Expand Down

0 comments on commit d328d9e

Please sign in to comment.