-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add mac and numeric validators (#23)
Co-authored-by: Кирилл Маликов <[email protected]>
- Loading branch information
Showing
4 changed files
with
231 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |