LT-92: Criteria schema :: allow single item or array

This commit is contained in:
Leonardo Campos 2017-01-14 01:36:04 -02:00 committed by Bruno Alves de Faria
parent 777d5216f1
commit 2cc313a035
7 changed files with 42 additions and 21 deletions

View File

@ -6,30 +6,12 @@ import Ajv from 'ajv';
export class CriteriaEvaluator {
constructor(criteriaObject) {
const criteriaValidator = this.getCriteriaValidator();
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)) {
if (!criteriaValidator(criteriaObject)) {
let message = '';
_.each(validator.errors, error => {
_.each(criteriaValidator.errors, error => {
message += `\noptions${error.dataPath} ${error.message}`;
});
throw new Error(message);
@ -42,6 +24,39 @@ export class CriteriaEvaluator {
});
}
getCriteriaValidator() {
if (CriteriaEvaluator.validator) {
return CriteriaEvaluator.validator;
}
const schema = {
properties: {},
definitions: {}
};
_.each(Criteria, (Criterion, key) => {
if (Criterion.prototype instanceof BaseCriterion) {
const criterionkey = key.replace(/Criterion$/, '');
const criterionDefinition = `#/definitions/${criterionkey}`;
schema.definitions[criterionkey] = Criteria[`${criterionkey}Schema`];
schema.properties[criterionkey] = {
oneOf: [
{ $ref: criterionDefinition },
{
type: 'array',
items: {
$ref: criterionDefinition
}
}
]
};
}
});
return CriteriaEvaluator.validator = new Ajv().compile(schema);
}
evaluate(data) {
const nonconformity = [];
this.criteria.forEach(criterion => {

View File

@ -2,6 +2,7 @@ import { BaseCriterion } from './BaseCriterion';
import { _ } from 'meteor/underscore';
export const MaxTargetsSchema = {
type: 'object',
properties: {
limit: {
label: 'Max targets allowed in study',

View File

@ -1,6 +1,7 @@
import { BaseCriterion } from './BaseCriterion';
export const MaxTargetsPerOrganSchema = {
type: 'object',
properties: {
limit: {
label: 'Max targets allowed per organ',

View File

@ -1,6 +1,7 @@
import { BaseCriterion } from './BaseCriterion';
export const MeasurementsLengthSchema = {
type: 'object',
properties: {
longAxis: {
label: 'Minimum length of long axis',

View File

@ -2,6 +2,7 @@ import { BaseCriterion } from './BaseCriterion';
import { _ } from 'meteor/underscore';
export const ModalitySchema = {
type: 'object',
properties: {
method: {
label: 'Specify if it\'s goinig to "allow" or "deny" the modalities',

View File

@ -1,6 +1,7 @@
import { BaseCriterion } from './BaseCriterion';
export const NonTargetResponseSchema = {
type: 'object'
};
/* NonTargetResponseCriterion

View File

@ -1,6 +1,7 @@
import { BaseCriterion } from './BaseCriterion';
export const TargetTypeSchema = {
type: 'object'
};
/* TargetTypeCriterion