Skip to content

Commit

Permalink
Require the format keyword to be of type string, restructure some of
Browse files Browse the repository at this point in the history
the format logic to be handled at validation time instead of schema
parsing time and use a RWMutex instead of a regular Mutex.
  • Loading branch information
johandorland committed Jun 26, 2019
1 parent b0c64e0 commit 16a6735
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
13 changes: 7 additions & 6 deletions format_checkers.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ var (

rxRelJSONPointer = regexp.MustCompile("^(?:0|[1-9][0-9]*)(?:#|(?:/(?:[^~/]|~0|~1)*)*)$")

lock = new(sync.Mutex)
lock = new(sync.RWMutex)
)

// Add adds a FormatChecker to the FormatCheckerChain
Expand All @@ -165,22 +165,23 @@ func (c *FormatCheckerChain) Remove(name string) *FormatCheckerChain {

// Has checks to see if the FormatCheckerChain holds a FormatChecker with the given name
func (c *FormatCheckerChain) Has(name string) bool {
lock.Lock()
lock.RLock()
_, ok := c.formatters[name]
lock.Unlock()
lock.RUnlock()

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 {
lock.Lock()
lock.RLock()
f, ok := c.formatters[name]
lock.Unlock()
lock.RUnlock()

// If a format is unrecognized it should always pass validation
if !ok {
return false
return true
}

return f.IsFormat(input)
Expand Down
8 changes: 6 additions & 2 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -673,9 +673,13 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema)

if existsMapKey(m, KEY_FORMAT) {
formatString, ok := m[KEY_FORMAT].(string)
if ok && FormatCheckers.Has(formatString) {
currentSchema.format = formatString
if !ok {
return errors.New(formatErrorDescription(
Locale.MustBeOfType(),
ErrorDetails{"key": KEY_FORMAT, "type": TYPE_STRING},
))
}
currentSchema.format = formatString
}

// validation : object
Expand Down

0 comments on commit 16a6735

Please sign in to comment.