fix: validar sintaxis Go template al guardar plantilla + import html/template
This commit is contained in:
@@ -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()})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user