Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

yup.validate only returns single field in errors array

I am trying to add yup in an angular7 project. I have added it successfully to my component.

  readonly optionalRequiredSchema = yup.object().shape({
    ['@name']: yup.string().required(),
    description: yup.string().required(),
    ['rule-type']: yup.string().required(),
    from: yup.object().required(),
    source: yup.object().required(),
    to: yup.object().required(),
    destination: yup.object().required(),
    service: yup.object().required(),
    application: yup.object().required(),
    action: yup.string().required()
  });

I am validating like this

  validateData() {
    this.optionalRequiredSchema.validate(this.allData)
      .then(
        (data) => console.log(data)
      ).catch(err => {
        console.log(err);
        this.showToast('top-right', 'danger', JSON.stringify(err.errors))


      })
  }

If i pass all empty object to validate it only give 1 field in errors array. when i correc that than it shows next 1 field. how can i get list of all invalid fields.

Thanks

like image 615
raju Avatar asked Sep 01 '26 23:09

raju


1 Answers

Yup validate function accept second argument options. Pass {abortEarly: false} will fix your issue

  validateData() {
    this.optionalRequiredSchema.validate(this.allData, {abortEarly: false})
      .then(
        (data) => console.log(data)
      ).catch(err => {
        console.log(err);
        this.showToast('top-right', 'danger', JSON.stringify(err.errors))


      })
  }
like image 191
aseferov Avatar answered Sep 04 '26 14:09

aseferov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!