Skip to content

Commit

Permalink
Fix format checking failed when the input type is unexpected (#283)
Browse files Browse the repository at this point in the history
* Fix ignoring format test cases

* Import official format test cases

* Fix format checks for unexpected input type
  • Loading branch information
kiootic authored and johandorland committed Jan 18, 2020
1 parent 001aa27 commit b537c05
Show file tree
Hide file tree
Showing 5 changed files with 1,184 additions and 15 deletions.
30 changes: 15 additions & 15 deletions format_checkers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
type (
// FormatChecker is the interface all formatters added to FormatCheckerChain must implement
FormatChecker interface {
// IsFormat checks if input has the correct format and type
// IsFormat checks if input has the correct format
IsFormat(input interface{}) bool
}

Expand Down Expand Up @@ -191,7 +191,7 @@ func (c *FormatCheckerChain) IsFormat(name string, input interface{}) bool {
func (f EmailFormatChecker) IsFormat(input interface{}) bool {
asString, ok := input.(string)
if !ok {
return false
return true
}

_, err := mail.ParseAddress(asString)
Expand All @@ -202,7 +202,7 @@ func (f EmailFormatChecker) IsFormat(input interface{}) bool {
func (f IPV4FormatChecker) IsFormat(input interface{}) bool {
asString, ok := input.(string)
if !ok {
return false
return true
}

// Credit: https://github.com/asaskevich/govalidator
Expand All @@ -214,7 +214,7 @@ func (f IPV4FormatChecker) IsFormat(input interface{}) bool {
func (f IPV6FormatChecker) IsFormat(input interface{}) bool {
asString, ok := input.(string)
if !ok {
return false
return true
}

// Credit: https://github.com/asaskevich/govalidator
Expand All @@ -226,7 +226,7 @@ func (f IPV6FormatChecker) IsFormat(input interface{}) bool {
func (f DateTimeFormatChecker) IsFormat(input interface{}) bool {
asString, ok := input.(string)
if !ok {
return false
return true
}

formats := []string{
Expand All @@ -250,7 +250,7 @@ func (f DateTimeFormatChecker) IsFormat(input interface{}) bool {
func (f DateFormatChecker) IsFormat(input interface{}) bool {
asString, ok := input.(string)
if !ok {
return false
return true
}
_, err := time.Parse("2006-01-02", asString)
return err == nil
Expand All @@ -260,7 +260,7 @@ func (f DateFormatChecker) IsFormat(input interface{}) bool {
func (f TimeFormatChecker) IsFormat(input interface{}) bool {
asString, ok := input.(string)
if !ok {
return false
return true
}

if _, err := time.Parse("15:04:05Z07:00", asString); err == nil {
Expand All @@ -275,7 +275,7 @@ func (f TimeFormatChecker) IsFormat(input interface{}) bool {
func (f URIFormatChecker) IsFormat(input interface{}) bool {
asString, ok := input.(string)
if !ok {
return false
return true
}

u, err := url.Parse(asString)
Expand All @@ -291,7 +291,7 @@ func (f URIFormatChecker) IsFormat(input interface{}) bool {
func (f URIReferenceFormatChecker) IsFormat(input interface{}) bool {
asString, ok := input.(string)
if !ok {
return false
return true
}

_, err := url.Parse(asString)
Expand All @@ -302,7 +302,7 @@ func (f URIReferenceFormatChecker) IsFormat(input interface{}) bool {
func (f URITemplateFormatChecker) IsFormat(input interface{}) bool {
asString, ok := input.(string)
if !ok {
return false
return true
}

u, err := url.Parse(asString)
Expand All @@ -317,7 +317,7 @@ func (f URITemplateFormatChecker) IsFormat(input interface{}) bool {
func (f HostnameFormatChecker) IsFormat(input interface{}) bool {
asString, ok := input.(string)
if !ok {
return false
return true
}

return rxHostname.MatchString(asString) && len(asString) < 256
Expand All @@ -327,7 +327,7 @@ func (f HostnameFormatChecker) IsFormat(input interface{}) bool {
func (f UUIDFormatChecker) IsFormat(input interface{}) bool {
asString, ok := input.(string)
if !ok {
return false
return true
}

return rxUUID.MatchString(asString)
Expand All @@ -337,7 +337,7 @@ func (f UUIDFormatChecker) IsFormat(input interface{}) bool {
func (f RegexFormatChecker) IsFormat(input interface{}) bool {
asString, ok := input.(string)
if !ok {
return false
return true
}

if asString == "" {
Expand All @@ -351,7 +351,7 @@ func (f RegexFormatChecker) IsFormat(input interface{}) bool {
func (f JSONPointerFormatChecker) IsFormat(input interface{}) bool {
asString, ok := input.(string)
if !ok {
return false
return true
}

return rxJSONPointer.MatchString(asString)
Expand All @@ -361,7 +361,7 @@ func (f JSONPointerFormatChecker) IsFormat(input interface{}) bool {
func (f RelativeJSONPointerFormatChecker) IsFormat(input interface{}) bool {
asString, ok := input.(string)
if !ok {
return false
return true
}

return rxRelJSONPointer.MatchString(asString)
Expand Down
11 changes: 11 additions & 0 deletions jsonschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ func TestFormats(t *testing.T) {

for _, dir := range dirs {
if testDirectories.MatchString(dir.Name()) {
formatJSONFile := filepath.Join(wd, dir.Name(), "optional", "format.json")
if _, err = os.Stat(formatJSONFile); err == nil {
err = executeTests(t, formatJSONFile)
} else {
err = nil
}

if err != nil {
t.Errorf("Error (%s)\n", err.Error())
}

formatsDirectory := filepath.Join(wd, dir.Name(), "optional", "format")
err = filepath.Walk(formatsDirectory, func(path string, fileInfo os.FileInfo, err error) error {
if fileInfo == nil || !strings.HasSuffix(fileInfo.Name(), ".json") {
Expand Down
218 changes: 218 additions & 0 deletions testdata/draft4/format.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
[
{
"description": "validation of e-mail addresses",
"schema": {"format": "email"},
"tests": [
{
"description": "ignores integers",
"data": 12,
"valid": true
},
{
"description": "ignores floats",
"data": 13.7,
"valid": true
},
{
"description": "ignores objects",
"data": {},
"valid": true
},
{
"description": "ignores arrays",
"data": [],
"valid": true
},
{
"description": "ignores booleans",
"data": false,
"valid": true
},
{
"description": "ignores null",
"data": null,
"valid": true
}
]
},
{
"description": "validation of IP addresses",
"schema": {"format": "ipv4"},
"tests": [
{
"description": "ignores integers",
"data": 12,
"valid": true
},
{
"description": "ignores floats",
"data": 13.7,
"valid": true
},
{
"description": "ignores objects",
"data": {},
"valid": true
},
{
"description": "ignores arrays",
"data": [],
"valid": true
},
{
"description": "ignores booleans",
"data": false,
"valid": true
},
{
"description": "ignores null",
"data": null,
"valid": true
}
]
},
{
"description": "validation of IPv6 addresses",
"schema": {"format": "ipv6"},
"tests": [
{
"description": "ignores integers",
"data": 12,
"valid": true
},
{
"description": "ignores floats",
"data": 13.7,
"valid": true
},
{
"description": "ignores objects",
"data": {},
"valid": true
},
{
"description": "ignores arrays",
"data": [],
"valid": true
},
{
"description": "ignores booleans",
"data": false,
"valid": true
},
{
"description": "ignores null",
"data": null,
"valid": true
}
]
},
{
"description": "validation of hostnames",
"schema": {"format": "hostname"},
"tests": [
{
"description": "ignores integers",
"data": 12,
"valid": true
},
{
"description": "ignores floats",
"data": 13.7,
"valid": true
},
{
"description": "ignores objects",
"data": {},
"valid": true
},
{
"description": "ignores arrays",
"data": [],
"valid": true
},
{
"description": "ignores booleans",
"data": false,
"valid": true
},
{
"description": "ignores null",
"data": null,
"valid": true
}
]
},
{
"description": "validation of date-time strings",
"schema": {"format": "date-time"},
"tests": [
{
"description": "ignores integers",
"data": 12,
"valid": true
},
{
"description": "ignores floats",
"data": 13.7,
"valid": true
},
{
"description": "ignores objects",
"data": {},
"valid": true
},
{
"description": "ignores arrays",
"data": [],
"valid": true
},
{
"description": "ignores booleans",
"data": false,
"valid": true
},
{
"description": "ignores null",
"data": null,
"valid": true
}
]
},
{
"description": "validation of URIs",
"schema": {"format": "uri"},
"tests": [
{
"description": "ignores integers",
"data": 12,
"valid": true
},
{
"description": "ignores floats",
"data": 13.7,
"valid": true
},
{
"description": "ignores objects",
"data": {},
"valid": true
},
{
"description": "ignores arrays",
"data": [],
"valid": true
},
{
"description": "ignores booleans",
"data": false,
"valid": true
},
{
"description": "ignores null",
"data": null,
"valid": true
}
]
}
]
Loading

0 comments on commit b537c05

Please sign in to comment.