You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
and a context isn't provided with schema.validate(), an error will occur:
TypeError: Cannot read property 'other' of undefined
Is there a way to avoid this error, without passing a context into the validate call?
A couple ideas:
Set a default context at the schema level
Allow an undefined context (not required to pass a context). Maybe internally, context would default to an empty object, and if context values aren't set, they simply get passed to schema rules as undefined. Rules can then handle this as needed.
For reference, this Formik issue might be resolved for some people by making this change in yup: jaredpalmer/formik#503. Although, I think resolving both issues would be beneficial.
The text was updated successfully, but these errors were encountered:
idk this sounds like good error, albeit not a super clear one. If you define a schema a dependent on context doesn't it make sense to error if you don't provide any context?
Thanks for the quick response. In general I think the error is fine. I'm interested in a way to allow the schema to decide wether it should error or not, though. Maybe you want yup to error by default, so option 2 isn't ideal.
Option 1 would allow the schema to have individual rules handle undefined context, or undefined properties on context, or even better, avoid undefined values altogether. This would help in situations where validation and casting logic is handled by a base class, or wrapper, where most of the time you can use the default context but can overwrite validation/casting and provide a different context when necessary. Hope that makes sense!
I found that when a schema uses context,
.when('$other', (other, schema) => (other === 4 ? schema.max(6) : schema)),
and a context isn't provided with
schema.validate()
, an error will occur:TypeError: Cannot read property 'other' of undefined
Is there a way to avoid this error, without passing a context into the
validate
call?A couple ideas:
undefined
. Rules can then handle this as needed.For reference, this Formik issue might be resolved for some people by making this change in yup: jaredpalmer/formik#503. Although, I think resolving both issues would be beneficial.
The text was updated successfully, but these errors were encountered: