AlexJPotter / fluentvalidation-ts

A TypeScript-first library for building strongly-typed validation rules
Apache License 2.0
87 stars 6 forks source link

Cannot chain .mustAsync method #18

Closed calebschoepp closed 3 years ago

calebschoepp commented 3 years ago

Maybe I missed this in the docs somewhere but I can't seem to chain .mustAsync and I'm unsure if I'm doing something wrong or if it is just not possible. The following example does not work. I'll keep digging in the docs and code but it'd be great if someone could help me understand why this does not work. Thanks :smile:

import { AsyncValidator } from 'fluentvalidation-ts';

type MyType = {
    field: string;
};

class Test extends AsyncValidator<MyType> {
    constructor() {
        super();

        this.ruleFor('field')
            .mustAsync(async () => true)
            .mustAsync(async () => true);
    }
}
AlexJPotter commented 3 years ago

Hi there,

Thanks for reporting this - it does appear to be an issue with the typings.

I'll look to get a fix out ASAP - in the meantime you should be able to work around this with two .ruleFor statements:

this.ruleFor('field').mustAsync(async () => true);
this.ruleFor('field').mustAsync(async () => true);

You could also pull out the predicates and wrap them into custom rules, which can be chained together using an array (see Custom Rules for further info and some examples).

AlexJPotter commented 3 years ago

Hi there - this issue should now be fixed in v2.2.2, please let me know if you have any further problems.

Thanks again for raising this issue! 😃

calebschoepp commented 3 years ago

Working like a charm. Thank you so much!