You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constconditions={"Must be under 18": {IF: {"person.age": {lessThan: 18}},THEN: {"person.error": "You are not old enough"}},"Must have a driver's license": {IF: {"person.hasDriversLicense": true},THEN: {"person.error": "You need to get a driver's license first"}}};constdataset={person: {age: 17,hasDriversLicense: false}};construles=newRulesEngine(conditions,{modifyDataset: true});rules.run(dataset);expect(data.person.error).toEqual("You are not old enough");
OR conditons
importRulesEnginefrom'rules-engine';constconditions={"Person will be in house if person is tired or hungry": {IF: {OR: {"person.tired": true,// if this matches"person.hungry": true// OR if this matches}},THEN: {"person.location": "house"},}};constdataset={person: {tired: false,hungry: false}};construles=newRulesEngine(conditions);rules.run(dataset,{modifyDataset: true});expect(data.person.location).toEqual('house');
AND conditons
importRulesEnginefrom'rules-engine';constconditions={"Person will be in house if person is tired or hungry": {IF: {AND: {"person.tired": true,// if this matches"person.hungry": true// AND if this matches}},THEN: {"person.location": "house"},}};constdataset={person: {tired: true,hungry: true}};construles=newRulesEngine(conditions);rules.run(dataset,{modifyDataset: true});expect(data.person.location).toEqual('house');
No conditions match
importRulesEnginefrom'rules-engine';constconditions={"Person will be in house if person is tired or hungry": {IF: {AND: {"person.tired": true,// if this matches"person.hungry": true// AND if this matches}},THEN: {"person.location": "house"// then run this},OTHERWISE: {"person.location": "work"// otherwise run this}}};constdataset={person: {tired: false,hungry: true}};construles=newRulesEngine(conditions,{modifyDataset: true});rules.run(dataset);expect(data.person.location).toEqual('work');
Use Regex to match
importRulesEnginefrom"rules-engine-lib";constconditions={"Must have a valid email": {IF: {"email": {matches: /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/}},THEN: {"communication.error": "You need to provide a valid email"}}};constdataset={email: "JohnDoe@example.com"};construles=newRulesEngine(conditions,{modifyDataset: true});rules.run(dataset);expect(data.communication.error).toBeUndefined();
Weight the condition
importRulesEnginefrom'rules-engine';constconditions={"Must be not student": {IF: {"person.school": true,},THEN: {"person.error": 'Person is student'},WEIGHT: 1,},"Must be age GreaterThan 16": {IF: {"person.age": {greaterThan: 16,},},THEN: {"person.error": 'Person is old'},WEIGHT: 0},};constdataset={person: {name: 'Gon',age: 18,adultPresent: false,school: true},company: {isEmployed: false},};construles=newRulesEngine(conditions,{modifyDataset: true});rules.run(dataset);expect(dataset.person.error).toBe('Person is student');
Special paths eg: contains dot
importRulesEnginefrom'rules-engine';constconditions={"Person will not be John": {IF: {"['comapny', 'person.name']": {not: 'John'}},THEN: {"company.isEmployee": true// then run this},OTHERWISE: {"company.isEmployee": false// otherwise run this}}};constdataset={company: {'person.name': 'John'}};construles=newRulesEngine(conditions,{modifyDataset: true});rules.run(dataset);expect(company.isEmployee).toEqual(false);