@modernpoacher/rules-runner
Encapsulate rules in an easily comprehended JSON/JavaScript object literal format.
Install
npm i -P @modernpoacher/rules-runner
Examples
if
the conditions are met then
will execute.
const RulesRunner = const config = 'Must be 16 or older if no adult is present': if: 'person.age': lessThan: 16 'person.adultPresent': false then: 'person.error': 'Must be 16 or older if no adult is present' 'errors.all[]': 'person' 'Must be employed': if: 'company.isEmployed': false then: 'company.error': 'Must be employed' 'errors.all[]': 'company' const rulesRunner = config const values = person: age: 15 adultPresent: false company: isEmployed: false rulesRunner assertassertassert
if
the conditions are not met otherwise
will execute.
const RulesRunner = const config = 'Person will be in house if person is tired or hungry': if: 'person.age': lessThan: 16 'person.adultPresent': false then: 'person.location': 'house' otherwise: 'person.location': 'work' const rulesRunner = config const values = person: age: 17 adultPresent: true rulesRunner assert
Comparators
- equals
'person.exists': true
'person.firstName': 'John'
'person.age': 21
- boolean
'person.exists': false
- between
'person.age': between: 1 20
- contains
'person.name': contains: 'Jr'
- lessThan
'person.age': lessThan: 21
- greaterThan
'person.age': greaterThan: 20
- oneOf
'person.state': oneOf: 'CA' 'TX' 'NY'
- anyOf
'person.state': anyOf: 'CA' 'TX' 'NY'
- allOf
'person.state': allOf: 'CA' 'TX' 'NY'
- matches
'person.name': matches: '/(john|bob|mary)/i'
- not
'person.state': not: 'CA' `
'person.state': not: oneOf: 'CA' 'TX'