function validateTask(raw) { const value = JSON.parse(raw); if (!value || typeof value !== "object" || Array.isArray(value)) { throw new Error("Se esperaba un objeto"); } const keys = Object.keys(value).sort().join(","); if (keys !== "priority,title" || typeof value.title !== "string" || value.title.trim().length === 0 || !["normal", "urgent"].includes(value.priority)) { throw new Error("La tarea no cumple el contrato"); } return value; } const task = validateTask('{"title":"Revisar contrato","priority":"normal"}'); console.log("Dato válido: " + task.title); try { validateTask('{"title":42,"priority":"normal"}'); throw new Error("El validador aceptó un dato incorrecto"); } catch (error) { if (error.message !== "La tarea no cumple el contrato") throw error; console.log("Dato incorrecto rechazado"); }