Yup: Errors as object instead of array

Created on 11 Sep 2017  路  6Comments  路  Source: jquense/yup

Hey is there currently a way to get errors as an Object keyed by the property names?

In other words:

const schema = yup.object().shape({
  name: yup.string(),
  age: yup.string().min(5),
})

schema.validate({ name: 'jimmy', msg: 'hi' })
  .catch(function(err){
    err.name   // 'ValidationError'
    err.errors // => {msg: 'msg must be at least 5 characters'} instead of ['msg must be at least 5 characters']
  })

Most helpful comment

Not sure what you mean there... If you don't know what you want what are you asking for?

All 6 comments

No, but you can transform the array to whatever object you want with fairly minimal effort

In this case I don't think I could transform the array since I won't know which property each error belongs to 馃槙

Not sure what you mean there... If you don't know what you want what are you asking for?

What I meant was just based on a error message (eg. msg must be at least 5 characters) it might to difficult to figure out what property it belongs to (msg) for all cases, especially its a customized error message.

sure! That's why ValidationError has an inner property that contains all full error object for every error thrown. errors is sort of a legacy property at this point

Ah i see now, yup yup inner is what i was looking for, thanks!

Was this page helpful?
0 / 5 - 0 ratings