How to compare 2 values with YUP

Asked

Viewed 188 times

1

Good morning I have a validation to do with Yup, and I need to make sure the initial interval is not longer than the final interval, anyone have any idea how to do that? I tried to pass the same variables in both fields of the schema, but it didn’t work.Follow the code:

const schema = yup.object().shape({
intervalo_inicial: yup.number().test('len',
  'Valor deve ser maior que 0', v => v > 0.00 && v < 100.00).required(),
intervalo_final: yup.number().test('len',
  'Valor deve ser menor que 100', v1 => v1 < 100.00 && v1 > 0.00).required(),
});```

1 answer

0

You can use the .ref() and .moreThan():

let schema = Yup.object().shape({
  intervalo_inicial: Yup
    .number()
    .required()
    .positive()
    .integer(),
  intervalo_final: Yup.number()
    .required()
    .positive()
    .moreThan(Yup.ref('intervalo_inicial'), "Intervalo final deve ser maior que o inicial")
    .integer(),
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.