@@ -258,107 +258,106 @@ test("multi field OR", () => {
258
258
] ) ;
259
259
} ) ;
260
260
261
- /**
262
- #### OR
263
-
264
- In addition to previous rule we need `bio`, if `state` is `NY`.
265
-
266
- ```js
267
- let rules = [{
268
- conditions: {
269
- or: [
270
- {
271
- age: { less : 70 },
272
- country: { is: "USA" }
273
- },
274
- {
275
- state: { is: "NY"}
276
- }
277
- ]
278
- },
279
- event: {
280
- type: "require",
281
- params: { fields: [ "bio" ]}
282
- }
283
- }]
284
- ```
285
-
286
- #### NOT
287
-
288
- When we don't require `bio` we need `zip` code.
289
-
290
- ```js
291
- let rules = [{
292
- conditions: {
293
- not: {
294
- or: [
295
- {
296
- age: { less : 70 },
297
- country: { is: "USA" }
261
+ test ( "multi field NOT" , ( ) => {
262
+ let rules = [
263
+ {
264
+ conditions : {
265
+ not : {
266
+ or : [
267
+ {
268
+ age : { less : 70 } ,
269
+ country : { is : "USA" } ,
270
+ } ,
271
+ {
272
+ state : { is : "NY" } ,
273
+ } ,
274
+ ] ,
298
275
} ,
299
- {
300
- state: { is: "NY"}
301
- }
302
- ]
303
- }
304
- },
305
- event: {
306
- type: "require",
307
- params: { fields: [ "zip" ]}
308
- }
309
- }]
310
- ```
311
-
312
- ### Nested object queries
313
-
314
- Rules engine supports querying inside nested objects, with [selectn](https://github.com/wilmoore/selectn.js),
315
- any data query that works in [selectn](https://github.com/wilmoore/selectn.js), will work in here
316
-
317
- Let's say we need to require `state`, when `work` has a `name` `congressman`, this is how we can do this:
318
-
319
- ```js
320
- let rules = [{
321
- conditions: {
322
- "work.name": {
323
- name: { equals: "congressman" },
324
- }
325
- },
326
- event: {
327
- type: "require",
328
- params: { fields: [ "state" ]}
329
- }
330
- }]
331
- ```
332
-
333
- ### Nested arrays object queries
276
+ } ,
277
+ event : EVENT ,
278
+ } ,
279
+ ] ;
334
280
335
- Sometimes we need to make changes to the form if some nested condition is true.
281
+ let engine = new Engine ( rules , schema ) ;
282
+ expect . assertions ( 5 ) ;
336
283
337
- For example if one of the `hobbies` is `baseball`, we need to make `state` `required`.
338
- This can be expressed like this:
284
+ return Promise . all ( [
285
+ engine
286
+ . run ( { age : 16 , country : "China" , state : "Beijing" } )
287
+ . then ( res => expect ( res ) . toEqual ( [ EVENT ] ) ) ,
288
+ engine
289
+ . run ( { age : 16 , country : "China" , state : "NY" } )
290
+ . then ( res => expect ( res ) . toEqual ( [ ] ) ) ,
291
+ engine
292
+ . run ( { age : 16 , country : "USA" } )
293
+ . then ( res => expect ( res ) . toEqual ( [ ] ) ) ,
294
+ engine . run ( { age : 80 , state : "NY" } ) . then ( res => expect ( res ) . toEqual ( [ ] ) ) ,
295
+ engine
296
+ . run ( { age : 69 , country : "USA" } )
297
+ . then ( res => expect ( res ) . toEqual ( [ ] ) ) ,
298
+ ] ) ;
299
+ } ) ;
339
300
340
- ```js
341
- let rules = [{
342
- conditions: {
343
- hobbies: {
344
- name: { equals: "baseball" },
345
- }
346
- },
347
- event: {
348
- type: "require",
349
- params: { fields: [ "state" ]}
350
- }
351
- }]
352
- ```
301
+ test ( "Nested object queries" , ( ) => {
302
+ let rules = [
303
+ {
304
+ conditions : {
305
+ "work.name" : { is : "congressman" } ,
306
+ } ,
307
+ event : EVENT ,
308
+ } ,
309
+ ] ;
353
310
354
- Rules engine will go through all the elements in the array and trigger `require` if `any` of the elements meet the criteria
311
+ let engine = new Engine ( rules , schema ) ;
312
+ expect . assertions ( 5 ) ;
355
313
356
- ## Support
314
+ return Promise . all ( [
315
+ engine . run ( { work : { } } ) . then ( res => expect ( res ) . toEqual ( [ ] ) ) ,
316
+ engine . run ( { } ) . then ( res => expect ( res ) . toEqual ( [ ] ) ) ,
317
+ engine
318
+ . run ( { work : { name : "congressman" } } )
319
+ . then ( res => expect ( res ) . toEqual ( [ EVENT ] ) ) ,
320
+ engine
321
+ . run ( { work : { name : "president" } } )
322
+ . then ( res => expect ( res ) . toEqual ( [ ] ) ) ,
323
+ engine
324
+ . run ( { work : { name : "blacksmith" } } )
325
+ . then ( res => expect ( res ) . toEqual ( [ ] ) ) ,
326
+ ] ) ;
327
+ } ) ;
357
328
358
- If you are having issues, please let us know.
359
- We have a mailing list located at: ...
329
+ test ( "Nested arrays object queries" , ( ) => {
330
+ let rules = [
331
+ {
332
+ conditions : {
333
+ hobbies : {
334
+ name : { is : "baseball" } ,
335
+ } ,
336
+ } ,
337
+ event : EVENT ,
338
+ } ,
339
+ ] ;
360
340
361
- ## License
341
+ let engine = new Engine ( rules , schema ) ;
342
+ expect . assertions ( 5 ) ;
362
343
363
- The project is licensed under the Apache Licence 2.0.
364
- **/
344
+ return Promise . all ( [
345
+ engine . run ( { hobbies : [ ] } ) . then ( res => expect ( res ) . toEqual ( [ ] ) ) ,
346
+ engine . run ( { } ) . then ( res => expect ( res ) . toEqual ( [ ] ) ) ,
347
+ engine
348
+ . run ( { hobbies : [ { name : "baseball" } ] } )
349
+ . then ( res => expect ( res ) . toEqual ( [ EVENT ] ) ) ,
350
+ engine
351
+ . run ( {
352
+ hobbies : [
353
+ { name : "reading" } ,
354
+ { name : "jumping" } ,
355
+ { name : "baseball" } ,
356
+ ] ,
357
+ } )
358
+ . then ( res => expect ( res ) . toEqual ( [ EVENT ] ) ) ,
359
+ engine
360
+ . run ( { hobbies : [ { name : "reading" } , { name : "jumping" } ] } )
361
+ . then ( res => expect ( res ) . toEqual ( [ ] ) ) ,
362
+ ] ) ;
363
+ } ) ;
0 commit comments