LT-92: Adding schema validation to evaluation JSONs
This commit is contained in:
parent
697b4e7ff8
commit
777d5216f1
@ -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 = [];
|
||||||
|
|
||||||
_.each(criteriaObject, (optionsObject, criterionkey) => {
|
const schema = {
|
||||||
const Criterion = Criteria[`${criterionkey}Criterion`];
|
properties: {},
|
||||||
const optionsArray = optionsObject instanceof Array ? optionsObject : [optionsObject];
|
definitions: {
|
||||||
_.each(optionsArray, options => {
|
simpleArray: { type: 'array' }
|
||||||
const validator = Criteria[`${criterionkey}Validator`];
|
}
|
||||||
if (!validator(options)) {
|
};
|
||||||
let message = `Invalid ${criterionkey}Criterion definition.`;
|
_.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 => {
|
_.each(validator.errors, error => {
|
||||||
message += `\noptions${error.dataPath} ${error.message}`;
|
message += `\noptions${error.dataPath} ${error.message}`;
|
||||||
});
|
});
|
||||||
throw new Error(message);
|
throw new Error(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
const criterion = new Criterion(options);
|
_.each(criteriaObject, (optionsObject, criterionkey) => {
|
||||||
this.criteria.push(criterion);
|
const Criterion = Criteria[`${criterionkey}Criterion`];
|
||||||
});
|
const optionsArray = optionsObject instanceof Array ? optionsObject : [optionsObject];
|
||||||
|
_.each(optionsArray, options => this.criteria.push(new Criterion(options)));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user