# HG changeset patch # User Yader Velasquez # Date 1671232814 21600 # Fri Dec 16 17:20:14 2022 -0600 # Node ID b84c7d8565789bc47e43a13f166eb0c8d1e2c4dc # Parent 0000000000000000000000000000000000000000 Init diff --git a/formguard.go b/formguard.go new file mode 100644 --- /dev/null +++ b/formguard.go @@ -0,0 +1,55 @@ +package formguard + +import ( + "errors" + "html/template" + "strings" + + "github.com/labstack/echo/v4" + "hg.code.netlandish.com/~netlandish/gobwebs/validate" +) + +// Form ... +type Form struct { + AntiSpam int `form:"antispam"` +} + +// XXX hardcoded for now +var FormGuardMinValue int = 5 + +// Validate ... +func (f *Form) Validate(c echo.Context) error { + errs := echo.FormFieldBinder(c). + FailFast(false). + Int("antispam", &f.AntiSpam). + BindErrors() + if errs != nil { + return validate.GetInputErrors(errs) + } + + if err := c.Validate(f); err != nil { + return err + } + + if f.AntiSpam < FormGuardMinValue { + return errors.New("This operation is forbidden") + } + + return nil +} + +// ScriptJS ... +func ScriptJS(ids ...string) template.HTML { + script := `` + script = strings.Replace(script, "", strings.Join(ids, ","), 1) + return template.HTML(script) +} + +// InputHTML ... +func InputHTML(id string) template.HTML { + input := "" + input = strings.Replace(input, "", id, 1) + return template.HTML(input) +} diff --git a/go.mod b/go.mod new file mode 100644 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module example.com/gobwebs-formguard + +go 1.19 diff --git a/script.js b/script.js new file mode 100644 --- /dev/null +++ b/script.js @@ -0,0 +1,1 @@ +console.log("This is a test");