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

This commit is contained in:
Leonardo Campos authored and Bruno Alves de Faria committed 2017-01-18 11:01:52 -02:00
1 parent 777d5216f1
commit 2cc313a035
7 files changed
+42 -21

No files matched your search

@@ -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 => {
@@ -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',
@@ -1,6 +1,7 @@
import { BaseCriterion } from './BaseCriterion';
export const MaxTargetsPerOrganSchema = {
type: 'object',
properties: {
limit: {
label: 'Max targets allowed per organ',
@@ -1,6 +1,7 @@
import { BaseCriterion } from './BaseCriterion';
export const MeasurementsLengthSchema = {
type: 'object',
properties: {
longAxis: {
label: 'Minimum length of long axis',
@@ -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',
@@ -1,6 +1,7 @@
import { BaseCriterion } from './BaseCriterion';
export const NonTargetResponseSchema = {
type: 'object'
};
/* NonTargetResponseCriterion
@@ -1,6 +1,7 @@
import { BaseCriterion } from './BaseCriterion';
export const TargetTypeSchema = {
type: 'object'
};
/* TargetTypeCriterion