Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate one field against another with Yup?

Tags:

reactjs

yup

I'm setting up a form with two fields; start month and end month.

Start month and end month are just represented by integers (0-11) for the corresponding months. I need validation to ensure that end month comes after start month (ie the integer for end month is bigger).

I've looked at other similar issues from a few years back, but yup seems to have updated to render them useless. I've tried the below code with a few variations.

I am also having difficulty validating end month as a number (.number() - I'm assuming I have to maybe to do this within the test function.

let schema = yup.object().shape({
  startMonth: yup
    .number()
    .required()
    .positive()
    .integer(),
  endMonth: Yup.string().test(
  "End Month Validation",
  "error message",
  value => {
    return value > startMonth;
  }
)
.number()
.required()
.positive()
.integer(),
});

Errors: Line 102: 'startMonth' is not defined no-undef

like image 801
colin Avatar asked Jun 20 '19 23:06

colin


People also ask

How do I compare two fields in Yup validation?

Solution: const yup = require('yup') const { setLocale } = yup setLocale({ mixed: { notType: 'the ${path} is obligatory', required: 'the field ${path} is obligatory', oneOf: 'the field ${path} must have one of the following values: ${values}' } }) const myNameSchema = yup. object(). shape({ first_name: yup.

How do you validate field only exists?

You can set a additional boolean key where value is default false. Change it to true when you modify the value in step 1. And then if the value is true for that key then apply the validation.

What is schema in Yup?

Yup is a JavaScript schema builder for value parsing and validation. Define a schema, transform a value to match, validate the shape of an existing value, or both. Yup schema are extremely expressive and allow modeling complex, interdependent validations, or value transformations.

What is shape in Yup?

yupobject documentation. Basically passing the schema to shape is just the long-form of passing it to the object constructor. Shape does however offer the benefit of chaining and overloading definitions in later chained methods. See yup.shape documentation. Follow this answer to receive notifications.


1 Answers

Another approach would be to make use of .ref() and .moreThan() to perform this validation logic.

Something like the following should achieve what you require:

let schema = Yup.object().shape({
  startMonth: Yup
    .number()
    .required()
    .positive()
    .integer(),
  endMonth: Yup.number() /* Remove .string() */
    .required()
    .positive()
    /* Reference startMonth field in validating endMonth value */
    .moreThan(Yup.ref('startMonth'), "End month must come after start month")
    .integer(),
});

schema.validate({ startMonth : 1, endMonth : 2 })  // Okay!
schema.validate({ startMonth : 11, endMonth : 2 }) // Throws exception

Hope that helps!

like image 101
Dacre Denny Avatar answered Sep 17 '22 13:09

Dacre Denny