...
1 package jsonschemax
2
3 import (
4 "errors"
5 "fmt"
6 "io"
7 "strings"
8
9 "github.com/tidwall/gjson"
10
11 "github.com/ory/jsonschema/v3"
12 )
13
14 func FormatValidationErrorForCLI(w io.Writer, conf []byte, err error) {
15 if err == nil {
16 return
17 }
18
19 if e := new(jsonschema.ValidationError); errors.As(err, &e) {
20 _, _ = fmt.Fprintln(w, "The configuration contains values or keys which are invalid:")
21 pointer, validation := FormatError(e)
22
23 if pointer == "#" {
24 if len(e.Causes) == 0 {
25 _, _ = fmt.Fprintln(w, "(root)")
26 _, _ = fmt.Fprintln(w, "^-- "+validation)
27 _, _ = fmt.Fprintln(w, "")
28 }
29 } else {
30 spaces := make([]string, len(pointer)+3)
31 _, _ = fmt.Fprintf(w, "%s: %+v", pointer, gjson.GetBytes(conf, pointer).Value())
32 _, _ = fmt.Fprintln(w, "")
33 _, _ = fmt.Fprintf(w, "%s^-- %s", strings.Join(spaces, " "), validation)
34 _, _ = fmt.Fprintln(w, "")
35 _, _ = fmt.Fprintln(w, "")
36 }
37
38 for _, cause := range e.Causes {
39 FormatValidationErrorForCLI(w, conf, cause)
40 }
41 return
42 }
43 }
44
45 func FormatError(e *jsonschema.ValidationError) (string, string) {
46 var (
47 err error
48 pointer string
49 message string
50 )
51
52 pointer = e.InstancePtr
53 message = e.Message
54 switch ctx := e.Context.(type) {
55 case *jsonschema.ValidationErrorContextRequired:
56 if len(ctx.Missing) > 0 {
57 message = "one or more required properties are missing"
58 pointer = ctx.Missing[0]
59 }
60 }
61
62
63 pointer, err = JSONPointerToDotNotation(pointer)
64 if err != nil {
65 pointer = e.InstancePtr
66 }
67
68 return pointer, message
69 }
70
View as plain text