Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ipv4 and ipv6 formats support #258

Merged
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e9055d1
Adds ipv4 and ipv6 formats support
riccardomanfrin Oct 12, 2020
6a8d346
Fix tests
riccardomanfrin Oct 13, 2020
43aa7a2
Comply to comment
riccardomanfrin Oct 14, 2020
12eed8d
Substitutes Type check with nil check on Regexp value
riccardomanfrin Oct 27, 2020
117a159
Makes regexp and callback private
riccardomanfrin Oct 27, 2020
6255270
Keep ipv4 and ipv6 format validation optional
riccardomanfrin Oct 27, 2020
cdf5de5
Update openapi3/schema_formats.go
riccardomanfrin Oct 27, 2020
c59bba2
Update openapi3/schema_formats.go
riccardomanfrin Oct 27, 2020
b98a889
Update openapi3/schema_formats.go
riccardomanfrin Oct 27, 2020
1eb4c54
Update openapi3/schema_formats.go
riccardomanfrin Oct 27, 2020
d44ccf3
Update openapi3/schema_formats.go
riccardomanfrin Oct 27, 2020
624e833
Update openapi3/schema_formats.go
riccardomanfrin Oct 27, 2020
718b995
Keep this for a different PR
riccardomanfrin Nov 11, 2020
5c84528
Adds check on f existence and default currupted error case
riccardomanfrin Nov 11, 2020
aa9a033
Fixups
riccardomanfrin Nov 11, 2020
774bc63
Removes redundant check
riccardomanfrin Nov 11, 2020
9c0b7ae
typo
riccardomanfrin Nov 11, 2020
af108f2
Fix
riccardomanfrin Nov 11, 2020
48e4b0a
Update openapi3/schema_formats.go
fenollp Nov 12, 2020
1736357
Apply suggestions from code review
fenollp Nov 12, 2020
c58b987
Apply suggestions from code review
fenollp Nov 12, 2020
d9145d4
Update openapi3/schema_formats.go
fenollp Nov 12, 2020
bcf4aae
Update openapi3/schema_formats.go
fenollp Nov 12, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,13 +930,18 @@ func (schema *Schema) visitJSONString(value string, fast bool) (err error) {
schema.compiledPattern = cp
} else if v := schema.Format; len(v) > 0 {
// No pattern, but does have a format
re := SchemaStringFormats[v]
if re != nil {
cp = &compiledPattern{
Regexp: re,
ErrReason: "JSON string doesn't match the format '" + v + " (regular expression `" + re.String() + "`)'",
if f, ok := SchemaStringFormats[v]; ok {
if f.regexp != nil && f.callback == nil {
schema.compiledPattern = &compiledPattern{
Regexp: f.regexp,
ErrReason: "JSON string doesn't match the format '" + v + " (regular expression `" + f.regexp.String() + "`)'",
}

} else if f.regexp == nil && f.callback != nil {
return f.callback(value)
} else {
return fmt.Errorf("corrupted entry %q in SchemaStringFormats", v)
}
schema.compiledPattern = cp
}
}
}
Expand Down
72 changes: 70 additions & 2 deletions openapi3/schema_formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package openapi3

import (
"fmt"
"net"
"regexp"
)

Expand All @@ -10,15 +11,71 @@ const (
FormatOfStringForUUIDOfRFC4122 = `^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$`
)

fenollp marked this conversation as resolved.
Show resolved Hide resolved
var SchemaStringFormats = make(map[string]*regexp.Regexp, 8)

//FormatCallback custom check on exotic formats
type FormatCallback func(Val string) error

type Format struct {
regexp *regexp.Regexp
callback FormatCallback
}

//SchemaStringFormats allows for validating strings format
var SchemaStringFormats = make(map[string]Format, 8)

//DefineStringFormat Defines a new regexp pattern for a given format
func DefineStringFormat(name string, pattern string) {
re, err := regexp.Compile(pattern)
if err != nil {
err := fmt.Errorf("Format '%v' has invalid pattern '%v': %v", name, pattern, err)
panic(err)
}
SchemaStringFormats[name] = re
SchemaStringFormats[name] = Format{regexp: re}
}

// DefineStringFormatCallback adds a validation function for a specific schema format entry
func DefineStringFormatCallback(name string, callback FormatCallback) {
SchemaStringFormats[name] = Format{callback: callback}
}

func validateIP(ip string) (*net.IP, error) {
parsed := net.ParseIP(ip)
if parsed == nil {
return nil, &SchemaError{
Value: ip,
Reason: "Not an IP address",
}
}
return &parsed, nil
}

func validateIPv4(ip string) error {
parsed, err := validateIP(ip)
if err != nil {
return err
}

if parsed.To4() == nil {
return &SchemaError{
Value: ip,
Reason: "Not an IPv4 address (it's IPv6)",
}
}
return nil
}
func validateIPv6(ip string) error {
parsed, err := validateIP(ip)
if err != nil {
return err
}

if parsed.To4() != nil {
return &SchemaError{
Value: ip,
Reason: "Not an IPv6 address (it's IPv4)",
}
}
return nil
}

func init() {
Expand All @@ -35,4 +92,15 @@ func init() {

// date-time
DefineStringFormat("date-time", `^[0-9]{4}-(0[0-9]|10|11|12)-([0-2][0-9]|30|31)T[0-9]{2}:[0-9]{2}:[0-9]{2}(.[0-9]+)?(Z|(\+|-)[0-9]{2}:[0-9]{2})?$`)

}

// DefineIPv4Format opts in ipv4 format validation on top of OAS 3 spec
func DefineIPv4Format() {
DefineStringFormatCallback("ipv4", validateIPv4)
}

// DefineIPv6Format opts in ipv6 format validation on top of OAS 3 spec
func DefineIPv6Format() {
DefineStringFormatCallback("ipv6", validateIPv6)
}