I'm new to using the Yup validation schema method and I was wondering how I could compare two object to check that one of their respective attributes are different.
For example, I have the following code:
export default Yup.object().shape({
personA: Yup.object().shape({
id: Yup.number()
.typeError("Must be a valid number")
.required("Required field")
}),
personB: Yup.object().shape({
id: Yup.number()
.typeError("Must be a valid number")
.required("Required field")
})
});
In this case, how can I check that the objects personA and personB do not have the same id ?
Thank you for reading :D
yup.object().shape({
object1: yup
.object({
attribute1: yup.string().required(),
attribute2: yup.string().required(),
})
.required(),
object2: yup
.object({
attribute1: yup.string().required(),
attribute2: yup.string().required(),
})
.test('different', 'Attribute values must be different', function (value) {
const object1 = this.parent.object1 || {};
return value.attribute1 !== object1.attribute1 || value.attribute2 !== object1.attribute2;
})
.required(),
});