diff --git a/src/components/fieldsDisplayForm/utils/schemas.js b/src/components/fieldsDisplayForm/utils/schemas.js index 3ec954c399767d7a76767195bbf8c8283541d099..ce0fffa44b33e26117793208ed532914375add9b 100644 --- a/src/components/fieldsDisplayForm/utils/schemas.js +++ b/src/components/fieldsDisplayForm/utils/schemas.js @@ -1,42 +1,23 @@ import * as Yup from "yup"; /** The validation through Yup is schema based, so there are schemas and it's validations. */ -/** Schema to validate the question field from every form array object. */ -const questionTextSchema = Yup.string() - .required("Este campo é obrigatório!") - .test("alphabets", "O caractere não é permitido", (value) => { - return /^[A-Za-z!?$%,. 1234567890àèìòùáéíóúâêîôûãõç]+$/.test(value); - }); +/**Function that compares the value with a regex, to assure the input is valid */ +function checkText(value) { + return /^[A-Za-z!?$%,. 1234567890àèìòùáéíóúâêîôûãõç]+$/.test(value); +} /** Function that applies the validation of it's used schema and sets the error messages. */ -export async function testQuestionTextSchema(form, value, index) { - await questionTextSchema - .validate(value) - .then((x) => { - form[index].error.errorMsg.question = ""; - }) - .catch((err) => { - form[index].error.errorMsg.question = err.message; - }); +export async function testQuestionTextSchema(error, value) { + value + ? checkText(value) + ? (error.errorMsg.question = "") + : (error.errorMsg.question = "O caractere não é permitido") + : (error.errorMsg.question = "Este campo é obrigatório!"); } -/** Schema to validate the description field from every form array object. */ -const descriptionTextSchema = Yup.string().test( - "alphabets", - "O caractere não é permitido", - (value) => { - if (!value) return true; - return /^[A-Za-z!?$%,. 1234567890àèìòùáéíóúâêîôûãõ\b- ]+$/.test(value); - } -); /** Function that applies the validation of it's used schema and sets the error messages. */ -export async function testDescriptionTextSchema(form, value, index) { - await descriptionTextSchema - .validate(value) - .then((x) => { - form[index].error.errorMsg.description = ""; - }) - .catch((err) => { - form[index].error.errorMsg.description = err.message; - }); +export async function testDescriptionTextSchema(error, value) { + value && checkText(value) + ? (error.errorMsg.description = "") + : (error.errorMsg.description = "O caractere não é permitido"); } /** Schema to validate the number of options at options field from FormFieldSelect, FormFieldCheckbox and FormFieldRadio. */ const selectOptionsSchema = Yup.array() @@ -87,23 +68,13 @@ export async function testSubformSchema(form, index) { form[index].error.errorMsg.subformUsage = err.message; }); } -/** Schema to validate the content of the options field from FormFieldSelect, FormFieldCheckbox and FormFieldRadio. */ -const optSchema = Yup.string() - .required("Por favor, preencha esta opção") - .test("alphabets", "O caractere não é permitido", (value) => { - if (!value) return true; - return /^[A-Za-z!?$%,. àèìòùáéíóúâêîôûãõ\b- ]+$/.test(value); - }); /** Function that applies the validation of it's used schema and sets the error messages. */ -export async function selectOptionTextTesting(form, value, index, idopt) { - await optSchema - .validate(value) - .then((x) => { - form[index].error.errorMsg.options[idopt] = ""; - }) - .catch((err) => { - form[index].error.errorMsg.options[idopt] = err.message; - }); +export async function selectOptionTextTesting(error, value, idopt) { + value + ? checkText(value) + ? (error.errorMsg.options[idopt] = "") + : (error.errorMsg.options[idopt] = "O caractere não é permitido") + : (error.errorMsg.options[idopt] = "Por favor, preencha esta opção"); } /** Schema to validate the quantity field of the validation from FormFieldText. */ const textValidationSchema = Yup.string() diff --git a/src/contexts/useForm.js b/src/contexts/useForm.js index e349fad3fc2e407aacbec311e366c97e3d5fe8f7..3d0bdbf40bd6b633b90e920715ad7d6935a30168 100644 --- a/src/contexts/useForm.js +++ b/src/contexts/useForm.js @@ -138,7 +138,7 @@ const useForm = () => { */ async function setQuestionField(value, index) { form[index].question = value; - await testQuestionTextSchema(form, value, index); + testQuestionTextSchema(form[index].error, value); setForm([...form]); } @@ -148,7 +148,7 @@ const useForm = () => { */ async function setDescriptionField(value, index) { form[index].description = value; - await testDescriptionTextSchema(form, value, index); + testDescriptionTextSchema(form[index].error, value); setForm([...form]); } @@ -157,9 +157,9 @@ const useForm = () => { * @param index - the position on the array that the operation needs to be done; * @param idopt - the id of the options being changed, inside the form[index]. */ - async function setSelectOption(value, index, idopt) { + function setSelectOption(value, index, idopt) { form[index].options[idopt] = value; - await selectOptionTextTesting(form, value, index, idopt); + selectOptionTextTesting(form[index].error, value, idopt); setForm([...form]); }