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 { _ } from 'meteor/underscore';
import Ajv from 'ajv';
export class CriteriaEvaluator {
constructor(criteriaObject) {
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) => {
const Criterion = Criteria[`${criterionkey}Criterion`];
const optionsArray = optionsObject instanceof Array ? optionsObject : [optionsObject];
_.each(optionsArray, 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);
});
_.each(optionsArray, options => this.criteria.push(new Criterion(options)));
});
}

View File

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

View File

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

View File

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

View File

@ -1,8 +1,7 @@
import { BaseCriterion } from './BaseCriterion';
import { _ } from 'meteor/underscore';
import Ajv from 'ajv';
export const ModalityValidator = new Ajv().compile({
export const ModalitySchema = {
properties: {
method: {
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']
});
};
/*
* ModalityCriteria

View File

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