0.0.6 • Published 5 years ago

rule-tree v0.0.6

Weekly downloads
7
License
MIT
Repository
github
Last release
5 years ago

Rule Tree

Build Status codecov Maintainability

A simpler structure for complex validations

Usage

import { createRuleTree } from 'rule-tree';

const activeUserHasManyFriends = createRuleTree({
    condition: 'and',
    rules: [
        { condition: 'equals', data: '#getStatus', value: 'active' },
        { condition: 'greater-than', data: '.friends.length', value: 1 }
    ]
});

const user = {
    id: 1,
    name: 'John',
    getStatus: () => 'active',
    friends: [2, 3]
};

activeUserHasManyFriends.validate(user);

Conditions

Built-in Conditions
  • and
  • equals
  • every
  • exists
  • greater-than-equals
  • greater-than
  • in
  • lesser-than-equals
  • lesser-than
  • not
  • or
  • some
  • sum-matches-rule
Custom Conditions
import { treeBuilder, DataParserFactory, DataValueParserFactory } from 'rule-tree';

treeBuilder
    .addCondition('is-john', DataParserFactory, (tree) => (data, rule) => data === 'John')
    .addCondition('is-fred', DataValueParserFactory, (tree) => ({
        validate(data, rule) {
            const selected = tree.dataSelector.select(data, rule.options.value);
            return selected === 'Fred';
        }
    }));

const tree = treeBuilder.build(schema);

See validators for more examples

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

6 years ago

0.0.3

6 years ago

0.0.2

6 years ago

0.0.1

6 years ago