Skip to content

Commit

Permalink
feat: using bytes.Buffer write string for ValidationError
Browse files Browse the repository at this point in the history
  • Loading branch information
huzedong1 committed Mar 15, 2023
1 parent ea2d5ad commit 411cefa
Showing 1 changed file with 13 additions and 6 deletions.
19 changes: 13 additions & 6 deletions errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package jsonschema

import (
"bytes"
"fmt"
"strings"
)
Expand Down Expand Up @@ -98,16 +99,22 @@ func (ve *ValidationError) Error() string {
return fmt.Sprintf("jsonschema: %s does not validate with %s: %s", quote(leaf.InstanceLocation), u+"#"+leaf.KeywordLocation, leaf.Message)
}

func (ve *ValidationError) GoString() string {
func (ve *ValidationError) writeIntoBuffer(msgBuf *bytes.Buffer, indent int) {
sloc := ve.AbsoluteKeywordLocation
sloc = sloc[strings.IndexByte(sloc, '#')+1:]
msg := fmt.Sprintf("[I#%s] [S#%s] %s", ve.InstanceLocation, sloc, ve.Message)
indentStr := strings.Repeat(" ", indent)
msgBuf.WriteString(indentStr)
msgBuf.WriteString(fmt.Sprintf("[I#%s] [S#%s] %s", ve.InstanceLocation, sloc, ve.Message))
for _, c := range ve.Causes {
for _, line := range strings.Split(c.GoString(), "\n") {
msg += "\n " + line
}
msgBuf.WriteRune('\n')
c.writeIntoBuffer(msgBuf, indent+2)
}
return msg
}

func (ve *ValidationError) GoString() string {
var msgBuf bytes.Buffer
ve.writeIntoBuffer(&msgBuf, 0)
return msgBuf.String()
}

func joinPtr(ptr1, ptr2 string) string {
Expand Down

0 comments on commit 411cefa

Please sign in to comment.