Hi All,
I have some special scenario that i need to handle.
We build our form dynamically base on data we get from the backend.
There is an object i get which looks like this:
{name: {
maxLength: 255,
minLength: 1,
type: string
}
}
So lets say i want to build a validation schema for this, it will be something like this(pseudo code):
Yup.string().max(maxLength).min(minLength);
The problem is that i need to parse the object and then merge the restrictions somehow.
the following doesn't work.
let yupValue = {};
switch(reqType) {
case 'maxLength':
yupValue[formField] = Yup.string()
.max(5, `Max of ${requirements[reqType]} characters`);
yup = yup.shape(yupValue);
break;
case 'minLength':
yupValue[formField] = Yup.string()
.min(3, `Min of ${requirements[reqType]} characters`);
yup = yup.shape(yupValue);
break;
default:
break;
}
where yup is let yup = Yup.object();
How can i merge it?
You can close the issue.
Hi @ceilat, I have the same problem. Did you find a solution you can share?
@DirkWolthuis try this one
const retypePasswordFields = {
newPassword: Yup.string()
.min(8, min)
.required(required),
reNewPassword: Yup.string()
.min(8, min)
.required(required)
.when('newPassword', {
is: (val: string) => (val && val.length > 0),
then: Yup.string().oneOf(
[Yup.ref('newPassword')],
retypePassword
),
}),
};
export const RetypePasswordScheme = Yup.object().shape(retypePasswordFields);
export const ChangePasswordScheme = Yup.object().shape({
currentPassword: Yup.string()
.min(8, min)
.required(required),
...retypePasswordFields,
});
Most helpful comment
Hi @ceilat, I have the same problem. Did you find a solution you can share?