Skip to main content

Ambient Context

Sometimes your validation logic will need to depend on external, or "ambient", context that isn't part of your form model. With fluentvalidation-ts validators are just classes, so you can make use of constructor arguments to inject dependencies.

The Gist

You can inject external dependencies into your validators using constructor arguments:

type FormModel = {
age: number;
};

class FormValidator extends Validator<FormModel> {
constructor(country: string) {
super();

this.ruleFor('age')
.greaterThanOrEqualTo(country === 'US' ? 21 : 18);
}
}

This approach means that you need to instantiate a new instance of your validator every time the ambient context changes, so there is potentially a performance cost involved.

Usage of the example validator from above might look something like this:

const ukValidator = new FormValidator('UK');
const usValidator = new FormValidator('US');

const pubGoer = { age: 20 };

const ukResult = ukValidator.validate(pubGoer); // {}
const usResult = usValidator.validate(pubGoer); // { age: 'Value must be greater than or equal to 21' }