@rss/common
Advanced tools
Comparing version 0.0.68 to 0.0.69
@@ -124,14 +124,2 @@ 'use strict'; | ||
}] | ||
}; | ||
module.exports.dependentQuestion = { | ||
type: 'group', | ||
key: 'personnel', | ||
required: true, | ||
questions: [{ | ||
type: 'people', | ||
key: 'personnel_authorized_to_work', | ||
required: true, | ||
questions: [module.exports.peopleGroup] | ||
}] | ||
}; |
'use strict'; | ||
var FqType = require('../../constant/flexible-questionnaire/FqType.constant'); | ||
var FqType = require('../../constant/flexible-questionnaire/fq-type.constant'); | ||
module.exports.hasError = function (question, answer) { | ||
var hasError = function hasError(question, answer) { | ||
if (!question.required && question.type !== 'group') return false; | ||
if (!answer && question.required) { | ||
return true; | ||
} | ||
var questionAnswer = answer[question.key]; | ||
@@ -12,32 +16,41 @@ if (questionAnswer && !questionAnswer) { | ||
} | ||
switch (question.type) { | ||
case FqType.RADIO: | ||
return answer && !answer; | ||
case FqType.CHECKBOX: | ||
return typeof questionAnswer === 'undefined' || questionAnswer && !questionAnswer.length; // TODO Validate responses are in question options | ||
case FqType.ATTACHMENT: | ||
return answer && !answer.length; // TODO Validate responses are in question options | ||
case FqType.DROPDOWN: | ||
return !questionAnswer.length; | ||
return questionAnswer && !questionAnswer.length; | ||
case FqType.GROUP: | ||
return module.exports.hasGroupError(question, questionAnswer); | ||
case 'people': | ||
case 'location': | ||
case 'agent': | ||
return module.exports.hasGroupError(question, questionAnswer); | ||
return answer && !answer.length; | ||
default: | ||
return question.required && !questionAnswer; | ||
return question.required && !answer; | ||
} | ||
}; | ||
module.exports.hasGroupError = function (questionGroup, answers) { | ||
if (questionGroup.required && !answers) { | ||
return true; | ||
} | ||
var hasGroupError = function hasGroupError(questionGroup, answers) { | ||
return questionGroup.questions.some(function (q) { | ||
if (q.type === FqType.DEPENDENT) { | ||
if (answers[q.parentKey] === q.parentValue) { | ||
return module.exports.hasGroupError(q, answers); | ||
if (answers) { | ||
if (q.type === FqType.GROUP) { | ||
return hasGroupError(q, answers[q.key]); | ||
} | ||
return false; | ||
if (q.type === FqType.DEPENDENT) { | ||
if (answers[q.parentKey] === q.parentValue) { | ||
return hasGroupError(q, answers[q.key]); | ||
} else if (Array.isArray(answers[q.parentKey])) { | ||
return hasGroupError(q, answers[q.key]); | ||
} | ||
return false; | ||
} | ||
return q.required && hasError(q, answers[q.key]); | ||
} | ||
return module.exports.hasError(q, answers); | ||
return q.required; | ||
}); | ||
}; | ||
}; | ||
module.exports = { hasError: hasError, hasGroupError: hasGroupError }; |
@@ -40,6 +40,6 @@ 'use strict'; | ||
}, { | ||
key: 'validate', | ||
value: function validate(questions, answers) { | ||
return questions.some(function (question) { | ||
return !!errorValidator.hasError(question, answers) === false; | ||
key: 'isValid', | ||
value: function isValid(questions, answers) { | ||
return !questions.some(function (question) { | ||
return errorValidator.hasError(question, answers); | ||
}); | ||
@@ -46,0 +46,0 @@ } |
@@ -28,2 +28,3 @@ 'use strict'; | ||
_this.templateType = null; | ||
_this.templatePrefix = null; | ||
_this.stateId = null; | ||
@@ -50,2 +51,3 @@ _this.rp = null; | ||
this.templateType = opt.templateType; | ||
this.templatePrefix = opt.templatePrefix; | ||
this.stateId = new ObjectId(opt.stateId); | ||
@@ -52,0 +54,0 @@ this.rp = new PersonReference(opt.rp); |
@@ -125,2 +125,3 @@ // constant - core | ||
exports.FQValidator = require('./dist/helper/flexible-questionnaire/validator'); | ||
exports.FqType = require('./dist/constant/flexible-questionnaire/FqType.constant'); | ||
exports.FqType = require('./dist/constant/flexible-questionnaire/fq-type.constant'); | ||
exports.FqHelper = require('./dist/helper/flexible-questionnaire/fq-helper'); |
{ | ||
"name": "@rss/common", | ||
"version": "0.0.68", | ||
"version": "0.0.69", | ||
"description": "common constant, classes, & helper", | ||
@@ -5,0 +5,0 @@ "author": "Risk & Safety Solution", |
@@ -150,15 +150,1 @@ module.exports.inputQuestion = { | ||
}; | ||
module.exports.dependentQuestion = { | ||
type: 'group', | ||
key: 'personnel', | ||
required: true, | ||
questions: [ | ||
{ | ||
type: 'people', | ||
key: 'personnel_authorized_to_work', | ||
required: true, | ||
questions: [module.exports.peopleGroup], | ||
}, | ||
], | ||
}; |
@@ -1,6 +0,10 @@ | ||
const FqType = require('../../constant/flexible-questionnaire/FqType.constant'); | ||
const FqType = require('../../constant/flexible-questionnaire/fq-type.constant'); | ||
module.exports.hasError = (question, answer) => { | ||
const hasError = (question, answer) => { | ||
if (!question.required && question.type !== 'group') return false; | ||
if (!answer && question.required) { | ||
return true; | ||
} | ||
const questionAnswer = answer[question.key]; | ||
@@ -10,32 +14,40 @@ if (questionAnswer && !questionAnswer) { | ||
} | ||
switch (question.type) { | ||
case FqType.RADIO: | ||
return answer && !answer; | ||
case FqType.CHECKBOX: | ||
return typeof questionAnswer === 'undefined' || (questionAnswer && !questionAnswer.length); // TODO Validate responses are in question options | ||
case FqType.ATTACHMENT: | ||
return answer && !answer.length; // TODO Validate responses are in question options | ||
case FqType.DROPDOWN: | ||
return !questionAnswer.length; | ||
return questionAnswer && !questionAnswer.length; | ||
case FqType.GROUP: | ||
return module.exports.hasGroupError(question, questionAnswer); | ||
case 'people': | ||
case 'location': | ||
case 'agent': | ||
return module.exports.hasGroupError(question, questionAnswer); | ||
return answer && !answer.length; | ||
default: | ||
return question.required && !questionAnswer; | ||
return question.required && !answer; | ||
} | ||
}; | ||
module.exports.hasGroupError = (questionGroup, answers) => { | ||
if (questionGroup.required && !answers) { | ||
return true; | ||
} | ||
return questionGroup.questions.some((q) => { | ||
if (q.type === FqType.DEPENDENT) { | ||
if (answers[q.parentKey] === q.parentValue) { | ||
return module.exports.hasGroupError(q, answers); | ||
const hasGroupError = (questionGroup, answers) => | ||
questionGroup.questions.some((q) => { | ||
if (answers) { | ||
if (q.type === FqType.GROUP) { | ||
return hasGroupError(q, answers[q.key]); | ||
} | ||
return false; | ||
if (q.type === FqType.DEPENDENT) { | ||
if (answers[q.parentKey] === q.parentValue) { | ||
return hasGroupError(q, answers[q.key]); | ||
} else if (Array.isArray(answers[q.parentKey])) { | ||
return hasGroupError(q, answers[q.key]); | ||
} | ||
return false; | ||
} | ||
return q.required && hasError(q, answers[q.key]); | ||
} | ||
return module.exports.hasError(q, answers); | ||
return q.required; | ||
}); | ||
}; | ||
module.exports = { hasError, hasGroupError }; |
@@ -6,3 +6,3 @@ const helper = require('./fq-helper'); | ||
it('should find no error in form question - TextBox', () => { | ||
const answer = { project_title: 'test' }; | ||
const answer = 'test'; | ||
@@ -13,3 +13,3 @@ expect(helper.hasError(data.inputQuestion, answer)).toBeFalsy(); | ||
it('should find error in form question - TextBox', () => { | ||
const answer = { project_title: '' }; | ||
const answer = ''; | ||
@@ -26,10 +26,8 @@ expect(helper.hasError(data.inputQuestion, answer)).toBeTruthy(); | ||
it('should find no error in form question - checkBox', () => { | ||
const answer = { | ||
rDNA_or_pathogens: [ | ||
{ | ||
label: 'Whole plants', | ||
value: 'Whole_plants', | ||
}, | ||
], | ||
}; | ||
const answer = [ | ||
{ | ||
label: 'Whole plants', | ||
value: 'Whole_plants', | ||
}, | ||
]; | ||
@@ -85,18 +83,14 @@ expect(helper.hasError(data.checkboxQuestion, answer)).toBeFalsy(); | ||
const answer = { | ||
personnel: { | ||
personnel_authorized_to_work: { | ||
personnel_followup: { | ||
edit_bua_on_behalf_of_pi: 'No', | ||
person_working_with: [ | ||
{ | ||
label: 'Bloodborne pathogen', | ||
value: 'Bloodborne_pathogen', | ||
}, | ||
], | ||
personnel_followup: { | ||
edit_bua_on_behalf_of_pi: 'No', | ||
person_working_with: [ | ||
{ | ||
label: 'Bloodborne pathogen', | ||
value: 'Bloodborne_pathogen', | ||
}, | ||
}, | ||
], | ||
}, | ||
}; | ||
expect(helper.hasError(data.dependentQuestion, answer)).toBeFalsy(); | ||
expect(helper.hasError(data.peopleGroup, answer)).toBeFalsy(); | ||
}); | ||
@@ -121,3 +115,3 @@ | ||
expect(helper.hasError(data.dependentQuestion, answer)).toBeTruthy(); | ||
expect(helper.hasError(data.peopleGroup, answer)).toBeTruthy(); | ||
}); | ||
@@ -132,3 +126,3 @@ | ||
expect(helper.hasError(data.dependentQuestion, answer)).toBeTruthy(); | ||
expect(helper.hasError(data.peopleGroup, answer)).toBeTruthy(); | ||
}); | ||
@@ -138,25 +132,23 @@ | ||
const answer = { | ||
personnel: { | ||
personnel_authorized_to_work: { | ||
personnel_followup: { | ||
edit_bua_on_behalf_of_pi: 'Yes', | ||
contact_type: [ | ||
{ | ||
label: 'Lab', | ||
value: 'Lab', | ||
}, | ||
], | ||
person_working_with: [ | ||
{ | ||
label: 'Bloodborne pathogen', | ||
value: 'Bloodborne_pathogen', | ||
}, | ||
], | ||
personnel_followup: { | ||
edit_bua_on_behalf_of_pi: 'Yes', | ||
edit_bua_on_behalf_of_pi_yes: { | ||
contact_type: [ | ||
{ | ||
label: 'Lab', | ||
value: 'Lab', | ||
}, | ||
], | ||
}, | ||
person_working_with: [ | ||
{ | ||
label: 'Bloodborne pathogen', | ||
value: 'Bloodborne_pathogen', | ||
}, | ||
}, | ||
], | ||
}, | ||
}; | ||
expect(helper.hasError(data.dependentQuestion, answer)).toBeFalsy(); | ||
expect(helper.hasError(data.peopleGroup, answer)).toBeFalsy(); | ||
}); | ||
}); |
@@ -30,4 +30,4 @@ const ObjectId = require('bson').ObjectID; | ||
validate(questions, answers) { | ||
return questions.some((question) => !!errorValidator.hasError(question, answers) === false); | ||
isValid(questions, answers) { | ||
return !questions.some((question) => errorValidator.hasError(question, answers)); | ||
} | ||
@@ -34,0 +34,0 @@ } |
@@ -19,3 +19,3 @@ const FlexibleQuestionnaire = require('./flexible-questionnaire'); | ||
expect(fq.validate(questions, answers)).toBeTruthy(); | ||
expect(fq.isValid(questions, answers)).toBeTruthy(); | ||
}); | ||
@@ -26,4 +26,4 @@ | ||
expect(fq.validate(questions, answers)).toBeFalsy(); | ||
expect(fq.isValid(questions, answers)).toBeFalsy(); | ||
}); | ||
}); |
@@ -13,2 +13,3 @@ const ObjectId = require('bson').ObjectID; | ||
this.templateType = null; | ||
this.templatePrefix = null; | ||
this.stateId = null; | ||
@@ -32,2 +33,3 @@ this.rp = null; | ||
this.templateType = opt.templateType; | ||
this.templatePrefix = opt.templatePrefix; | ||
this.stateId = new ObjectId(opt.stateId); | ||
@@ -34,0 +36,0 @@ this.rp = new PersonReference(opt.rp); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
503174
250
13739