I'm working with an API that returns a schema for validating a form before users can submit their data.
For example, the schema has a User
class featuring an attribute called email
. If there is an error, User.validators.getEmailErrors()
returns an Array
of all the errors, e.g. ['Email address cannot be blank', 'Email addresses must match']
.
However, if the field is valid, and no errors are found, getEmailErrors()
returns null
.
In my app, I want to safely chain more methods from getEmailErrors()
, e.g. getEmailErrors().join(',')
, but without checking for null
beforehand. Rather, is there a way, e.g. using ES6 proxies, to make getEmailAddress()
aware of whether it will return an Array
, and to safely ignore any methods like join()
in case it returns null
?
The easy solution would be to return an empty Array
in the valid case instead of null
, but assume I can't change that.