fix: validar sintaxis Go template al guardar plantilla + import html/template

This commit is contained in:
Lizandro Guarnizo
2026-04-30 22:38:52 -05:00
parent 92ef6d48e8
commit 46c89457dc
@@ -1,6 +1,7 @@
package controllers
import (
"html/template"
"math"
"strconv"
@@ -42,6 +43,11 @@ func CreatePlantilla(c *fiber.Ctx) error {
if err := c.BodyParser(&m); err != nil {
return c.Status(400).JSON(fiber.Map{"error": err.Error()})
}
if m.CuerpoHTML != "" {
if _, err := template.New("validate").Parse(m.CuerpoHTML); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Sintaxis inválida en el HTML: " + err.Error()})
}
}
if err := models.CreatePlantilla(m); err != nil {
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
}
@@ -57,6 +63,11 @@ func UpdatePlantilla(c *fiber.Ctx) error {
if err := c.BodyParser(&updates); err != nil {
return c.Status(400).JSON(fiber.Map{"error": err.Error()})
}
if html, ok := updates["cuerpo_html"].(string); ok && html != "" {
if _, err := template.New("validate").Parse(html); err != nil {
return c.Status(400).JSON(fiber.Map{"error": "Sintaxis inválida en el HTML: " + err.Error()})
}
}
if err := models.UpdatePlantilla(uint(id), updates); err != nil {
return c.Status(500).JSON(fiber.Map{"error": err.Error()})
}