@@ -1353,6 +1353,10 @@ let v = new Validator({
13531353 evenNumber: " The '{field}' field must be an even number! Actual: {actual}" ,
13541354 realNumber: " The '{field}' field must be a real number! Actual: {actual}" ,
13551355 notPermitNumber: " The '{field}' cannot have the value {actual}" ,
1356+ compareGt: " The '{field}' field must be greater than {gt}! Actual: {actual}" ,
1357+ compareGte: " The '{field}' field must be greater than or equal to {gte}! Actual: {actual}" ,
1358+ compareLt: " The '{field}' field must be less than {lt}! Actual: {actual}" ,
1359+ compareLte: " The '{field}' field must be less than or equal to {lte}! Actual: {actual}"
13561360 },
13571361 customFunctions: {
13581362 even : (value , errors )=> {
@@ -1366,7 +1370,22 @@ let v = new Validator({
13661370 errors .push ({ type: " realNumber" , actual: value });
13671371 }
13681372 return value;
1369- }
1373+ },
1374+ compare : (value , errors , schema )=> {
1375+ if ( typeof schema .custom .gt === " number" && value <= schema .custom .gt ){
1376+ errors .push ({ type: " compareGt" , actual: value, gt: schema .custom .gt });
1377+ }
1378+ if ( typeof schema .custom .gte === " number" && value < schema .custom .gte ){
1379+ errors .push ({ type: " compareGte" , actual: value, gte: schema .custom .gte });
1380+ }
1381+ if ( typeof schema .custom .lt === " number" && value >= schema .custom .lt ){
1382+ errors .push ({ type: " compareLt" , actual: value, lt: schema .custom .lt });
1383+ }
1384+ if ( typeof schema .custom .lte === " number" && value > schema .custom .lte ){
1385+ errors .push ({ type: " compareLte" , actual: value, lte: schema .custom .lte });
1386+ }
1387+ return value;
1388+ }
13701389 }
13711390});
13721391
@@ -1376,6 +1395,7 @@ const schema = {
13761395 people: {
13771396 type: " number" ,
13781397 custom: [
1398+ " compare|gte:-100|lt:200" , // extended definition with additional parameters - equal to: {type:"compare",gte:-100, lt:200},
13791399 " even" ,
13801400 " real" ,
13811401 function (value , errors ){
@@ -1388,6 +1408,8 @@ const schema = {
13881408 }
13891409};
13901410
1411+ console .log (v .validate ({people: - 200 }, schema));
1412+ console .log (v .validate ({people: 200 }, schema));
13911413console .log (v .validate ({people: 5 }, schema));
13921414console .log (v .validate ({people: - 5 }, schema));
13931415console .log (v .validate ({people: 3 }, schema));
0 commit comments