LT-92: Criteria schema :: allow single item or array
This commit is contained in:
parent
777d5216f1
commit
2cc313a035
@ -6,30 +6,12 @@ import Ajv from 'ajv';
|
|||||||
export class CriteriaEvaluator {
|
export class CriteriaEvaluator {
|
||||||
|
|
||||||
constructor(criteriaObject) {
|
constructor(criteriaObject) {
|
||||||
|
const criteriaValidator = this.getCriteriaValidator();
|
||||||
this.criteria = [];
|
this.criteria = [];
|
||||||
|
|
||||||
const schema = {
|
if (!criteriaValidator(criteriaObject)) {
|
||||||
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 = '';
|
let message = '';
|
||||||
_.each(validator.errors, error => {
|
_.each(criteriaValidator.errors, error => {
|
||||||
message += `\noptions${error.dataPath} ${error.message}`;
|
message += `\noptions${error.dataPath} ${error.message}`;
|
||||||
});
|
});
|
||||||
throw new 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) {
|
evaluate(data) {
|
||||||
const nonconformity = [];
|
const nonconformity = [];
|
||||||
this.criteria.forEach(criterion => {
|
this.criteria.forEach(criterion => {
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import { BaseCriterion } from './BaseCriterion';
|
|||||||
import { _ } from 'meteor/underscore';
|
import { _ } from 'meteor/underscore';
|
||||||
|
|
||||||
export const MaxTargetsSchema = {
|
export const MaxTargetsSchema = {
|
||||||
|
type: 'object',
|
||||||
properties: {
|
properties: {
|
||||||
limit: {
|
limit: {
|
||||||
label: 'Max targets allowed in study',
|
label: 'Max targets allowed in study',
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import { BaseCriterion } from './BaseCriterion';
|
import { BaseCriterion } from './BaseCriterion';
|
||||||
|
|
||||||
export const MaxTargetsPerOrganSchema = {
|
export const MaxTargetsPerOrganSchema = {
|
||||||
|
type: 'object',
|
||||||
properties: {
|
properties: {
|
||||||
limit: {
|
limit: {
|
||||||
label: 'Max targets allowed per organ',
|
label: 'Max targets allowed per organ',
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import { BaseCriterion } from './BaseCriterion';
|
import { BaseCriterion } from './BaseCriterion';
|
||||||
|
|
||||||
export const MeasurementsLengthSchema = {
|
export const MeasurementsLengthSchema = {
|
||||||
|
type: 'object',
|
||||||
properties: {
|
properties: {
|
||||||
longAxis: {
|
longAxis: {
|
||||||
label: 'Minimum length of long axis',
|
label: 'Minimum length of long axis',
|
||||||
|
|||||||
@ -2,6 +2,7 @@ import { BaseCriterion } from './BaseCriterion';
|
|||||||
import { _ } from 'meteor/underscore';
|
import { _ } from 'meteor/underscore';
|
||||||
|
|
||||||
export const ModalitySchema = {
|
export const ModalitySchema = {
|
||||||
|
type: 'object',
|
||||||
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',
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import { BaseCriterion } from './BaseCriterion';
|
import { BaseCriterion } from './BaseCriterion';
|
||||||
|
|
||||||
export const NonTargetResponseSchema = {
|
export const NonTargetResponseSchema = {
|
||||||
|
type: 'object'
|
||||||
};
|
};
|
||||||
|
|
||||||
/* NonTargetResponseCriterion
|
/* NonTargetResponseCriterion
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
import { BaseCriterion } from './BaseCriterion';
|
import { BaseCriterion } from './BaseCriterion';
|
||||||
|
|
||||||
export const TargetTypeSchema = {
|
export const TargetTypeSchema = {
|
||||||
|
type: 'object'
|
||||||
};
|
};
|
||||||
|
|
||||||
/* TargetTypeCriterion
|
/* TargetTypeCriterion
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user