Yup: Validating nested schema date objects

Created on 13 Jul 2019  路  7Comments  路  Source: jquense/yup

Is there support for validating child property dates within an object field?

I can't quite get the below example to work properly. The val in the is condition of startDate.day is always undefined so I get an error like endDate.day field must be later than undefined.

Not sure if I can access properties in the when clause such as obj.property.

startDate: yup.object().shape({
    day: yup
        .date()
        .required()
        .when("endDate.day", {
            is: val => val !== null, // val is always undefined
            then: schema => schema.max(yup.ref("endDate.day")) // Also undefined
        }),
    time: yup.date().required()
    }),
endDate: yup.object().shape({
    day: yup
        .date()
        .min(yup.ref("startDate.day")) // Undefined
        .required(),
    time: yup.date().required()
})

All 7 comments

Struggling with this too!

I'm also struggling with a weird bug where only the date fields in an object come through as undefined in .test(), maybe related? Did you find a workaround?

@timstallmann I unfortunately did not with my original post. Ended up going a different route outside of the yup schema definition with comparison logic.

You're only finding the date fields undefined within the .test() function?

Nope they're coming through as undefined all the way through the Yup pipeline as far as I can tell. I tried both Yup.date() and adding the custom schema validator that's mentioned in the docs using moment. So strange though!

I'm struggling in a similar scenario. I have an object type field and i need to ref the sibling of this field, but from its child. I don't know if this is possible and the docs doesn't explains too much about how to use ref. This equalTo function is from here https://github.com/jaredpalmer/formik/issues/90

originport: Yup.object().shape({
                id: Yup.number().test(
                    "originport",
                    "Campo obrigat贸rio",
                    function test(value) {
                        return !!value
                    }
                )
            }),
destinationport: Yup.object().shape({
                id: Yup.number()
                    .equalTo(Yup.ref("originport.id"), "O destino precisa ser diferente da origem")
                    .test(
                        "destinationport",
                        "Campo obrigat贸rio",
                         value => !!value

                    )
            })

Did anyone manage to do this? I have a nested array of objects that need to validation against one of the top-most fields, which is a date, and manipulate that date (i.e. adding/subtracting days) which obviously requires a ref to be resolved.

Any news about? 馃槩
why @jquense closed this?

Was this page helpful?
0 / 5 - 0 ratings