Can anyone provide insight into why this validation issue is occurring? I am implementing a schema using yup to ensure that the data within an array are strings. Below is the yup schema I am working with:
signUpSchema: async (req, res, next) => {
const signUpSchema = yup.object().shape({
username: yup
.string()
.required('name is required')
.typeError('name must be a string'),
email: yup
.string()
.email('email must be a valid email')
.required('email is required')
.typeError('email must be a string'),
...
locationTracking: yup
.array()
.of(yup.string())
.required('locationTracking is required')
.typeError('locationTracking must be an array')
.min(1, 'locationTracking must have at least one location'),
});
try {
await signUpSchema.validate(req.body);
next();
} catch (error) {
next(error);
}
},
I have also tried incorporating the required and typeError methods in my code:
locationTracking: yup
.array()
.of(yup.string().require().typeError('data must be strings'))
.required('locationTracking is required')
.typeError('locationTracking must be an array')
.min(1, 'locationTracking must have at least one location'),
});
While the schema states that locationTracking should be an array, it does not specifically enforce that the elements must be strings. This results in numbers, booleans, and other data types passing the validation process. I am unsure of what mistake I may be making and have been unable to find a solution online.
The data being validated is from req.body sent as JSON via postman. I initially suspected some form of type coercion, but upon inspection, the data types returned are numbers, booleans, etc., managing to bypass my validation checks.