Skip to content

Commit

Permalink
Merge pull request #32 from veleek/31-nestedcompositeerror
Browse files Browse the repository at this point in the history
Add ValidateName to CompositeError
  • Loading branch information
casualjim authored Aug 23, 2021
2 parents 8b5b779 + 351dfa8 commit b4f29f3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
13 changes: 9 additions & 4 deletions headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,16 @@ func (e Validation) MarshalJSON() ([]byte, error) {
})
}

// ValidateName produces an error message name for an aliased property
// ValidateName sets the name for a validation or updates it for a nested property
func (e *Validation) ValidateName(name string) *Validation {
if e.Name == "" && name != "" {
e.Name = name
e.message = name + e.message
if name != "" {
if e.Name == "" {
e.Name = name
e.message = name + e.message
} else {
e.Name = name + "." + e.Name
e.message = name + "." + e.message
}
}
return e
}
Expand Down
13 changes: 13 additions & 0 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ func CompositeValidationError(errors ...error) *CompositeError {
}
}

// ValidateName recursively sets the name for all validations or updates them for nested properties
func (c *CompositeError) ValidateName(name string) *CompositeError {
for i, e := range c.Errors {
if ve, ok := e.(*Validation); ok {
c.Errors[i] = ve.ValidateName(name)
} else if ce, ok := e.(*CompositeError); ok {
c.Errors[i] = ce.ValidateName(name)
}
}

return c
}

// FailedAllPatternProperties an error for when the property doesn't match a pattern
func FailedAllPatternProperties(name, in, key string) *Validation {
msg := fmt.Sprintf(failedAllPatternProps, name, key, in)
Expand Down

0 comments on commit b4f29f3

Please sign in to comment.