Skip to content

Commit

Permalink
fix(#8806): merge extra validations with pupil so rules can have both
Browse files Browse the repository at this point in the history
  • Loading branch information
garethbowen authored Nov 3, 2024
1 parent 46a01b9 commit 0d70a98
Show file tree
Hide file tree
Showing 8 changed files with 460 additions and 375 deletions.
8 changes: 4 additions & 4 deletions shared-libs/transitions/test/unit/pregnancy_registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ describe('pregnancy registration with weeks since LMP', () => {
return transition.onMatch({ doc: doc }).then(function(changed) {
assert.equal(changed, true);
assert.equal(doc.patient_id, undefined);
assert.equal(getMessage(doc), 'Invalid patient name. Invalid LMP; must be between 0-40 weeks.');
assert.equal(getMessage(doc), 'Invalid LMP; must be between 0-40 weeks. Invalid patient name.');
});
});

Expand Down Expand Up @@ -584,9 +584,9 @@ describe('pregnancy registration with exact LMP date', () => {
assert.equal(doc.patient_id, undefined);
assert.equal(doc.lmp_date, null);
assert.equal(getMessage(doc),
'Invalid patient name. ' +
' Date should be later than 40 weeks ago. ' +
' Date should be older than 8 weeks ago.');
'Date should be later than 40 weeks ago. ' +
' Date should be older than 8 weeks ago. ' +
' Invalid patient name.');
});
});

Expand Down
54 changes: 19 additions & 35 deletions shared-libs/validation/src/pupil.js
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
};
Loading

0 comments on commit 0d70a98

Please sign in to comment.