remove eval funciton in form for CSP fix
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
// validationUtils.js
|
||||
|
||||
// Validation functions
|
||||
const required = (value, requiredMessage, emojiNotAllowedMessage) => {
|
||||
let errors;
|
||||
if (!value) {
|
||||
errors = requiredMessage;
|
||||
} else {
|
||||
const emojiRegex =
|
||||
/[\u{1F600}-\u{1F64F}\u{1F300}-\u{1F5FF}\u{1F680}-\u{1F6FF}\u{1F700}-\u{1F77F}\u{1F780}-\u{1F7FF}\u{1F800}-\u{1F8FF}\u{1F900}-\u{1F9FF}\u{1FA00}-\u{1FA6F}\u{1FA70}-\u{1FAFF}\u{2600}-\u{26FF}\u{2700}-\u{27BF}\u{2300}-\u{23FF}\u{2B50}\u{1F004}-\u{1F0CF}\u{1F0A0}-\u{1F0A5}\u{1F170}-\u{1F251}]/gu;
|
||||
|
||||
if (emojiRegex.test(value)) {
|
||||
errors = emojiNotAllowedMessage;
|
||||
}
|
||||
}
|
||||
return errors;
|
||||
};
|
||||
|
||||
const postcode = (value, invalidPostcodeMessage) =>
|
||||
value &&
|
||||
!/^([A-Z][A-HJ-Y]?[0-9][A-Z0-9]? ?[0-9][A-Z]{2}|GIR ?0A{2})$/i.test(value)
|
||||
? "Invalid postcode"
|
||||
: undefined;
|
||||
|
||||
// Function to parse the validation string
|
||||
export const parseValidationString = (validationString) => {
|
||||
if (typeof validationString !== "string") {
|
||||
return []; // Return an empty array if it's not a string
|
||||
}
|
||||
|
||||
// Remove square brackets and split by commas
|
||||
return validationString
|
||||
.replace(/[\[\]']+/g, "") // Remove square brackets
|
||||
.split(",") // Split by commas
|
||||
.map((rule) => rule.trim()); // Trim each validation rule
|
||||
};
|
||||
|
||||
// Function to handle multiple validation rules
|
||||
export const validateField = (
|
||||
value,
|
||||
validationString,
|
||||
requiredMessage,
|
||||
emojiNotAllowedMessage,
|
||||
invalidPostcodeMessage
|
||||
) => {
|
||||
const validations = parseValidationString(validationString); // Parse the validation string
|
||||
let errors;
|
||||
|
||||
console.log(typeof validations, validations);
|
||||
for (let validationRule of validations) {
|
||||
const validationFunc = validationFunctions[validationRule];
|
||||
if (validationFunc) {
|
||||
errors = validationFunc(
|
||||
value,
|
||||
requiredMessage,
|
||||
emojiNotAllowedMessage,
|
||||
invalidPostcodeMessage
|
||||
);
|
||||
}
|
||||
if (errors) break; // Exit if any validation fails
|
||||
}
|
||||
|
||||
return errors;
|
||||
};
|
||||
|
||||
// Map validation strings to actual validation functions
|
||||
export const validationFunctions = {
|
||||
required,
|
||||
postcode,
|
||||
// Add other validation functions as needed
|
||||
};
|
||||
Reference in New Issue
Block a user