Can now do basic validations of form submits.

This commit is contained in:
Zed A. Shaw 2025-06-04 00:24:59 -04:00
parent 4045799ab9
commit c5f39b3fa3
3 changed files with 29 additions and 3 deletions

17
main.go
View file

@ -8,13 +8,15 @@ import (
_ "github.com/mattn/go-sqlite3"
"github.com/jmoiron/sqlx"
sq "github.com/Masterminds/squirrel"
"github.com/go-playground/validator/v10"
)
type Link struct {
Id int `db:"id" json:"id"`
StreamId int `db:"stream_id" json:"stream_id" form:"stream_id"`
Url string `db:"url" json:"url" form:"url"`
Description string `db:"description" json:"description" form:"description"`
StreamId int `db:"stream_id" json:"stream_id" form:"stream_id" validate:"required,numeric"`
Url string `db:"url" json:"url" form:"url" validate:"required,url"`
Description string `db:"description" json:"description" form:"description" validate:"required"`
}
type Stream struct {
@ -88,6 +90,15 @@ func main() {
return c.Redirect("/live/")
}
var validate *validator.Validate
validate = validator.New(validator.WithRequiredStructEnabled())
if err := validate.Struct(link); err != nil {
validationErrors := err.(validator.ValidationErrors)
log.Println(validationErrors)
return c.Redirect("/live/")
}
sql, args, err := sq.Insert("stream_link").Columns("stream_id", "url", "description").Values(link.StreamId, link.Url, link.Description).ToSql()
if err != nil {