Added option to check format also on numbers
In some cases i would like to check if a entered number is valid using
my own code, for example, if i want to check if a given property points
to valid id on a database.
This patches breaks backward compatibility because so far the format
property was only used on string values so the parameter to IsFormat()
was also a string. We have to convert it to be an interface{} so we
are able to send any type of input to the function.
diff --git a/format_checkers.go b/format_checkers.go
index 94bd095..922d0e2 100644
--- a/format_checkers.go
+++ b/format_checkers.go
@@ -12,7 +12,7 @@
type (
// FormatChecker is the interface all formatters added to FormatCheckerChain must implement
FormatChecker interface {
- IsFormat(input string) bool
+ IsFormat(input interface{}) bool
}
// FormatCheckerChain holds the formatters
@@ -134,23 +134,47 @@
return f.IsFormat(inputString)
}
-func (f EmailFormatChecker) IsFormat(input string) bool {
- return rxEmail.MatchString(input)
+func (f EmailFormatChecker) IsFormat(input interface{}) bool {
+
+ asString, ok := input.(string)
+ if ok == false {
+ return false
+ }
+
+ return rxEmail.MatchString(asString)
}
// Credit: https://github.com/asaskevich/govalidator
-func (f IPV4FormatChecker) IsFormat(input string) bool {
- ip := net.ParseIP(input)
- return ip != nil && strings.Contains(input, ".")
+func (f IPV4FormatChecker) IsFormat(input interface{}) bool {
+
+ asString, ok := input.(string)
+ if ok == false {
+ return false
+ }
+
+ ip := net.ParseIP(asString)
+ return ip != nil && strings.Contains(asString, ".")
}
// Credit: https://github.com/asaskevich/govalidator
-func (f IPV6FormatChecker) IsFormat(input string) bool {
- ip := net.ParseIP(input)
- return ip != nil && strings.Contains(input, ":")
+func (f IPV6FormatChecker) IsFormat(input interface{}) bool {
+
+ asString, ok := input.(string)
+ if ok == false {
+ return false
+ }
+
+ ip := net.ParseIP(asString)
+ return ip != nil && strings.Contains(asString, ":")
}
-func (f DateTimeFormatChecker) IsFormat(input string) bool {
+func (f DateTimeFormatChecker) IsFormat(input interface{}) bool {
+
+ asString, ok := input.(string)
+ if ok == false {
+ return false
+ }
+
formats := []string{
"15:04:05",
"15:04:05Z07:00",
@@ -160,7 +184,7 @@
}
for _, format := range formats {
- if _, err := time.Parse(format, input); err == nil {
+ if _, err := time.Parse(format, asString); err == nil {
return true
}
}
@@ -168,8 +192,14 @@
return false
}
-func (f URIFormatChecker) IsFormat(input string) bool {
- u, err := url.Parse(input)
+func (f URIFormatChecker) IsFormat(input interface{}) bool {
+
+ asString, ok := input.(string)
+ if ok == false {
+ return false
+ }
+
+ u, err := url.Parse(asString)
if err != nil || u.Scheme == "" {
return false
}
@@ -177,25 +207,49 @@
return true
}
-func (f URIReferenceFormatChecker) IsFormat(input string) bool {
- _, err := url.Parse(input)
+func (f URIReferenceFormatChecker) IsFormat(input interface{}) bool {
+
+ asString, ok := input.(string)
+ if ok == false {
+ return false
+ }
+
+ _, err := url.Parse(asString)
return err == nil
}
-func (f HostnameFormatChecker) IsFormat(input string) bool {
- return rxHostname.MatchString(input) && len(input) < 256
+func (f HostnameFormatChecker) IsFormat(input interface{}) bool {
+
+ asString, ok := input.(string)
+ if ok == false {
+ return false
+ }
+
+ return rxHostname.MatchString(asString) && len(asString) < 256
}
-func (f UUIDFormatChecker) IsFormat(input string) bool {
- return rxUUID.MatchString(input)
+func (f UUIDFormatChecker) IsFormat(input interface{}) bool {
+
+ asString, ok := input.(string)
+ if ok == false {
+ return false
+ }
+
+ return rxUUID.MatchString(asString)
}
// IsFormat implements FormatChecker interface.
-func (f RegexFormatChecker) IsFormat(input string) bool {
- if input == "" {
+func (f RegexFormatChecker) IsFormat(input interface{}) bool {
+
+ asString, ok := input.(string)
+ if ok == false {
+ return false
+ }
+
+ if asString == "" {
return true
}
- _, err := regexp.Compile(input)
+ _, err := regexp.Compile(asString)
if err != nil {
return false
}
diff --git a/validation.go b/validation.go
index 6140bd8..9afea25 100644
--- a/validation.go
+++ b/validation.go
@@ -828,5 +828,17 @@
}
}
+ // format
+ if currentSubSchema.format != "" {
+ if !FormatCheckers.IsFormat(currentSubSchema.format, float64Value) {
+ result.addError(
+ new(DoesNotMatchFormatError),
+ context,
+ value,
+ ErrorDetails{"format": currentSubSchema.format},
+ )
+ }
+ }
+
result.incrementScore()
}