-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #332 from FerX/feature/custom-array
improving the custom function with arrays of elements
- Loading branch information
Showing
4 changed files
with
284 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
let Validator = require("../index"); | ||
|
||
|
||
let v = new Validator({ | ||
debug: true, | ||
useNewCustomCheckerFunction: true, | ||
messages: { | ||
// Register our new error message text | ||
evenNumber: "The '{field}' field must be an even number! Actual: {actual}", | ||
realNumber: "The '{field}' field must be a real number! Actual: {actual}", | ||
notPermitNumber: "The '{field}' cannot have the value {actual}", | ||
}, | ||
customFunctions:{ | ||
even: (value, errors)=>{ | ||
if(value % 2 != 0 ){ | ||
errors.push({ type: "evenNumber", actual: value }); | ||
} | ||
return value; | ||
}, | ||
real: (value, errors)=>{ | ||
if(value <0 ){ | ||
errors.push({ type: "realNumber", actual: value }); | ||
} | ||
return value; | ||
} | ||
} | ||
}); | ||
|
||
|
||
|
||
const schema = { | ||
people:{ | ||
type: "number", | ||
custom: [ | ||
"even", | ||
"real", | ||
function (value, errors){ | ||
if(value === "3" ){ | ||
errors.push({ type: "notPermitNumber", actual: value }); | ||
} | ||
return value; | ||
} | ||
] | ||
} | ||
}; | ||
|
||
console.log(v.validate({people:5}, schema)); | ||
console.log(v.validate({people:-5}, schema)); | ||
console.log(v.validate({people:3}, schema)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters