LT-92: Adding schema validation for JSON criteria

This commit is contained in:
Bruno Alves de Faria 2017-01-13 17:32:10 -02:00
parent ed2f0ee36b
commit 697b4e7ff8
7 changed files with 42 additions and 29 deletions

View File

@ -8,14 +8,20 @@ export class CriteriaEvaluator {
_.each(criteriaObject, (optionsObject, criterionkey) => { _.each(criteriaObject, (optionsObject, criterionkey) => {
const Criterion = Criteria[`${criterionkey}Criterion`]; const Criterion = Criteria[`${criterionkey}Criterion`];
if (optionsObject instanceof Array) { const optionsArray = optionsObject instanceof Array ? optionsObject : [optionsObject];
_.each(optionsObject, options => { _.each(optionsArray, options => {
const criterion = new Criterion(options); const validator = Criteria[`${criterionkey}Validator`];
this.criteria.push(criterion); if (!validator(options)) {
}); let message = `Invalid ${criterionkey}Criterion definition.`;
} else { _.each(validator.errors, error => {
this.criteria.push(new Criterion(optionsObject)); message += `\noptions${error.dataPath} ${error.message}`;
} });
throw new Error(message);
}
const criterion = new Criterion(options);
this.criteria.push(criterion);
});
}); });
} }

View File

@ -1,7 +1,8 @@
import { BaseCriterion } from './BaseCriterion'; import { BaseCriterion } from './BaseCriterion';
import { _ } from 'meteor/underscore'; import { _ } from 'meteor/underscore';
import Ajv from 'ajv';
export const MaxTargetsSchema = { export const MaxTargetsValidator = new Ajv().compile({
properties: { properties: {
limit: { limit: {
label: 'Max targets allowed in study', label: 'Max targets allowed in study',
@ -10,7 +11,7 @@ export const MaxTargetsSchema = {
} }
}, },
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,6 +1,7 @@
import { BaseCriterion } from './BaseCriterion'; import { BaseCriterion } from './BaseCriterion';
import Ajv from 'ajv';
export const MaxTargetsPerOrganSchema = { export const MaxTargetsPerOrganValidator = new Ajv().compile({
properties: { properties: {
limit: { limit: {
label: 'Max targets allowed per organ', label: 'Max targets allowed per organ',
@ -9,7 +10,7 @@ export const MaxTargetsPerOrganSchema = {
} }
}, },
required: ['limit'] required: ['limit']
}; });
/* /*
* MaxTargetsPerOrganCriterion * MaxTargetsPerOrganCriterion

View File

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

View File

@ -1,11 +1,13 @@
import { BaseCriterion } from './BaseCriterion'; import { BaseCriterion } from './BaseCriterion';
import { _ } from 'meteor/underscore'; import { _ } from 'meteor/underscore';
import Ajv from 'ajv';
export const ModalitySchema = { export const ModalityValidator = new Ajv().compile({
properties: { properties: {
method: { method: {
label: 'Specify if it\'s goinig to "allow" or "restrict" the modalities', label: 'Specify if it\'s goinig to "allow" or "deny" the modalities',
type: 'string' type: 'string',
enum: ['allow', 'deny']
}, },
measurementTypes: { measurementTypes: {
label: 'List of measurement types that will be evaluated', label: 'List of measurement types that will be evaluated',
@ -17,7 +19,7 @@ export const ModalitySchema = {
uniqueItems: true uniqueItems: true
}, },
modalities: { modalities: {
label: 'List of allowed/restricted modalities', label: 'List of allowed/denied modalities',
type: 'array', type: 'array',
items: { items: {
type: 'string' type: 'string'
@ -27,15 +29,15 @@ export const ModalitySchema = {
} }
}, },
required: ['method', 'modalities'] required: ['method', 'modalities']
}; });
/* /*
* ModalityCriteria * ModalityCriteria
* Check if a modality is allowed or restricted * Check if a modality is allowed or denied
* Options: * Options:
* method (string): Specify if it\'s goinig to "allow" or "restrict" the modalities * method (string): Specify if it\'s goinig to "allow" or "deny" the modalities
* measurementTypes (string[]): List of measurement types that will be evaluated * measurementTypes (string[]): List of measurement types that will be evaluated
* modalities (string[]): List of allowed/restricted modalities * modalities (string[]): List of allowed/denied modalities
*/ */
export class ModalityCriterion extends BaseCriterion { export class ModalityCriterion extends BaseCriterion {
@ -60,7 +62,7 @@ export class ModalityCriterion extends BaseCriterion {
const modality = metadata.modality.toUpperCase(); const modality = metadata.modality.toUpperCase();
if (((validationMethod === 'allow') && !modalitiesSet.has(modality)) || if (((validationMethod === 'allow') && !modalitiesSet.has(modality)) ||
((validationMethod === 'restrict') && modalitiesSet.has(modality))) { ((validationMethod === 'deny') && modalitiesSet.has(modality))) {
measurements.push(measurement); measurements.push(measurement);
invalidModalities.push(modality); invalidModalities.push(modality);
} }

View File

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