@@ -17,11 +17,20 @@ local posix = require("posix.fnmatch")
1717
1818local BuiltInFunctions = {}
1919
20+ function BuiltInFunctions .validateVariadicArgs (expectedLen , args )
21+ if # args ~= expectedLen then
22+ return error (" Expected" .. expectedLen .. " arguments, but got " ..# args )
23+ end
24+ for i = 1 ,expectedLen do
25+ if type (args [i ])~= " string" then
26+ return error (" Argument must be a string" )
27+ end
28+ end
29+ end
30+
2031-- Wrapper for keyMatch
2132function BuiltInFunctions .keyMatchFunc (args )
22- if # args < 2 then
23- error (" BuiltInFunctions should have atleast 2 arguments" )
24- end
33+ BuiltInFunctions .validateVariadicArgs (2 , args )
2534 return BuiltInFunctions .keyMatch (args [1 ], args [2 ])
2635end
2736
4251
4352-- Wrapper for keyGet
4453function BuiltInFunctions .keyGetFunc (args )
45- if # args < 2 then
46- error (" BuiltInFunctions should have atleast 2 arguments" )
47- end
54+ BuiltInFunctions .validateVariadicArgs (2 , args )
4855 return BuiltInFunctions .keyGet (args [1 ], args [2 ])
4956end
5057
6774
6875-- Wrapper for keyMatch2
6976function BuiltInFunctions .keyMatch2Func (args )
70- if # args < 2 then
71- error (" BuiltInFunctions should have atleast 2 arguments" )
72- end
77+ BuiltInFunctions .validateVariadicArgs (2 , args )
7378 return BuiltInFunctions .keyMatch2 (args [1 ], args [2 ])
7479end
7580
8489
8590-- Wrapper for keyMatch3
8691function BuiltInFunctions .keyMatch3Func (args )
87- if # args < 2 then
88- error (" BuiltInFunctions should have atleast 2 arguments" )
89- end
92+ BuiltInFunctions .validateVariadicArgs (2 , args )
9093 return BuiltInFunctions .keyMatch3 (args [1 ], args [2 ])
9194end
9295
100103
101104-- Wrapper for regexMatch
102105function BuiltInFunctions .regexMatchFunc (args )
103- if # args < 2 then
104- error (" BuiltInFunctions should have atleast 2 arguments" )
105- end
106+ BuiltInFunctions .validateVariadicArgs (2 , args )
106107 return BuiltInFunctions .regexMatch (args [1 ], args [2 ])
107108end
108109
118119
119120-- Wrapper for globMatch
120121function BuiltInFunctions .globMatchFunc (args )
121- if # args < 2 then
122- error (" BuiltInFunctions should have atleast 2 arguments" )
123- end
122+ BuiltInFunctions .validateVariadicArgs (2 , args )
124123 return BuiltInFunctions .globMatch (args [1 ], args [2 ])
125124end
126125
0 commit comments