So I am building a data collection software that relies heavily on forms and form validations. I have currently built up dynamic forms with Formik and Yup with individual validation but im looking to dive deeper into validating the users current input agaisnt a previous input to make sure the value of the same key from the objects are incrementing or decrementing based on the form settings.
const ObjOne =
{
NumberOne: 100,
NumberTwo: 200,
NumberThree: 300,
}
const ObjTwo =
{
NumberOne: 150,
NumberTwo: 250,
NumberThree: 350,
}
I want to setup some sort of system to compare these two objects to validate that the users current submission for NumberOne in Object Two is properly incrementing higher than the previous value in Object One. If said value is not incrementing then it would return an error telling the user the data is not valid.
If someone could help me brainstorm on how to acheive this or point me in the proper direction i would greatly appreciate it.
With your example, you can do a basic function which compare NumberOne values:
const ObjOne =
{
NumberOne: 100,
NumberTwo: 200,
NumberThree: 300,
}
const ObjTwo =
{
NumberOne: 150,
NumberTwo: 250,
NumberThree: 350,
}
const check = (one, two) => {
if (two.NumberOne > one.NumberOne)
console.log('ok');
else
console.log('ko');
}
check(ObjOne, ObjTwo);
In this example, only NumberOne values are compared but you can do a more complicated function to check all keys. This example just answer to your specific question.
This does the job for the given example:
const check = (one, two) => {
if (two.NumberOne < one.NumberOne || two.NumberTwo < one.NumberTwo || two.NumberThree < one.NumberThree)
console.log('ko');
else
console.log('ok');
}