Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating errors from validation results #10

Open
dmitrizagidulin opened this issue Mar 27, 2017 · 0 comments
Open

Creating errors from validation results #10

dmitrizagidulin opened this issue Mar 27, 2017 · 0 comments

Comments

@dmitrizagidulin
Copy link
Member

dmitrizagidulin commented Mar 27, 2017

A typical usage for json-document validation is something like in jose/JWKSet.importKeys():

if (!validation.valid) {
  return Promise.reject(validation)
}

However, Promise.reject(), just like a regular throw, is supposed to accept Error objects. For various historical reasons, throw also accepts non-error objects (so you can throw a string, ie throw 'Something'). Same with Promise.reject(). However, this is considered an anti-pattern, and Standard linters flag this as an error.

Now, a validation result is currently not an Error instance, it's its own object.

So the question before us is this: What's the best way to turn a validation result into an Error instance? I'm currently doing something like:

return Promise.reject(new Error(JSON.stringify(validation)))

but that feels hack-y. We should actually consider a .error getter method on our validation result instances. (That would return a nice error instance, populated with a stringified validation error message.) So that the use can do:

return Promise.reject(validation.error)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant