Support Center & Knowledge base

Validation

On top of the rule engine there is a special purposed validation system. While leveraging the rule engine, it provides an simpler abstraction to work with and also a set of commonly used rules.

Getting started

To get started you’ll need a reference to the @dolittle/validation package.

$ npm i @dolittle/validation

Or with yarn:

$ yarn add @dolittle/validation

By importing @dolittle/validation in your file, you’ll now have the PropertyRuleSetBuilder found in [@dolittle/rules]((/fundamentals/javascript-fundamentals/rules/) with methods for building our your rules for properties on objects. These rule types are called ValueRules.

Usage

Imagine you have a type that holds a property for email, you can simply then add the emailAddress rule adding a call to the method for the rule:

import { ObjectRuleSetContainerBuilder } from '@dolittle/rules';
import '@dolittle/validation';

const ruleSetContainerBuilder = new ObjectRuleSetContainerBuilder();
ruleSetContainerBuilder.ruleFor(_ => _.email).emailAddress();

With TypeScript, you would typically introduce a type that holds the property:

import { ObjectRuleSetContainerBuilder } from '@dolittle/rules';
import '@dolittle/validation';

class Person {
    email: String = '';
}

const ruleSetContainerBuilder = new ObjectRuleSetContainerBuilder<Person>();
ruleSetContainerBuilder.ruleFor(_ => _.email).emailAddress();

Evaluating this would be leveraging the same evaluation found in the rule engine.

Built-in rules