Support Center & Knowledge base

Value Rule

A value rule is a concrete implementation of a rule. Its use case is linked to typically single values like Numbers, Strings, Date and similar.

Part of the implementation ValueRule you get a method to make sure it is the correct type and fail it is not with a reason; failIfValueTypeMismatch(). In TypeScript one could argue that this should just be a generic type, but for API consistency and full compatibility with JavaScript, we’ve chosen this for now.

Implementation of a value rule in TypeScript:

import { ValueRule, IRuleContext, Reason } from '@dolittle/rules';

class MyRule extends ValueRule {
    static Reason ValueIsNotFortyTwo = Reason.create('4b99af88-09c0-4342-8876-24c42a48d728', 'Value should be forty two');

    evaluate(context: IRuleContext, subject: any): void {
        if (this.failIfValueTypeMismatch(context, subject, Number)) {
            const value = subject as Number;

            if (value != 42) {
      , subject, MyRule.ValueIsNotFortyTwo.noArguments());