Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
35 changes: 35 additions & 0 deletions output.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,38 @@ func (ve *ValidationError) DetailedOutput() Detailed {
Errors: errors,
}
}

// Truncated return output in format
type Truncated struct {
Valid bool `json:"valid"`
KeywordLocation string `json:"keywordLocation"`
AbsoluteKeywordLocation string `json:"absoluteKeywordLocation"`
InstanceLocation string `json:"instanceLocation"`
Error string `json:"error,omitempty"`
Errors []BasicError `json:"errors,omitempty"`
}

func (ve *ValidationError) TruncatedOutput() Truncated {
var errors []BasicError
var flatten func(*ValidationError)
flatten = func(ve *ValidationError) {
errors = append(errors, BasicError{
KeywordLocation: ve.KeywordLocation,
AbsoluteKeywordLocation: ve.AbsoluteKeywordLocation,
InstanceLocation: ve.InstanceLocation,
Error: ve.Message,
})
for _, cause := range ve.Causes {
flatten(cause)
break
}
}
flatten(ve)
return Truncated{
KeywordLocation: ve.KeywordLocation,
AbsoluteKeywordLocation: ve.AbsoluteKeywordLocation,
InstanceLocation: ve.InstanceLocation,
Error: ve.Message,
Errors: errors,
}
}