jquense / yup

Dead simple Object schema validation
MIT License
22.94k stars 935 forks source link

The total sum of the four inputs equals 100 #2153

Closed khalilbenachir closed 9 months ago

khalilbenachir commented 11 months ago

I apologize for using this platform to pose my question, but after searching in various places, I couldn't find a solution. I have a schema as follows:

Yup.object().shape({
    input1: Yup.number().required(),
    input2: Yup.number().required(),
    input3: Yup.number().required(),
    input4: Yup.number().required(),
})

I want to ensure that the sum of the fourth inputs is 100. Has anyone encountered and resolved this issue before?

Rangoric commented 10 months ago

Not sure exactly how to do this, but the Schema.test function added to one of them should allow you to check if all of them add up to 100. There is a context passed to it that you can use to get the other values then do the test.

jquense commented 9 months ago
Yup.object().shape({
    input1: Yup.number().required(),
    input2: Yup.number().required(),
    input3: Yup.number().required(),
    input4: Yup.number().required(),
}).test((value) => /*do the check here */)