Validate Promise API
See original GitHub issueI like this library but the API could be better. I’m talking about this:
validate(post).then(errors => { // errors is an array of validation errors
if (errors.length > 0) {
console.log("validation failed. errors: ", errors);
} else {
console.log("validation succeed");
}
});
It would be better if the validate() reject promise on errors. To do that, this line should be replaced with:
const errors = executor.stripEmptyErrors(validationErrors);
return errors.length ? Promise.reject(errors) : Promise.resolve();
Then we simply work with validate() in async&await:
try {
await validate(post);
console.log("validation succeed");
} catch (errors) {
console.log("validation failed. errors: ", errors);
}
And if you don’t want to handle the errors (like in routing-controllers), you can just simply do that and it will throw error on errors:
await validate(post);
await postRepository.persist(post);
...
What do you think about this? It might be a breaking change, so you should publish this as a major version (to prevent auto update on npm install for older users) or we have to create a new name for the validate function with promise rejection behavior.
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (9 by maintainers)
Top Results From Across the Web
Promise - JavaScript - MDN Web Docs
A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with...
Read more >Promise API - The Modern JavaScript Tutorial
Promise.all(promises) – waits for all promises to resolve and returns an array of their results. · Promise.allSettled(promises) (recently added ...
Read more >Promise-Based Validation - Code
Here, we ensure every validation is a Promise by checking the return value of validation.ok() . If it contains the then() method, we...
Read more >JavaScript Promise Tutorial – How to Resolve or Reject ...
First, let us create a generic function that accepts a PokeAPI URL as argument and returns a Promise. If the API call is...
Read more >Check if a Value is a Promise using JavaScript | bobbyhadz
To check if a value is promise, check if the type of the value is an object and has a property named then...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

nooo don’t like either
Done in #43 . However it violate the DRY so it would be better to refactor the common code to private method and call it in the public API.