Skip to content

Commit

Permalink
feat: Added validator service
Browse files Browse the repository at this point in the history
  • Loading branch information
ralvarezdev committed Dec 18, 2024
1 parent 8937ae9 commit 7d0fb6b
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 6 deletions.
2 changes: 1 addition & 1 deletion field/errors.go → field/birthdate/errors.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package field
package birthdate

import (
"errors"
Expand Down
12 changes: 11 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,14 @@ module github.com/ralvarezdev/go-validator

go 1.23.4

require github.com/ralvarezdev/go-flags v0.2.0 // indirect
require (
github.com/ralvarezdev/go-flags v0.2.0
google.golang.org/grpc v1.69.2
google.golang.org/protobuf v1.36.0
)

require (
github.com/ralvarezdev/go-logger v0.1.0 // indirect
golang.org/x/sys v0.26.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20241015192408-796eee8c2d53 // indirect
)
21 changes: 19 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
github.com/ralvarezdev/go-flags v0.1.0 h1:WG6eMbS2yL+UYtI3X8R/HbisrfEgzLiMKMWqXanwyys=
github.com/ralvarezdev/go-flags v0.1.0/go.mod h1:pYw9H7NJ07Y5asZDC/EI5bpBLR0kdL2ISsh6X5ws+3s=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/ralvarezdev/go-flags v0.2.0 h1:NdMuJgCv2f9RysYOMHQQM1BT9HGv6PPNDRYPT5T+d2c=
github.com/ralvarezdev/go-flags v0.2.0/go.mod h1:pYw9H7NJ07Y5asZDC/EI5bpBLR0kdL2ISsh6X5ws+3s=
github.com/ralvarezdev/go-logger v0.1.0 h1:i2AI1nlxU6Hizvk75Vc8wtFydiVrqIeeRbJwiuO/69A=
github.com/ralvarezdev/go-logger v0.1.0/go.mod h1:v5OvFrkS+wsYNTCVegXWiRhBtcYrQJr4LDMDntvpAos=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241015192408-796eee8c2d53 h1:X58yt85/IXCx0Y3ZwN6sEIKZzQtDEYaBWrDvErdXrRE=
google.golang.org/genproto/googleapis/rpc v0.0.0-20241015192408-796eee8c2d53/go.mod h1:GX3210XPVPUjJbTUbvwI8f2IpZDMZuPJWDzDuebbviI=
google.golang.org/grpc v1.69.2 h1:U3S9QEtbXC0bYNvRtcoklF3xGtLViumSYxWykJS+7AU=
google.golang.org/grpc v1.69.2/go.mod h1:vyjdE6jLBI76dgpDojsFGNaHlxdjXN9ghpnd2o7JGZ4=
google.golang.org/protobuf v1.36.0 h1:mjIs9gYtt56AzC4ZaffQuh88TZurBGhIJMBZGSxNerQ=
google.golang.org/protobuf v1.36.0/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
10 changes: 10 additions & 0 deletions service/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package service

import (
"errors"
)

var (
NilValidatorError = errors.New("validator cannot be nil")
NilMessageError = errors.New("message cannot be nil")
)
101 changes: 101 additions & 0 deletions service/validator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package service

import (
goflagmode "github.com/ralvarezdev/go-flags/mode"
govalidatorbirthdate "github.com/ralvarezdev/go-validator/field/birthdate"
govalidatormail "github.com/ralvarezdev/go-validator/field/mail"
govalidatormapper "github.com/ralvarezdev/go-validator/structs/mapper"
govalidatorvalidations "github.com/ralvarezdev/go-validator/structs/validations"
"google.golang.org/protobuf/types/known/timestamppb"
"time"
)

type (
// Validator interface
Validator interface {
ModeFlag() *goflagmode.Flag
ValidateEmail(
emailField string,
email string,
mapperValidations *govalidatorvalidations.MapperValidations,
)
ValidateBirthdate(
birthdateField string,
birthdate *timestamppb.Timestamp,
mapperValidations *govalidatorvalidations.MapperValidations,
)
ValidateNilFields(request interface{}, mapper *govalidatormapper.Mapper) (
*govalidatorvalidations.MapperValidations,
error,
)
CheckValidations(mapperValidations *govalidatorvalidations.MapperValidations) *string
}

// DefaultValidator struct
DefaultValidator struct {
mode *goflagmode.Flag
}
)

// NewDefaultValidator creates a new default validator
func NewDefaultValidator(mode *goflagmode.Flag) *DefaultValidator {
return &DefaultValidator{
mode: mode,
}
}

// ModeFlag returns the mode flag
func (d *DefaultValidator) ModeFlag() *goflagmode.Flag {
return d.mode
}

// ValidateEmail validates the email address field
func (d *DefaultValidator) ValidateEmail(
emailField string,
email string,
mapperValidations *govalidatorvalidations.MapperValidations,
) {
if _, err := govalidatormail.ValidMailAddress(email); err != nil {
mapperValidations.AddFailedFieldValidationError(
emailField,
govalidatormail.InvalidMailAddressError,
)
}
}

// ValidateBirthdate validates the birthdate field
func (d *DefaultValidator) ValidateBirthdate(
birthdateField string,
birthdate *timestamppb.Timestamp,
mapperValidations *govalidatorvalidations.MapperValidations,
) {
if birthdate == nil || birthdate.AsTime().After(time.Now()) {
mapperValidations.AddFailedFieldValidationError(
birthdateField,
govalidatorbirthdate.InvalidBirthdateError,
)
}
}

// ValidateNilFields validates the nil fields
func (d *DefaultValidator) ValidateNilFields(
request interface{},
mapper *govalidatormapper.Mapper,
) (*govalidatorvalidations.MapperValidations, error) {
return govalidatorvalidations.ValidateMapperNilFields(
request,
mapper,
d.mode,
)
}

// CheckValidations checks the validations and returns a pointer to the error message
func (d *DefaultValidator) CheckValidations(
mapperValidations *govalidatorvalidations.MapperValidations,
) *string {
// Get the error message from the validations if there are any
if mapperValidations.HasFailed() {
return mapperValidations.StringPtr()
}
return nil
}
4 changes: 2 additions & 2 deletions structs/validations/struct_fields_validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ func (s *MapperValidations) FailedValidationsMessage(level int) *string {
return &messageString
}

// String returns a formatted error message. If there are no failed validations, it returns nil
func (s *MapperValidations) String() *string {
// StringPtr returns a pointer to the failed validations message
func (s *MapperValidations) StringPtr() *string {
// Return the failed validations message
message := s.FailedValidationsMessage(0)

Expand Down

0 comments on commit 7d0fb6b

Please sign in to comment.