LT-92: Adding schema validation to evaluation JSONs

This commit is contained in:
Bruno Alves de Faria 2017-01-13 18:32:28 -02:00
parent 697b4e7ff8
commit 777d5216f1
7 changed files with 42 additions and 31 deletions

View File

@ -1,27 +1,44 @@
import { BaseCriterion } from './criteria/BaseCriterion';
import * as Criteria from './criteria'; import * as Criteria from './criteria';
import { _ } from 'meteor/underscore'; import { _ } from 'meteor/underscore';
import Ajv from 'ajv';
export class CriteriaEvaluator { export class CriteriaEvaluator {
constructor(criteriaObject) { constructor(criteriaObject) {
this.criteria = []; this.criteria = [];
const schema = {
properties: {},
definitions: {
simpleArray: { type: 'array' }
}
};
_.each(Criteria, (Criterion, key) => {
if (Criterion.prototype instanceof BaseCriterion) {
const criterionkey = key.replace(/Criterion$/, '');
schema.definitions[criterionkey] = Criteria[`${criterionkey}Schema`];
schema.properties[criterionkey] = {
oneOf: [
{ $ref: '#/definitions/simpleArray' },
{ $ref: `#/definitions/${criterionkey}` }
]
};
}
});
const validator = new Ajv().compile(schema);
if (!validator(criteriaObject)) {
let message = '';
_.each(validator.errors, error => {
message += `\noptions${error.dataPath} ${error.message}`;
});
throw new Error(message);
}
_.each(criteriaObject, (optionsObject, criterionkey) => { _.each(criteriaObject, (optionsObject, criterionkey) => {
const Criterion = Criteria[`${criterionkey}Criterion`]; const Criterion = Criteria[`${criterionkey}Criterion`];
const optionsArray = optionsObject instanceof Array ? optionsObject : [optionsObject]; const optionsArray = optionsObject instanceof Array ? optionsObject : [optionsObject];
_.each(optionsArray, options => { _.each(optionsArray, options => this.criteria.push(new Criterion(options)));
const validator = Criteria[`${criterionkey}Validator`];
if (!validator(options)) {
let message = `Invalid ${criterionkey}Criterion definition.`;
_.each(validator.errors, error => {
message += `\noptions${error.dataPath} ${error.message}`;
});
throw new Error(message);
}
const criterion = new Criterion(options);
this.criteria.push(criterion);
});
}); });
} }

View File

@ -1,8 +1,7 @@
import { BaseCriterion } from './BaseCriterion'; import { BaseCriterion } from './BaseCriterion';
import { _ } from 'meteor/underscore'; import { _ } from 'meteor/underscore';
import Ajv from 'ajv';
export const MaxTargetsValidator = new Ajv().compile({ export const MaxTargetsSchema = {
properties: { properties: {
limit: { limit: {
label: 'Max targets allowed in study', label: 'Max targets allowed in study',
@ -11,7 +10,7 @@ export const MaxTargetsValidator = new Ajv().compile({
} }
}, },
required: ['limit'] required: ['limit']
}); };
/* MaxTargetsCriterion /* MaxTargetsCriterion
* Check if the number of target measurements exceeded the limit allowed * Check if the number of target measurements exceeded the limit allowed

View File

@ -1,7 +1,6 @@
import { BaseCriterion } from './BaseCriterion'; import { BaseCriterion } from './BaseCriterion';
import Ajv from 'ajv';
export const MaxTargetsPerOrganValidator = new Ajv().compile({ export const MaxTargetsPerOrganSchema = {
properties: { properties: {
limit: { limit: {
label: 'Max targets allowed per organ', label: 'Max targets allowed per organ',
@ -10,7 +9,7 @@ export const MaxTargetsPerOrganValidator = new Ajv().compile({
} }
}, },
required: ['limit'] required: ['limit']
}); };
/* /*
* MaxTargetsPerOrganCriterion * MaxTargetsPerOrganCriterion

View File

@ -1,7 +1,6 @@
import { BaseCriterion } from './BaseCriterion'; import { BaseCriterion } from './BaseCriterion';
import Ajv from 'ajv';
export const MeasurementsLengthValidator = new Ajv().compile({ export const MeasurementsLengthSchema = {
properties: { properties: {
longAxis: { longAxis: {
label: 'Minimum length of long axis', label: 'Minimum length of long axis',
@ -70,7 +69,7 @@ export const MeasurementsLengthValidator = new Ajv().compile({
{ required: ['message', 'longAxisSliceThicknessMultiplier'] }, { required: ['message', 'longAxisSliceThicknessMultiplier'] },
{ required: ['message', 'shortAxisSliceThicknessMultiplier'] } { required: ['message', 'shortAxisSliceThicknessMultiplier'] }
] ]
}); };
/* /*
* MeasurementsLengthCriterion * MeasurementsLengthCriterion

View File

@ -1,8 +1,7 @@
import { BaseCriterion } from './BaseCriterion'; import { BaseCriterion } from './BaseCriterion';
import { _ } from 'meteor/underscore'; import { _ } from 'meteor/underscore';
import Ajv from 'ajv';
export const ModalityValidator = new Ajv().compile({ export const ModalitySchema = {
properties: { properties: {
method: { method: {
label: 'Specify if it\'s goinig to "allow" or "deny" the modalities', label: 'Specify if it\'s goinig to "allow" or "deny" the modalities',
@ -29,7 +28,7 @@ export const ModalityValidator = new Ajv().compile({
} }
}, },
required: ['method', 'modalities'] required: ['method', 'modalities']
}); };
/* /*
* ModalityCriteria * ModalityCriteria

View File

@ -1,8 +1,7 @@
import { BaseCriterion } from './BaseCriterion'; import { BaseCriterion } from './BaseCriterion';
import Ajv from 'ajv';
export const NonTargetResponseValidator = new Ajv().compile({ export const NonTargetResponseSchema = {
}); };
/* NonTargetResponseCriterion /* NonTargetResponseCriterion
* Check if the there are non-target measurements with response different than "present" on baseline * Check if the there are non-target measurements with response different than "present" on baseline

View File

@ -1,8 +1,7 @@
import { BaseCriterion } from './BaseCriterion'; import { BaseCriterion } from './BaseCriterion';
import Ajv from 'ajv';
export const TargetTypeValidator = new Ajv().compile({ export const TargetTypeSchema = {
}); };
/* TargetTypeCriterion /* TargetTypeCriterion
* Check if the there are non-bidirectional target measurements on baseline * Check if the there are non-bidirectional target measurements on baseline