question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validate Promise API

See original GitHub issue

I 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:closed
  • Created 7 years ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
pleerockcommented, Oct 16, 2016

nooo don’t like either

0reactions
MichalLytekcommented, Oct 16, 2016

its easier to make same changes again

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found