UNPKG

analytica-frontend-lib

Version:

Repositório público dos componentes utilizados nas plataformas da Analytica Ensino

159 lines (147 loc) 6.51 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/components/SendActivityModal/validation.ts var _zod = require('zod'); var ERROR_MESSAGES = { SUBTYPE_REQUIRED: "Campo obrigat\xF3rio! Por favor, selecione uma op\xE7\xE3o para continuar.", MODE_REQUIRED: "Campo obrigat\xF3rio! Por favor, selecione o modo de prova para continuar.", TITLE_REQUIRED: "Campo obrigat\xF3rio! Por favor, preencha este campo para continuar.", STUDENTS_REQUIRED: "Campo obrigat\xF3rio! Por favor, selecione pelo menos um aluno para continuar.", START_DATE_REQUIRED: "Campo obrigat\xF3rio! Por favor, preencha este campo para continuar.", FINAL_DATE_REQUIRED: "Campo obrigat\xF3rio! Por favor, preencha este campo para continuar.", FINAL_DATE_INVALID: "A data final deve ser maior ou igual \xE0 data inicial." }; var activityStepSchema = _zod.z.object({ subtype: _zod.z.enum( ["TAREFA" /* TAREFA */, "TRABALHO" /* TRABALHO */, "PROVA" /* PROVA */], { error: () => ERROR_MESSAGES.SUBTYPE_REQUIRED } ), title: _zod.z.string({ error: () => ERROR_MESSAGES.TITLE_REQUIRED }).transform((val) => val.trim()).refine((val) => val.length > 0, { message: ERROR_MESSAGES.TITLE_REQUIRED }), notification: _zod.z.string().optional() }); var recipientStepSchema = _zod.z.object({ students: _zod.z.array( _zod.z.object({ studentId: _zod.z.string(), userInstitutionId: _zod.z.string() }), { error: () => ERROR_MESSAGES.STUDENTS_REQUIRED } ).min(1, ERROR_MESSAGES.STUDENTS_REQUIRED) }); var DATE_REGEX = /^\d{4}-\d{2}-\d{2}$/; var TIME_REGEX = /^\d{2}:\d{2}$/; var deadlineStepBaseSchema = _zod.z.object({ startDate: _zod.z.string({ error: () => ERROR_MESSAGES.START_DATE_REQUIRED }).min(1, ERROR_MESSAGES.START_DATE_REQUIRED).regex(DATE_REGEX, ERROR_MESSAGES.START_DATE_REQUIRED), startTime: _zod.z.string().regex(TIME_REGEX).default("00:00"), finalDate: _zod.z.string({ error: () => ERROR_MESSAGES.FINAL_DATE_REQUIRED }).min(1, ERROR_MESSAGES.FINAL_DATE_REQUIRED).regex(DATE_REGEX, ERROR_MESSAGES.FINAL_DATE_REQUIRED), finalTime: _zod.z.string().regex(TIME_REGEX).default("23:59"), canRetry: _zod.z.boolean().default(false) }); var deadlineStepSchema = deadlineStepBaseSchema.refine( (data) => { const start = /* @__PURE__ */ new Date(`${data.startDate}T${data.startTime}`); const end = /* @__PURE__ */ new Date(`${data.finalDate}T${data.finalTime}`); return end >= start; }, { message: ERROR_MESSAGES.FINAL_DATE_INVALID, path: ["finalDate"] } ); function validateActivityStep(data, options) { const errors = {}; if (_optionalChain([options, 'optionalAccess', _ => _.enableExamMode])) { if (!data.title || data.title.trim().length === 0) { errors.title = ERROR_MESSAGES.TITLE_REQUIRED; } return errors; } const result = activityStepSchema.safeParse({ subtype: data.subtype, title: data.title, notification: data.notification }); if (!result.success) { result.error.issues.forEach((issue) => { const field = issue.path[0]; if (field === "subtype" || field === "title") { errors[field] = issue.message; } }); } if (_optionalChain([options, 'optionalAccess', _2 => _2.enableExamMode]) && data.subtype === "PROVA" /* PROVA */ && !data.mode) { errors.mode = ERROR_MESSAGES.MODE_REQUIRED; } return errors; } function validateRecipientStep(data) { const errors = {}; const result = recipientStepSchema.safeParse({ students: _nullishCoalesce(data.students, () => ( [])) }); if (!result.success) { result.error.issues.forEach((issue) => { if (issue.path[0] === "students") { errors.students = issue.message; } }); } return errors; } function validateDeadlineStep(data, options) { const errors = {}; if (_optionalChain([options, 'optionalAccess', _3 => _3.enableExamMode])) { if (!data.startDate) { errors.startDate = ERROR_MESSAGES.START_DATE_REQUIRED; } return errors; } if (!data.startDate) { errors.startDate = ERROR_MESSAGES.START_DATE_REQUIRED; } if (!data.finalDate) { errors.finalDate = ERROR_MESSAGES.FINAL_DATE_REQUIRED; } if (data.startDate && data.finalDate) { const result = deadlineStepSchema.safeParse({ startDate: data.startDate, startTime: _nullishCoalesce(data.startTime, () => ( "00:00")), finalDate: data.finalDate, finalTime: _nullishCoalesce(data.finalTime, () => ( "23:59")), canRetry: _nullishCoalesce(data.canRetry, () => ( false)) }); if (!result.success) { result.error.issues.forEach((issue) => { const field = issue.path[0]; if (field === "startDate" || field === "finalDate") { errors[field] = issue.message; } }); } } return errors; } function validateStep(step, data, options) { switch (step) { case 1: return validateActivityStep(data, options); case 2: return validateRecipientStep(data); case 3: return validateDeadlineStep(data, options); default: return {}; } } function isStepValid(step, data, options) { const errors = validateStep(step, data, options); return Object.keys(errors).length === 0; } function isFormValid(data, options) { return isStepValid(1, data, options) && isStepValid(2, data) && isStepValid(3, data, options); } exports.ERROR_MESSAGES = ERROR_MESSAGES; exports.activityStepSchema = activityStepSchema; exports.recipientStepSchema = recipientStepSchema; exports.deadlineStepSchema = deadlineStepSchema; exports.validateActivityStep = validateActivityStep; exports.validateRecipientStep = validateRecipientStep; exports.validateDeadlineStep = validateDeadlineStep; exports.validateStep = validateStep; exports.isStepValid = isStepValid; exports.isFormValid = isFormValid; //# sourceMappingURL=chunk-AVDZHON5.js.map