Skip to content

Validate Promise API #40

@MichalLytek

Description

@MichalLytek

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: featureIssues related to new features.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions