2017-04-17 18:08:24 -04:00
|
|
|
package gojsonschema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2019-04-12 20:56:09 -04:00
|
|
|
"net/mail"
|
2017-04-17 18:08:24 -04:00
|
|
|
"net/url"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
2019-04-12 20:56:09 -04:00
|
|
|
"sync"
|
2017-04-17 18:08:24 -04:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
// FormatChecker is the interface all formatters added to FormatCheckerChain must implement
|
|
|
|
FormatChecker interface {
|
2019-04-12 20:56:09 -04:00
|
|
|
IsFormat(input interface{}) bool
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// FormatCheckerChain holds the formatters
|
|
|
|
FormatCheckerChain struct {
|
|
|
|
formatters map[string]FormatChecker
|
|
|
|
}
|
|
|
|
|
|
|
|
// EmailFormatter verifies email address formats
|
|
|
|
EmailFormatChecker struct{}
|
|
|
|
|
|
|
|
// IPV4FormatChecker verifies IP addresses in the ipv4 format
|
|
|
|
IPV4FormatChecker struct{}
|
|
|
|
|
|
|
|
// IPV6FormatChecker verifies IP addresses in the ipv6 format
|
|
|
|
IPV6FormatChecker struct{}
|
|
|
|
|
|
|
|
// DateTimeFormatChecker verifies date/time formats per RFC3339 5.6
|
|
|
|
//
|
|
|
|
// Valid formats:
|
|
|
|
// Partial Time: HH:MM:SS
|
|
|
|
// Full Date: YYYY-MM-DD
|
|
|
|
// Full Time: HH:MM:SSZ-07:00
|
|
|
|
// Date Time: YYYY-MM-DDTHH:MM:SSZ-0700
|
|
|
|
//
|
|
|
|
// Where
|
|
|
|
// YYYY = 4DIGIT year
|
|
|
|
// MM = 2DIGIT month ; 01-12
|
|
|
|
// DD = 2DIGIT day-month ; 01-28, 01-29, 01-30, 01-31 based on month/year
|
|
|
|
// HH = 2DIGIT hour ; 00-23
|
|
|
|
// MM = 2DIGIT ; 00-59
|
|
|
|
// SS = 2DIGIT ; 00-58, 00-60 based on leap second rules
|
|
|
|
// T = Literal
|
|
|
|
// Z = Literal
|
|
|
|
//
|
|
|
|
// Note: Nanoseconds are also suported in all formats
|
|
|
|
//
|
|
|
|
// http://tools.ietf.org/html/rfc3339#section-5.6
|
|
|
|
DateTimeFormatChecker struct{}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
DateFormatChecker struct{}
|
|
|
|
|
|
|
|
TimeFormatChecker struct{}
|
|
|
|
|
|
|
|
// URIFormatChecker validates a URI with a valid Scheme per RFC3986
|
2017-04-17 18:08:24 -04:00
|
|
|
URIFormatChecker struct{}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
// URIReferenceFormatChecker validates a URI or relative-reference per RFC3986
|
|
|
|
URIReferenceFormatChecker struct{}
|
|
|
|
|
|
|
|
// URITemplateFormatChecker validates a URI template per RFC6570
|
|
|
|
URITemplateFormatChecker struct{}
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
// HostnameFormatChecker validates a hostname is in the correct format
|
|
|
|
HostnameFormatChecker struct{}
|
|
|
|
|
|
|
|
// UUIDFormatChecker validates a UUID is in the correct format
|
|
|
|
UUIDFormatChecker struct{}
|
2019-04-12 20:56:09 -04:00
|
|
|
|
|
|
|
// RegexFormatChecker validates a regex is in the correct format
|
|
|
|
RegexFormatChecker struct{}
|
|
|
|
|
|
|
|
// JSONPointerFormatChecker validates a JSON Pointer per RFC6901
|
|
|
|
JSONPointerFormatChecker struct{}
|
|
|
|
|
|
|
|
// RelativeJSONPointerFormatChecker validates a relative JSON Pointer is in the correct format
|
|
|
|
RelativeJSONPointerFormatChecker struct{}
|
2017-04-17 18:08:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Formatters holds the valid formatters, and is a public variable
|
|
|
|
// so library users can add custom formatters
|
|
|
|
FormatCheckers = FormatCheckerChain{
|
|
|
|
formatters: map[string]FormatChecker{
|
2019-04-12 20:56:09 -04:00
|
|
|
"date": DateFormatChecker{},
|
|
|
|
"time": TimeFormatChecker{},
|
|
|
|
"date-time": DateTimeFormatChecker{},
|
|
|
|
"hostname": HostnameFormatChecker{},
|
|
|
|
"email": EmailFormatChecker{},
|
|
|
|
"idn-email": EmailFormatChecker{},
|
|
|
|
"ipv4": IPV4FormatChecker{},
|
|
|
|
"ipv6": IPV6FormatChecker{},
|
|
|
|
"uri": URIFormatChecker{},
|
|
|
|
"uri-reference": URIReferenceFormatChecker{},
|
|
|
|
"iri": URIFormatChecker{},
|
|
|
|
"iri-reference": URIReferenceFormatChecker{},
|
|
|
|
"uri-template": URITemplateFormatChecker{},
|
|
|
|
"uuid": UUIDFormatChecker{},
|
|
|
|
"regex": RegexFormatChecker{},
|
|
|
|
"json-pointer": JSONPointerFormatChecker{},
|
|
|
|
"relative-json-pointer": RelativeJSONPointerFormatChecker{},
|
2017-04-17 18:08:24 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Regex credit: https://www.socketloop.com/tutorials/golang-validate-hostname
|
|
|
|
rxHostname = regexp.MustCompile(`^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])(\.([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]{0,61}[a-zA-Z0-9]))*$`)
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
// Use a regex to make sure curly brackets are balanced properly after validating it as a AURI
|
|
|
|
rxURITemplate = regexp.MustCompile("^([^{]*({[^}]*})?)*$")
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
rxUUID = regexp.MustCompile("^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$")
|
2019-04-12 20:56:09 -04:00
|
|
|
|
|
|
|
rxJSONPointer = regexp.MustCompile("^(?:/(?:[^~/]|~0|~1)*)*$")
|
|
|
|
|
|
|
|
rxRelJSONPointer = regexp.MustCompile("^(?:0|[1-9][0-9]*)(?:#|(?:/(?:[^~/]|~0|~1)*)*)$")
|
|
|
|
|
|
|
|
lock = new(sync.Mutex)
|
2017-04-17 18:08:24 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Add adds a FormatChecker to the FormatCheckerChain
|
|
|
|
// The name used will be the value used for the format key in your json schema
|
|
|
|
func (c *FormatCheckerChain) Add(name string, f FormatChecker) *FormatCheckerChain {
|
2019-04-12 20:56:09 -04:00
|
|
|
lock.Lock()
|
2017-04-17 18:08:24 -04:00
|
|
|
c.formatters[name] = f
|
2019-04-12 20:56:09 -04:00
|
|
|
lock.Unlock()
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove deletes a FormatChecker from the FormatCheckerChain (if it exists)
|
|
|
|
func (c *FormatCheckerChain) Remove(name string) *FormatCheckerChain {
|
2019-04-12 20:56:09 -04:00
|
|
|
lock.Lock()
|
2017-04-17 18:08:24 -04:00
|
|
|
delete(c.formatters, name)
|
2019-04-12 20:56:09 -04:00
|
|
|
lock.Unlock()
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Has checks to see if the FormatCheckerChain holds a FormatChecker with the given name
|
|
|
|
func (c *FormatCheckerChain) Has(name string) bool {
|
2019-04-12 20:56:09 -04:00
|
|
|
lock.Lock()
|
2017-04-17 18:08:24 -04:00
|
|
|
_, ok := c.formatters[name]
|
2019-04-12 20:56:09 -04:00
|
|
|
lock.Unlock()
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsFormat will check an input against a FormatChecker with the given name
|
|
|
|
// to see if it is the correct format
|
|
|
|
func (c *FormatCheckerChain) IsFormat(name string, input interface{}) bool {
|
|
|
|
f, ok := c.formatters[name]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
return f.IsFormat(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f EmailFormatChecker) IsFormat(input interface{}) bool {
|
|
|
|
|
|
|
|
asString, ok := input.(string)
|
|
|
|
if ok == false {
|
2017-04-17 18:08:24 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
_, err := mail.ParseAddress(asString)
|
2017-04-17 18:08:24 -04:00
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
return err == nil
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Credit: https://github.com/asaskevich/govalidator
|
2019-04-12 20:56:09 -04:00
|
|
|
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, ".")
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Credit: https://github.com/asaskevich/govalidator
|
2019-04-12 20:56:09 -04:00
|
|
|
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, ":")
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
func (f DateTimeFormatChecker) IsFormat(input interface{}) bool {
|
|
|
|
|
|
|
|
asString, ok := input.(string)
|
|
|
|
if ok == false {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
formats := []string{
|
|
|
|
"15:04:05",
|
|
|
|
"15:04:05Z07:00",
|
|
|
|
"2006-01-02",
|
|
|
|
time.RFC3339,
|
|
|
|
time.RFC3339Nano,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, format := range formats {
|
2019-04-12 20:56:09 -04:00
|
|
|
if _, err := time.Parse(format, asString); err == nil {
|
2017-04-17 18:08:24 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
func (f DateFormatChecker) IsFormat(input interface{}) bool {
|
|
|
|
asString, ok := input.(string)
|
|
|
|
if ok == false {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
_, err := time.Parse("2006-01-02", asString)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f TimeFormatChecker) IsFormat(input interface{}) bool {
|
|
|
|
asString, ok := input.(string)
|
|
|
|
if ok == false {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := time.Parse("15:04:05Z07:00", asString); err == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := time.Parse("15:04:05", asString)
|
|
|
|
return err == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f URIFormatChecker) IsFormat(input interface{}) bool {
|
|
|
|
|
|
|
|
asString, ok := input.(string)
|
|
|
|
if ok == false {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(asString)
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
if err != nil || u.Scheme == "" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
return !strings.Contains(asString, `\`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f URIReferenceFormatChecker) IsFormat(input interface{}) bool {
|
|
|
|
|
|
|
|
asString, ok := input.(string)
|
|
|
|
if ok == false {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := url.Parse(asString)
|
|
|
|
return err == nil && !strings.Contains(asString, `\`)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f URITemplateFormatChecker) IsFormat(input interface{}) bool {
|
|
|
|
asString, ok := input.(string)
|
|
|
|
if ok == false {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(asString)
|
|
|
|
if err != nil || strings.Contains(asString, `\`) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return rxURITemplate.MatchString(u.Path)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 interface{}) bool {
|
|
|
|
|
|
|
|
asString, ok := input.(string)
|
|
|
|
if ok == false {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return rxUUID.MatchString(asString)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsFormat implements FormatChecker interface.
|
|
|
|
func (f RegexFormatChecker) IsFormat(input interface{}) bool {
|
|
|
|
|
|
|
|
asString, ok := input.(string)
|
|
|
|
if ok == false {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if asString == "" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
_, err := regexp.Compile(asString)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2017-04-17 18:08:24 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
func (f JSONPointerFormatChecker) IsFormat(input interface{}) bool {
|
|
|
|
asString, ok := input.(string)
|
|
|
|
if ok == false {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return rxJSONPointer.MatchString(asString)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
func (f RelativeJSONPointerFormatChecker) IsFormat(input interface{}) bool {
|
|
|
|
asString, ok := input.(string)
|
|
|
|
if ok == false {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return rxRelJSONPointer.MatchString(asString)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|