Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
73 lines
1.6 KiB
Go
73 lines
1.6 KiB
Go
// Copyright © 2023 Ory Corp
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package jsonschemax
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
|
|
"github.com/tidwall/gjson"
|
|
|
|
"github.com/ory/jsonschema/v3"
|
|
)
|
|
|
|
func FormatValidationErrorForCLI(w io.Writer, conf []byte, err error) {
|
|
if err == nil {
|
|
return
|
|
}
|
|
|
|
if e := new(jsonschema.ValidationError); errors.As(err, &e) {
|
|
_, _ = fmt.Fprintln(w, "The configuration contains values or keys which are invalid:")
|
|
pointer, validation := FormatError(e)
|
|
|
|
if pointer == "#" {
|
|
if len(e.Causes) == 0 {
|
|
_, _ = fmt.Fprintln(w, "(root)")
|
|
_, _ = fmt.Fprintln(w, "^-- "+validation)
|
|
_, _ = fmt.Fprintln(w, "")
|
|
}
|
|
} else {
|
|
spaces := make([]string, len(pointer)+3)
|
|
_, _ = fmt.Fprintf(w, "%s: %+v", pointer, gjson.GetBytes(conf, pointer).Value())
|
|
_, _ = fmt.Fprintln(w, "")
|
|
_, _ = fmt.Fprintf(w, "%s^-- %s", strings.Join(spaces, " "), validation)
|
|
_, _ = fmt.Fprintln(w, "")
|
|
_, _ = fmt.Fprintln(w, "")
|
|
}
|
|
|
|
for _, cause := range e.Causes {
|
|
FormatValidationErrorForCLI(w, conf, cause)
|
|
}
|
|
return
|
|
}
|
|
}
|
|
|
|
func FormatError(e *jsonschema.ValidationError) (string, string) {
|
|
var (
|
|
err error
|
|
pointer string
|
|
message string
|
|
)
|
|
|
|
pointer = e.InstancePtr
|
|
message = e.Message
|
|
switch ctx := e.Context.(type) {
|
|
case *jsonschema.ValidationErrorContextRequired:
|
|
if len(ctx.Missing) > 0 {
|
|
message = "one or more required properties are missing"
|
|
pointer = ctx.Missing[0]
|
|
}
|
|
}
|
|
|
|
// We can ignore the error as it will simply echo the pointer.
|
|
pointer, err = JSONPointerToDotNotation(pointer)
|
|
if err != nil {
|
|
pointer = e.InstancePtr
|
|
}
|
|
|
|
return pointer, message
|
|
}
|