-
-
Notifications
You must be signed in to change notification settings - Fork 217
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(#8806): merge extra validations with pupil so rules can have both
- Loading branch information
1 parent
46a01b9
commit 0d70a98
Showing
8 changed files
with
460 additions
and
375 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 |
---|---|---|
@@ -1,53 +1,37 @@ | ||
const validator_functions = require('./validator_functions.js'); | ||
const validation_result = require('./validation_result.js'); | ||
|
||
const lexer = require('./lexer.js'); | ||
const parser = require('./parser.js'); | ||
const validator = require('./validator.js'); | ||
|
||
const ruleCache = {}; | ||
|
||
const addFunction = function(name, callable) { | ||
validator_functions[name.toLowerCase()] = callable; | ||
const getEntities = (rule) => { | ||
if (!ruleCache[rule]) { | ||
const tokens = lexer.tokenize(rule); | ||
const entities = parser.parse(tokens); | ||
ruleCache[rule] = entities; | ||
} | ||
return ruleCache[rule]; | ||
}; | ||
|
||
const validate = function(rules, values) { | ||
const results = {}; | ||
|
||
// Start by defaulting all given values' validation results to "passing" | ||
Object.keys(values).forEach((key) => { | ||
results[key] = true; | ||
}); | ||
|
||
// And then run the rules | ||
Object.keys(rules).forEach((index) => { | ||
if (typeof values[index] === 'undefined' || values[index] === null) { | ||
values[index] = ''; | ||
} | ||
|
||
const rule = rules[index]; | ||
let tokens; | ||
let entities; | ||
|
||
if (ruleCache[rule]) { | ||
entities = ruleCache[rule]; | ||
} else { | ||
tokens = lexer.tokenize(rule); | ||
entities = parser.parse(tokens); | ||
const validate = async function(validations, values) { | ||
const results = []; | ||
|
||
ruleCache[rule] = entities; | ||
for (const validation of validations) { | ||
const key = validation.property; | ||
if (typeof values[key] === 'undefined' || values[key] === null) { | ||
values[key] = ''; | ||
} | ||
|
||
results[index] = validator.validate(entities, values, index); | ||
}); | ||
const rule = validation.rule; | ||
const entities = getEntities(rule); | ||
const valid = await validator.validate(entities, values, key); | ||
results.push({ valid, validation }); | ||
} | ||
|
||
return validation_result.create(results); | ||
return results; | ||
}; | ||
|
||
|
||
module.exports = { | ||
addFunction, | ||
lexer, | ||
parser, | ||
validate | ||
}; |
Oops, something went wrong.