Skip to content

Commit 8582aef

Browse files
author
Fernando
committed
shorthand and custom parameters
1 parent 59b7b3c commit 8582aef

File tree

3 files changed

+62
-4
lines changed

3 files changed

+62
-4
lines changed

README.md

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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));
13911413
console.log(v.validate({people:5}, schema));
13921414
console.log(v.validate({people:-5}, schema));
13931415
console.log(v.validate({people:3}, schema));

lib/validator.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -462,13 +462,28 @@ class Validator {
462462

463463
if(Array.isArray(schema[fnName])){
464464
for (let i = 0; i < schema[fnName].length; i++) {
465+
466+
let custom = schema[fnName][i];
465467

466-
if(typeof schema[fnName][i]==="string"){
468+
if (typeof custom === "string") {
469+
custom = this.parseShortHand(custom);
470+
schema[fnName][i] = custom;
471+
}
472+
473+
const customIndex = ruleIndex*1000+i;
474+
context.customs[customIndex] = { messages, schema: {...schema, custom, index: i} };
475+
476+
ret.push( `
477+
const ${ruleVName}_${i} = context.customs[${customIndex}];
478+
479+
`);
480+
481+
if(custom.type){
467482
ret.push( `
468-
${vName} = ${context.async ? "await " : ""}context.customFunctions[${ruleVName}.schema.${fnName}[${i}]].call(this, ${vName}, ${fnCustomErrorsVName} , ${ruleVName}.schema, "${path}", parent, context);
483+
${vName} = ${context.async ? "await " : ""}context.customFunctions[${ruleVName}.schema.${fnName}[${i}].type].call(this, ${vName}, ${fnCustomErrorsVName} , ${ruleVName}_${i}.schema, "${path}", parent, context);
469484
`);
470485
}
471-
if(typeof schema[fnName][i]==="function"){
486+
if(typeof custom==="function"){
472487
ret.push( `
473488
${vName} = ${context.async ? "await " : ""}${ruleVName}.schema.${fnName}[${i}].call(this, ${vName}, ${fnCustomErrorsVName} , ${ruleVName}.schema, "${path}", parent, context);
474489
`);

test/validator.spec.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,12 +659,31 @@ describe("Test custom validation with array", () => {
659659
errors.push({ type: "realNumber", actual: value });
660660
}
661661
return value;
662+
},
663+
compare: (value, errors, schema)=>{
664+
if( typeof schema.custom.gt==="number" && value <= schema.custom.gt ){
665+
errors.push({ type: "compareGt", actual: value, gt: schema.custom.gt });
666+
}
667+
if( typeof schema.custom.gte==="number" && value < schema.custom.gte ){
668+
errors.push({ type: "compareGte", actual: value, gte: schema.custom.gte });
669+
}
670+
if( typeof schema.custom.lt==="number" && value >= schema.custom.lt ){
671+
errors.push({ type: "compareLt", actual: value, lt: schema.custom.lt });
672+
}
673+
if( typeof schema.custom.lte==="number" && value > schema.custom.lte ){
674+
errors.push({ type: "compareLte", actual: value, lte: schema.custom.lte });
675+
}
676+
return value;
662677
}
663678
},
664679
messages: {
665680
evenNumber: "The '{field}' field must be an even number! Actual: {actual}",
666681
realNumber: "The '{field}' field must be a real number! Actual: {actual}",
667682
permitNumber: "The '{field}' cannot have the value {actual}",
683+
compareGt: "The '{field}' field must be greater than {gt}! Actual: {actual}",
684+
compareGte: "The '{field}' field must be greater than or equal to {gte}! Actual: {actual}",
685+
compareLt: "The '{field}' field must be less than {lt}! Actual: {actual}",
686+
compareLte: "The '{field}' field must be less than or equal to {lte}! Actual: {actual}"
668687
}
669688
});
670689

@@ -676,6 +695,7 @@ describe("Test custom validation with array", () => {
676695
num: {
677696
type: "number",
678697
custom: [
698+
"compare|gte:-100|lt:200", // equal to: {type:"compare",gte:-100, lt:200},
679699
"even",
680700
"real",
681701
(value, errors) => {
@@ -699,6 +719,7 @@ describe("Test custom validation with array", () => {
699719
expect(check({ num: -8 })[0].type).toEqual("realNumber");
700720
expect(check({ num: 198 })[0].type).toEqual("permitNumber");
701721
expect(check({ num: 4 })[0].type).toEqual("permitNumber");
722+
expect(check({ num: 202 })[0].type).toEqual("compareLt");
702723
expect(check({ num: -3 }).map(e=>e.type)).toEqual(["evenNumber","realNumber","permitNumber"]);
703724
});
704725

0 commit comments

Comments
 (0)