diff --git a/go.mod b/go.mod index a5e066a..9b992c0 100644 --- a/go.mod +++ b/go.mod @@ -5,13 +5,13 @@ go 1.21 require ( github.com/gofrs/uuid v4.4.0+incompatible github.com/pkg/errors v0.9.1 - github.com/stretchr/testify v1.8.4 - golang.org/x/net v0.21.0 + github.com/stretchr/testify v1.9.0 + golang.org/x/net v0.28.0 ) require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/text v0.14.0 // indirect + golang.org/x/text v0.17.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 518fbdb..320fc72 100644 --- a/go.sum +++ b/go.sum @@ -6,12 +6,12 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= -golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= -golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= +golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= +golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc= +golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/mac.go b/mac.go new file mode 100644 index 0000000..31c5b5d --- /dev/null +++ b/mac.go @@ -0,0 +1,83 @@ +package validator + +import ( + "context" + "net" +) + +type MAC struct { + message string + whenFunc WhenFunc + skipEmpty bool + skipError bool +} + +func NewMAC() *MAC { + return &MAC{ + message: "Must be a valid MAC address.", + } +} + +func (m *MAC) WithMessage(v string) *MAC { + rc := *m + rc.message = v + + return &rc +} + +func (m *MAC) When(v WhenFunc) *MAC { + rc := *m + rc.whenFunc = v + + return &rc +} + +func (m *MAC) when() WhenFunc { + return m.whenFunc +} + +func (m *MAC) setWhen(v WhenFunc) { + m.whenFunc = v +} + +func (m *MAC) SkipOnEmpty() *MAC { + rc := *m + rc.skipEmpty = true + + return &rc +} + +func (m *MAC) skipOnEmpty() bool { + return m.skipEmpty +} + +func (m *MAC) setSkipOnEmpty(v bool) { + m.skipEmpty = v +} + +func (m *MAC) SkipOnError() *MAC { + rs := *m + rs.skipError = true + + return &rs +} + +func (m *MAC) shouldSkipOnError() bool { + return m.skipError +} +func (m *MAC) setSkipOnError(v bool) { + m.skipError = v +} + +func (m *MAC) ValidateValue(_ context.Context, value any) error { + v, ok := toString(value) + if !ok { + return NewResult().WithError(NewValidationError(m.message)) + } + + if _, err := net.ParseMAC(v); err != nil { + return NewResult().WithError(NewValidationError(m.message)) + } + + return nil +} diff --git a/numeric.go b/numeric.go new file mode 100644 index 0000000..d6e8da8 --- /dev/null +++ b/numeric.go @@ -0,0 +1,139 @@ +package validator + +import ( + "context" +) + +type Numeric struct { + min float64 + max float64 + notNumericMessage string + tooBigMessage string + tooSmallMessage string + whenFunc WhenFunc + skipEmpty bool + skipError bool +} + +func NewNumeric(min, max float64) *Numeric { + return &Numeric{ + min: min, + max: max, + notNumericMessage: "Value must be a numeric.", + tooBigMessage: "Value must be no greater than {max}.", + tooSmallMessage: "Value must be no less than {min}.", + } +} + +func (n *Numeric) WithTooBigMessage(message string) *Numeric { + rc := *n + rc.tooBigMessage = message + + return &rc +} + +func (n *Numeric) WithTooSmallMessage(message string) *Numeric { + rc := *n + rc.tooSmallMessage = message + + return &rc +} + +func (n *Numeric) WithNotNumericMessage(message string) *Numeric { + rc := *n + rc.notNumericMessage = message + + return &rc +} + +func (n *Numeric) When(v WhenFunc) *Numeric { + rc := *n + rc.whenFunc = v + + return &rc +} + +func (n *Numeric) when() WhenFunc { + return n.whenFunc +} + +func (n *Numeric) setWhen(v WhenFunc) { + n.whenFunc = v +} + +func (n *Numeric) SkipOnEmpty() *Numeric { + rc := *n + rc.skipEmpty = true + + return &rc +} + +func (n *Numeric) skipOnEmpty() bool { + return n.skipEmpty +} + +func (n *Numeric) setSkipOnEmpty(v bool) { + n.skipEmpty = v +} + +func (n *Numeric) SkipOnError() *Numeric { + rs := *n + rs.skipError = true + + return &rs +} + +func (n *Numeric) shouldSkipOnError() bool { + return n.skipError +} +func (n *Numeric) setSkipOnError(v bool) { + n.skipError = v +} + +func (n *Numeric) ValidateValue(_ context.Context, value any) error { + if value == nil { + return NewResult().WithError(NewValidationError(n.notNumericMessage)) + } + + var i float64 + + switch v := value.(type) { + case *float32: + i = float64(*v) + case *float64: + i = *v + case float32: + i = float64(v) + case float64: + i = v + default: + return NewResult().WithError(NewValidationError(n.notNumericMessage)) + } + + result := NewResult() + + if i < n.min { + result = result.WithError( + NewValidationError(n.tooSmallMessage). + WithParams(map[string]any{ + "min": n.min, + "max": n.max, + }), + ) + } + + if i > n.max { + result = result.WithError( + NewValidationError(n.tooBigMessage). + WithParams(map[string]any{ + "min": n.min, + "max": n.max, + }), + ) + } + + if !result.IsValid() { + return result + } + return nil +}