11import * as chai from 'chai' ;
22import { expect } from 'chai' ;
3- import { evaluate , ExpressionHandler , validate } from '../' ;
3+ import { evaluate , ExpressionHandler , validate , ValidationContext } from '../' ;
44import chaiAsPromised from 'chai-as-promised' ;
55
66chai . use ( chaiAsPromised ) ;
@@ -1679,11 +1679,44 @@ describe('evaluator', () => {
16791679 expect ( await evaluate ( expression , context , functionsTable , runOpts ) ) . to . eql ( true ) ;
16801680 } ) ;
16811681
1682+ it ( 'should evaluate recordNested.a with ref' , async ( ) => {
1683+ const expression = {
1684+ 'recordNested.a' : { eq : { ref : 'recordNested.a' as const } } ,
1685+ } ;
1686+ type Context = {
1687+ timesCounter : number ;
1688+ userId : string ;
1689+ recordNested ?: Record < string , string | boolean | number | undefined > ;
1690+ } ;
1691+ const context : Context = {
1692+ timesCounter : 5 ,
1693+ 1694+ recordNested : {
1695+ a : 5 ,
1696+ } ,
1697+ } ;
1698+ const validationContext = {
1699+ timesCounter : 5 ,
1700+ 1701+ recordNested : {
1702+ a : 5 ,
1703+ } ,
1704+ } ;
1705+ const runOpts : CustomEvaluatorFuncRunOptions = { dryRun : false } ;
1706+ expect ( await validate ( expression , validationContext , functionsTable , runOpts ) ) . to . be . an ( 'undefined' ) ;
1707+ expect ( await evaluate ( expression , context , functionsTable , runOpts ) ) . to . eql ( true ) ;
1708+ } ) ;
1709+
16821710 it ( 'should evaluate recordNested.a with short eq to false' , async ( ) => {
16831711 const expression = {
16841712 'recordNested.a' : 5 ,
16851713 } ;
1686- const context = {
1714+ type Context = {
1715+ timesCounter : number ;
1716+ userId : string ;
1717+ recordNested ?: Record < string , string | boolean | number | undefined > ;
1718+ } ;
1719+ const context : ValidationContext < Context > = {
16871720 timesCounter : 5 ,
1688172116891722 recordNested : {
@@ -1699,7 +1732,35 @@ describe('evaluator', () => {
16991732 const expression = {
17001733 'specialNested.a' : 5 ,
17011734 } ;
1702- const context = {
1735+ type Context = {
1736+ timesCounter : number ;
1737+ userId : string ;
1738+ specialNested ?: Record < string , string | boolean | number | undefined |
1739+ ( string | boolean | number | ( string | boolean | number ) [ ] ) [ ] > ;
1740+ } ;
1741+ const context :ValidationContext < Context > = {
1742+ timesCounter : 5 ,
1743+ 1744+ specialNested : {
1745+ a : 5 ,
1746+ } ,
1747+ } ;
1748+ const runOpts : CustomEvaluatorFuncRunOptions = { dryRun : false } ;
1749+ expect ( await validate ( expression , context , functionsTable , runOpts ) ) . to . be . an ( 'undefined' ) ;
1750+ expect ( await evaluate ( expression , context , functionsTable , runOpts ) ) . to . eql ( true ) ;
1751+ } ) ;
1752+
1753+ it ( 'should evaluate specialNested.a with ref' , async ( ) => {
1754+ const expression = {
1755+ 'specialNested.a' : { eq : { ref : 'specialNested.a' as const } } ,
1756+ } ;
1757+ type Context = {
1758+ timesCounter : number ;
1759+ userId : string ;
1760+ specialNested ?: Record < string , string | boolean | number | undefined |
1761+ ( string | boolean | number | ( string | boolean | number ) [ ] ) [ ] > ;
1762+ } ;
1763+ const context : ValidationContext < Context > = {
17031764 timesCounter : 5 ,
1704176517051766 specialNested : {
@@ -1715,7 +1776,13 @@ describe('evaluator', () => {
17151776 const expression = {
17161777 'specialNested.a' : { eq : 5 } ,
17171778 } ;
1718- const context = {
1779+ type Context = {
1780+ timesCounter : number ;
1781+ userId : string ;
1782+ specialNested ?: Record < string , string | boolean | number | undefined |
1783+ ( string | boolean | number | ( string | boolean | number ) [ ] ) [ ] > ;
1784+ } ;
1785+ const context :ValidationContext < Context > = {
17191786 timesCounter : 5 ,
1720178717211788 specialNested : {
@@ -1731,7 +1798,13 @@ describe('evaluator', () => {
17311798 const expression = {
17321799 'specialNested.b' : { gt : 10 } ,
17331800 } ;
1734- const context = {
1801+ type Context = {
1802+ timesCounter : number ;
1803+ userId : string ;
1804+ specialNested ?: Record < string , string | boolean | number | undefined |
1805+ ( string | boolean | number | ( string | boolean | number ) [ ] ) [ ] > ;
1806+ } ;
1807+ const context :ValidationContext < Context > = {
17351808 timesCounter : 5 ,
1736180917371810 specialNested : {
@@ -1747,7 +1820,13 @@ describe('evaluator', () => {
17471820 const expression = {
17481821 'specialNested.name' : { regexp : '^test' } ,
17491822 } ;
1750- const context = {
1823+ type Context = {
1824+ timesCounter : number ;
1825+ userId : string ;
1826+ specialNested ?: Record < string , string | boolean | number | undefined |
1827+ ( string | boolean | number | ( string | boolean | number ) [ ] ) [ ] > ;
1828+ } ;
1829+ const context :ValidationContext < Context > = {
17511830 timesCounter : 5 ,
1752183117531832 specialNested : {
@@ -1763,7 +1842,13 @@ describe('evaluator', () => {
17631842 const expression = {
17641843 'specialNested.id' : { inq : [ 1 , 2 , 3 ] } ,
17651844 } ;
1766- const context = {
1845+ type Context = {
1846+ timesCounter : number ;
1847+ userId : string ;
1848+ specialNested ?: Record < string , string | boolean | number | undefined |
1849+ ( string | boolean | number | ( string | boolean | number ) [ ] ) [ ] > ;
1850+ } ;
1851+ const context :ValidationContext < Context > = {
17671852 timesCounter : 5 ,
1768185317691854 specialNested : {
@@ -1779,7 +1864,12 @@ describe('evaluator', () => {
17791864 const expression = {
17801865 'recordNested.status' : true ,
17811866 } ;
1782- const context = {
1867+ type Context = {
1868+ timesCounter : number ;
1869+ userId : string ;
1870+ recordNested ?: Record < string , string | boolean | number | undefined > ;
1871+ } ;
1872+ const context :ValidationContext < Context > = {
17831873 timesCounter : 5 ,
1784187417851875 recordNested : {
@@ -1798,7 +1888,22 @@ describe('evaluator', () => {
17981888 { 'specialNested.b' : { gt : 10 } } ,
17991889 ] ,
18001890 } ;
1801- const context = {
1891+ type Context = {
1892+ timesCounter : number ;
1893+ userId : string ;
1894+ specialNested ?: Record < string , string | boolean | number | undefined |
1895+ ( string | boolean | number | ( string | boolean | number ) [ ] ) [ ] > ;
1896+ } ;
1897+ const validationContext : ValidationContext < Context > = {
1898+ timesCounter : 5 ,
1899+ 1900+ specialNested : {
1901+ a : 3 ,
1902+ b : 15 ,
1903+ } ,
1904+ } ;
1905+
1906+ const context : Context = {
18021907 timesCounter : 5 ,
1803190818041909 specialNested : {
@@ -1807,7 +1912,7 @@ describe('evaluator', () => {
18071912 } ,
18081913 } ;
18091914 const runOpts : CustomEvaluatorFuncRunOptions = { dryRun : false } ;
1810- expect ( await validate ( expression , context , functionsTable , runOpts ) ) . to . be . an ( 'undefined' ) ;
1915+ expect ( await validate ( expression , validationContext , functionsTable , runOpts ) ) . to . be . an ( 'undefined' ) ;
18111916 expect ( await evaluate ( expression , context , functionsTable , runOpts ) ) . to . eql ( true ) ;
18121917 } ) ;
18131918 } ) ;
0 commit comments