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 3 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
30 changes: 24 additions & 6 deletions openapi3/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,14 +930,25 @@ 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() + "`)'",
f := SchemaStringFormats[v]
switch f.Type {
riccardomanfrin marked this conversation as resolved.
Show resolved Hide resolved
case FormatTypeRe:
{
re := f.Regexp
if re != nil {
cp = &compiledPattern{
Regexp: re,
ErrReason: "JSON string doesn't match the format '" + v + " (regular expression `" + re.String() + "`)'",
}
schema.compiledPattern = cp
}
}
case FormatTypeCallback:
{
return f.Callback(value)
}
schema.compiledPattern = cp
}

}
}
if cp != nil {
Expand All @@ -953,6 +964,13 @@ func (schema *Schema) visitJSONString(value string, fast bool) (err error) {
Reason: cp.ErrReason,
}
}
} else if len(schema.Format) > 0 && cp == nil {
riccardomanfrin marked this conversation as resolved.
Show resolved Hide resolved
return &SchemaError{
Value: value,
Schema: schema,
SchemaField: "format",
Reason: "Unsupported Format",
}
}
return
}
Expand Down
83 changes: 81 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,89 @@ 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)
// FormatType is the type of format validation used
fenollp marked this conversation as resolved.
Show resolved Hide resolved
type FormatType string
fenollp marked this conversation as resolved.
Show resolved Hide resolved
fenollp marked this conversation as resolved.
Show resolved Hide resolved

// FormatTypeRe is regexp based validation
fenollp marked this conversation as resolved.
Show resolved Hide resolved
const FormatTypeRe = "re"
fenollp marked this conversation as resolved.
Show resolved Hide resolved
fenollp marked this conversation as resolved.
Show resolved Hide resolved

fenollp marked this conversation as resolved.
Show resolved Hide resolved
// FormatTypeCallback is callback based validation
fenollp marked this conversation as resolved.
Show resolved Hide resolved
const FormatTypeCallback = "callback"
fenollp marked this conversation as resolved.
Show resolved Hide resolved
fenollp marked this conversation as resolved.
Show resolved Hide resolved

fenollp marked this conversation as resolved.
Show resolved Hide resolved
fenollp marked this conversation as resolved.
Show resolved Hide resolved
type FormatCallback func(Val string) error

//Format is the format type context fo validate the format
fenollp marked this conversation as resolved.
Show resolved Hide resolved
fenollp marked this conversation as resolved.
Show resolved Hide resolved
type Format struct {
Type FormatType
Regexp *regexp.Regexp
riccardomanfrin marked this conversation as resolved.
Show resolved Hide resolved
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
f := Format{
riccardomanfrin marked this conversation as resolved.
Show resolved Hide resolved
Type: FormatTypeRe,
Regexp: re,
Callback: nil}
SchemaStringFormats[name] = f
}

// DefineStringCallbackFormat define callback based type callback validation
riccardomanfrin marked this conversation as resolved.
Show resolved Hide resolved
func DefineStringCallbackFormat(name string, callback FormatCallback) {
riccardomanfrin marked this conversation as resolved.
Show resolved Hide resolved

f := Format{
riccardomanfrin marked this conversation as resolved.
Show resolved Hide resolved
Type: FormatTypeCallback,
Regexp: nil,
Callback: callback}
SchemaStringFormats[name] = f
}

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 +110,8 @@ 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})?$`)

DefineStringCallbackFormat("ipv4", validateIPv4)
riccardomanfrin marked this conversation as resolved.
Show resolved Hide resolved
DefineStringCallbackFormat("ipv6", validateIPv6)
riccardomanfrin marked this conversation as resolved.
Show resolved Hide resolved
riccardomanfrin marked this conversation as resolved.
Show resolved Hide resolved

}