All files / src/utils schemaValidator.ts

100% Statements 66/66
100% Branches 0/0
100% Functions 0/0
100% Lines 66/66

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 1371x     1x       1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x                                                                                                        
import { t } from 'elysia';
 
// Definición de valores constantes para opciones
export const OPTION_VALUES = ['yes', 'partial', 'no', 'na'] as const;
export type OptionValue = (typeof OPTION_VALUES)[number];
 
// Esquema para la validación de opciones de respuesta
export const optionValidator = t.Object({
	value: t.String({
		enum: OPTION_VALUES,
		error: 'El valor debe ser "yes", "partial", "no" o "na"',
	}),
	label: t.String({ minLength: 1, maxLength: 50 }),
	description: t.String({ minLength: 0, maxLength: 500 }),
});
 
// Esquema para la validación de preguntas
export const questionValidator = t.Object({
	id: t.String({ minLength: 1, maxLength: 50 }),
	text: t.String({ minLength: 1, maxLength: 500 }),
	options: t.Array(optionValidator),
	response: t.Union([t.String({ enum: OPTION_VALUES }), t.Null()]),
	observations: t.String({ maxLength: 1000 }),
	evidence_url: t.String({ maxLength: 500 }),
});
 
// Esquema para la validación de subsecciones
export const subsectionValidator = t.Object({
	subsection: t.String({ minLength: 1, maxLength: 50 }),
	title: t.String({ minLength: 1, maxLength: 100 }),
	questions: t.Array(questionValidator),
});
 
// Esquema para la validación de secciones
export const sectionValidator = t.Object({
	section: t.String({ minLength: 1, maxLength: 50 }),
	title: t.String({ minLength: 1, maxLength: 100 }),
	subsections: t.Array(subsectionValidator),
});
 
// Esquema para la auditoría NIST completa
export const auditValidator = t.Object({
	program: t.String({
		minLength: 1,
		maxLength: 100,
		error: 'El nombre del programa es requerido',
	}),
	sections: t.Array(sectionValidator),
});
 
// Esquema para el ID de auditoría
export const auditIdValidator = t.Object({
	id: t.String({
		minLength: 1,
		error: 'El ID de auditoría es requerido',
	}),
});
 
// Esquema para la recepción de los resultados de auditoría
export const auditResultValidator = t.Object({
	id: t.Optional(t.String()),
	auditDate: t.Date(),
	completionPercentage: t.Number({ minimum: 0, maximum: 100 }),
	sections: t.Record(
		t.String(),
		t.Object({
			completionPercentage: t.Number({ minimum: 0, maximum: 100 }),
			questions: t.Record(
				t.String(),
				t.Object({
					response: t.Union([t.String({ enum: OPTION_VALUES }), t.Null()]),
					observations: t.String(),
					evidence_url: t.String(),
				})
			),
		})
	),
});
 
// Esquema para las respuestas de errores comunes
export const errorResponseValidator = t.Object({
	success: t.Boolean(),
	message: t.String(),
	errorCode: t.Optional(t.String()),
});
 
// Ejemplo de cómo hacer una petición para guardar una auditoría
/**
 * Para hacer una petición POST basada en esta estructura:
 *
 * ```javascript
 * // Ejemplo de uso en un cliente
 * const response = await fetch('/api/audit', {
 *   method: 'POST',
 *   headers: {
 *     'Content-Type': 'application/json',
 *   },
 *   body: JSON.stringify({
 *     program: "Programa de Auditoría para NIST 800-30 (Ciclo PDCA)",
 *     sections: [
 *       {
 *         section: "1",
 *         title: "PLANIFICAR (PLAN)",
 *         subsections: [
 *           {
 *             subsection: "1.1",
 *             title: "Risk Framing",
 *             questions: [
 *               {
 *                 id: "1.1.1",
 *                 text: "¿Existe una estrategia de gestión de riesgos documentada que incluya roles y responsabilidades?",
 *                 options: [
 *                   { value: "yes", label: "Sí", description: "Estrategia documentada y aprobada" },
 *                   { value: "partial", label: "Parcialmente", description: "Estrategia en desarrollo o incompleta" },
 *                   { value: "no", label: "No", description: "Sin estrategia documentada" },
 *                   { value: "na", label: "No aplica", description: "" }
 *                 ],
 *                 response: "yes",
 *                 observations: "Se verificó la existencia del documento en el repositorio",
 *                 evidence_url: "https://ejemplo.com/evidencia1.pdf"
 *               },
 *               // Más preguntas...
 *             ]
 *           },
 *           // Más subsecciones...
 *         ]
 *       },
 *       // Más secciones...
 *     ]
 *   })
 * });
 *
 * const result = await response.json();
 * // Result contendrá el ID de la auditoría y otros datos del resultado
 * ```
 */