jquense / yup

Dead simple Object schema validation
MIT License
22.93k stars 934 forks source link

Custom objet validation with early : false failed #2241

Open Syfele opened 3 months ago

Syfele commented 3 months ago

I have an object to validate that I build in several steps (step 1, step 2 ...) and in one of the steps there is drag and drop on several dynamic elements and forms in modals which make the use of classic forms very difficult.

All this to say that I validate an object and not a form

my object is built well with empty fields and it is filled if the user does actions ....

at the time of validation I use { abortEarly: false } (to have all the error messages) but unfortunately I only receive the first error message, and I have to correct what's wrong to get the next error message

I read @jquense's answers everywhere where he explains that abortEarly does not mean to execute the validations in parallel and return the result as soon as possible, but rather to wait for the validation to be finished to have a result

When I click on my button, my object is ready for validation and I do not know what to do to display all the messages to the user at once

Help Others Reproduce It is too hard to create a sandbox as I need to copy many components to work correctly, as I said there is a drag and drop events and modal forms

Here is my validation callback

const validateSecondStep = () => {
    yupSecondStepSchema.validate(object)
        .then((v) => {console.log(object);}, {abortEarly: false})
        .catch((err) => {console.log(err.errors);})
}

And here is my yupSecondStepSchema

const yupSecondStepSchema = Yup.object().shape({
    days: Yup.array(
        Yup.object({
            sessions: Yup.array(
                Yup.object({
                    hour: Yup.string().required(),
                    sets: Yup.array(
                        Yup.object({
                            label: Yup.object().required(),
                            reference: Yup.string().required(),
                            value: Yup.string().required()
                        })
                    ).min(1) // a session must have at least one set
                })
            )
        })
    )
});
)

I confirm that the first time I get

['days[0].sessions[0].hour is a required field']

I must set hour to session to get

['days[0].sessions[0].sets field must have at least 1 items']

My Object structure :

{
    days: [
        {
            dayNumber: 1,
            sessions: [
                {
                    hour: null,
                    sets: [
                        {
                            label: string,
                            reference: string,
                            value: number
                        }
                    ]
                }
            ]
        }
    ]
}

Any help will be a pleasure

Syfele commented 3 months ago

No one has anything to tell ?

jquense commented 2 months ago

I don't really know what you are asking about here, there isn't really a specific issue demonstrated that I can tell sorry. Schema look fine if your getting different results it might be due to the code calling the schema