trc-client-core
Version:
The core of the TRC Client
71 lines (54 loc) • 2.04 kB
JavaScript
const WORKSHOP_NAME_ERROR = 'You must supply a workshop name';
const ABBREVIATED_NAME_ERROR = 'You must supply an Abbreviated Name';
const ATTENDANCE_ERROR = 'You must supply a value for Attendance';
const COURSE_CODE_ERROR = 'You must supply a Course Code';
const COURSE_ICON_ERROR = 'You must supply a Course Icon';
const DEPARTMENT_CATEGORY_ERROR = 'You must supply a Department Category';
const DURATION_ERROR = 'You must supply a Course Duration';
const LEARNING_OUTCOMES_ERROR = 'You must supply at least one learning outcome';
const OVERVIEW_ERROR = 'You must supply a training overview of at least 25 characters';
const TRAINING_HOURS_ERROR = 'Training Hours must be a number';
const PRICE_ERROR = 'Price must be a number';
function isNumber (val) {
return /^\d+$/.test(val);
}
function isNotLongEnough(str) {
return str && str.replace(/ /g, '').length < 25;
}
export default function CourseValidation(data) {
var errors = {};
if(!data.workshopName) {
errors.workshopName = WORKSHOP_NAME_ERROR;
}
if(!data.abbreviatedName) {
errors.abbreviatedName = ABBREVIATED_NAME_ERROR;
}
if(!data.attendance) {
errors.attendance = ATTENDANCE_ERROR;
}
if(!data.courseCode) {
errors.courseCode = COURSE_CODE_ERROR;
}
if(!data.courseIcon) {
errors.courseIcon = COURSE_ICON_ERROR;
}
if(!data.departmentCategory) {
errors.departmentCategory = DEPARTMENT_CATEGORY_ERROR;
}
if(!data.duration) {
errors.duration = DURATION_ERROR;
}
if(!data.learningOutcomes) {
errors.learningOutcomes = LEARNING_OUTCOMES_ERROR;
}
if(isNotLongEnough(data.overview)) {
errors.overview = OVERVIEW_ERROR;
}
if(!isNumber(data.trainingHours)) {
errors.trainingHours = TRAINING_HOURS_ERROR;
}
if(!isNumber(data.price)) {
errors.price = PRICE_ERROR;
}
return errors;
}