Skip to content

Commit

Permalink
Rename Errors to Validator
Browse files Browse the repository at this point in the history
  • Loading branch information
theandrew168 committed Sep 8, 2024
1 parent 4de1abe commit 2b0b93e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 31 deletions.
14 changes: 7 additions & 7 deletions backend/web/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ func HandleRegisterForm(repo *repository.Repository) http.Handler {
password := r.PostForm.Get("password")

// Validate the form values.
e := util.NewErrors()
e.CheckRequired("username", username)
e.CheckRequired("password", password)
v := util.NewValidator()
v.CheckRequired("username", username)
v.CheckRequired("password", password)

// If the form isn't valid, re-render the template with existing input values.
if !e.OK() {
if !v.IsValid() {
data := page.RegisterData{
Username: username,
Errors: e,
Errors: v,
}
util.Render(w, r, http.StatusBadRequest, func(w io.Writer) error {
return tmpl.Render(w, data)
Expand All @@ -68,10 +68,10 @@ func HandleRegisterForm(repo *repository.Repository) http.Handler {
switch {
case errors.Is(err, postgres.ErrConflict):
// If a conflict occurs, re-render the form with an error.
e.Add("username", "Username is already taken")
v.Add("username", "Username is already taken")
data := page.RegisterData{
Username: username,
Errors: e,
Errors: v,
}
util.Render(w, r, http.StatusBadRequest, func(w io.Writer) error {
return tmpl.Render(w, data)
Expand Down
22 changes: 11 additions & 11 deletions backend/web/signin.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ func HandleSigninForm(repo *repository.Repository) http.Handler {
password := r.PostForm.Get("password")

// Validate the form values.
e := util.NewErrors()
e.CheckRequired("username", username)
e.CheckRequired("password", password)
v := util.NewValidator()
v.CheckRequired("username", username)
v.CheckRequired("password", password)

// If the form isn't valid, re-render the template with existing input values.
if !e.OK() {
if !v.IsValid() {
data := page.SigninData{
Username: username,
Errors: e,
Errors: v,
}
util.Render(w, r, http.StatusBadRequest, func(w io.Writer) error {
return tmpl.Render(w, data)
Expand All @@ -58,11 +58,11 @@ func HandleSigninForm(repo *repository.Repository) http.Handler {
if err != nil {
switch {
case errors.Is(err, postgres.ErrNotFound):
e.Add("username", "Invalid username or password")
e.Add("password", "Invalid username or password")
v.Add("username", "Invalid username or password")
v.Add("password", "Invalid username or password")
data := page.SigninData{
Username: username,
Errors: e,
Errors: v,
}
util.Render(w, r, http.StatusBadRequest, func(w io.Writer) error {
return tmpl.Render(w, data)
Expand All @@ -75,11 +75,11 @@ func HandleSigninForm(repo *repository.Repository) http.Handler {

ok := account.PasswordMatches(password)
if !ok {
e.Add("username", "Invalid username or password")
e.Add("password", "Invalid username or password")
v.Add("username", "Invalid username or password")
v.Add("password", "Invalid username or password")
data := page.SigninData{
Username: username,
Errors: e,
Errors: v,
}
util.Render(w, r, http.StatusBadRequest, func(w io.Writer) error {
return tmpl.Render(w, data)
Expand Down
26 changes: 13 additions & 13 deletions backend/web/util/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,32 @@ import (
"unicode/utf8"
)

type Errors map[string]string
type Validator map[string]string

func NewErrors() Errors {
return make(Errors)
func NewValidator() Validator {
return make(Validator)
}

func (e Errors) OK() bool {
return len(e) == 0
func (v Validator) IsValid() bool {
return len(v) == 0
}

func (e Errors) Add(field, message string) {
e[field] = message
func (v Validator) Add(field, message string) {
v[field] = message
}

func (e Errors) Check(field, message string, ok bool) {
func (v Validator) Check(field, message string, ok bool) {
if !ok {
e[field] = message
v[field] = message
}
}

func (e Errors) CheckRequired(field, value string) {
func (v Validator) CheckRequired(field, value string) {
message := "This field is required"
e.Check(field, message, strings.TrimSpace(value) != "")
v.Check(field, message, strings.TrimSpace(value) != "")
}

func (e Errors) CheckMaxCharacters(field, value string, n int) {
func (v Validator) CheckMaxCharacters(field, value string, n int) {
message := fmt.Sprintf("This field cannot me more than %d characters long", n)
e.Check(field, message, utf8.RuneCountInString(value) <= n)
v.Check(field, message, utf8.RuneCountInString(value) <= n)
}

0 comments on commit 2b0b93e

Please sign in to comment.