UNPKG

linagora-rse

Version:
71 lines (60 loc) 1.44 kB
const moment = require('moment'); const { buildErrorMessage, createValidateFunction } = require('../validator/helper'); const TIME_FORMAT = 'H:m'; const schema = { type: 'array', minItems: 1, maxItems: 1, items: { type: 'object', properties: { start: { type: 'string' }, end: { type: 'string' }, daysOfWeek: { type: 'array', uniqueItems: true, items: { type: 'integer', minimum: 0, maximum: 6 } } }, required: ['start', 'end', 'daysOfWeek'], additionalProperties: false } }; const validate = createValidateFunction(schema); module.exports = { rights: { padmin: 'rw', admin: 'rw', user: 'rw' }, validator }; function validator(businessHours) { const valid = validate(businessHours); if (!valid) { return buildErrorMessage(validate.errors); } return validateBusinessHour(businessHours[0]); } function validateBusinessHour(businessHour) { const startObj = moment(businessHour.start, TIME_FORMAT, true); const endObj = moment(businessHour.end, TIME_FORMAT, true); if (!startObj.isValid()) { return `start must be in time format: ${TIME_FORMAT}`; } if (!endObj.isValid()) { return `end must be in time format: ${TIME_FORMAT}`; } if (!startObj.isBefore(endObj)) { return 'start time must be before the end time'; } return null; }