Skip to content

Commit

Permalink
Merge pull request #41 from cploutarchou/37-create-a-validation-package
Browse files Browse the repository at this point in the history
37 create a validation package
  • Loading branch information
cploutarchou committed Jun 1, 2022
2 parents c09a324 + 1a10e09 commit 38a4602
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
8 changes: 8 additions & 0 deletions microGo/terminal/cli/templates/data/user.go.txt
@@ -1,6 +1,7 @@
package data

import (
microGo "cloud0.christosploutarchou.com/cploutarchou/MicroGO"
"errors"
"time"

Expand All @@ -26,6 +27,13 @@ func (u *User) Table() string {
return "users"
}

func (u *User) Validate(validator *microGo.Validation) {
validator.Check(u.LastName != "", "last_name", "Last name must be provided")
validator.Check(u.FirstName != "", "first_name", "First name must be provided")
validator.Check(u.Email != "", "email", "Email must be provided")
validator.IsEmail("email", u.Email)
}

// GetAll returns a slice of all users
func (u *User) GetAll() ([]*User, error) {
collection := upper.Collection(u.Table())
Expand Down
38 changes: 21 additions & 17 deletions microGo/validator.go
@@ -1,34 +1,34 @@
package microGo

import (
"github.com/asaskevich/govalidator"
"net/http"
"net/url"
"strconv"
"strings"
"time"

"github.com/asaskevich/govalidator"
)

type Validation struct {
Data url.Values
Error map[string]string
Data url.Values
Errors map[string]string
}

func (m *MicroGo) Validator(data url.Values) *Validation {
return &Validation{
Error: make(map[string]string),
Data: data,
Errors: make(map[string]string),
Data: data,
}

}

func (v *Validation) Valid() bool {
return len(v.Error) == 0
return len(v.Errors) == 0
}

func (v *Validation) AddError(key, message string) {
if _, exists := v.Error[key]; !exists {
v.Error[key] = message
if _, exists := v.Errors[key]; !exists {
v.Errors[key] = message
}
}

Expand All @@ -39,11 +39,12 @@ func (v *Validation) Has(field string, r *http.Request) bool {
}
return true
}

func (v *Validation) Required(r *http.Request, fields ...string) {
for _, field := range fields {
value := r.Form.Get(field)
if strings.TrimSpace(value) == "" {
v.AddError(field, "Required field cannot be empty!")
v.AddError(field, "This field cannot be blank")
}
}
}
Expand All @@ -56,30 +57,33 @@ func (v *Validation) Check(ok bool, key, message string) {

func (v *Validation) IsEmail(field, value string) {
if !govalidator.IsEmail(value) {
v.AddError(field, "No valid email address provided")
v.AddError(field, "Invalid email address")
}
}

func (v *Validation) IsInt(field, value string) {
_, err := strconv.Atoi(value)
if err != nil {
v.AddError(field, "integer value required")
v.AddError(field, "This field must be an integer")
}
}

func (v *Validation) IsFloat(field, value string) {
_, err := strconv.ParseFloat(value, 64)
if err != nil {
v.AddError(field, "float number required")
v.AddError(field, "This field must be a floating point number")
}
}
func (v *Validation) IsIsoDate(field, value string) {
_, err := time.Parse("2022-05-18", value)

func (v *Validation) IsDateISO(field, value string) {
_, err := time.Parse("2006-01-02", value)
if err != nil {
v.AddError(field, "No valid iso date provided. Valid Format YYYY-MM-DD : ")
v.AddError(field, "This field must be a date in the form of YYYY-MM-DD")
}
}

func (v *Validation) NoSpaces(field, value string) {
if govalidator.HasWhitespace(value) {
v.AddError(field, "Spaces are not allowed")
v.AddError(field, "Spaces are not permitted")
}
}

0 comments on commit 38a4602

Please sign in to comment.