Tuesday, 27 September 2022

Custom validation for different max date values in yup form

Is it possible using Yup validaion to have two different max conditions for one validation object? So currently I have this code:

yup.array().of(yup.object().shape({
                        dateToCheck: yup
                            .date()
                            .min(minDatetoCheck, 'Cant be below this date')
                            .max(maxDatetoCheck, 'Cant be above this date'))
                            .required()
                    })
                )
                .when('initialValue', {
                    is: true,
                    then: yup.array().min(1)
                })

So I wanted to add extra check so that any date that input with year above 9999 (so like years with 5 numbers and more) should be treated as 'Invalid date format' . I tried this:

        .date()
        .min(minDatetoCheck, 'Cant be below this date')
        .max(maxDatetoCheck, 'Cant be above this date')
        .max(9999, 'Invalid date format')
        .required()

However it is not working. Maybe is there a way to setup specific custom date form in .date() method that is adhering to 4 number long years only? Because the default year format allows for 5 number year long years and it is not something I need.



from Custom validation for different max date values in yup form

No comments:

Post a Comment