| 1 |
// Copyright 2023-2025 Princess Beef Heavy Industries, LLC / Dave Shanley |
| 2 |
// https://pb33f.io |
| 3 |
|
| 4 |
package helpers |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"strings" |
| 9 |
"unicode" |
| 10 |
|
| 11 |
"github.com/santhosh-tekuri/jsonschema/v6" |
| 12 |
) |
| 13 |
|
| 14 |
// ExtractJSONPathFromValidationError traverses and processes a ValidationError to construct a JSONPath string representation of its instance location. |
| 15 |
func ExtractJSONPathFromValidationError(e *jsonschema.ValidationError) string { |
| 16 |
if len(e.Causes) > 0 { |
| 17 |
for _, cause := range e.Causes { |
| 18 |
ExtractJSONPathFromValidationError(cause) |
| 19 |
} |
| 20 |
} |
| 21 |
|
| 22 |
if len(e.InstanceLocation) > 0 { |
| 23 |
|
| 24 |
var b strings.Builder |
| 25 |
b.WriteString("$") |
| 26 |
|
| 27 |
for _, seg := range e.InstanceLocation { |
| 28 |
switch { |
| 29 |
case isNumeric(seg): |
| 30 |
fmt.Fprintf(&b, "[%s]", seg) |
| 31 |
|
| 32 |
case isSimpleIdentifier(seg): |
| 33 |
b.WriteByte('.') |
| 34 |
b.WriteString(seg) |
| 35 |
|
| 36 |
default: |
| 37 |
esc := escapeBracketString(seg) |
| 38 |
b.WriteString("['") |
| 39 |
b.WriteString(esc) |
| 40 |
b.WriteString("']") |
| 41 |
} |
| 42 |
} |
| 43 |
return b.String() |
| 44 |
} |
| 45 |
return "" |
| 46 |
} |
| 47 |
|
| 48 |
// isNumeric returns true if s is a non‐empty string of digits. |
| 49 |
func isNumeric(s string) bool { |
| 50 |
if s == "" { |
| 51 |
return false |
| 52 |
} |
| 53 |
for _, r := range s { |
| 54 |
if r < '0' || r > '9' { |
| 55 |
return false |
| 56 |
} |
| 57 |
} |
| 58 |
return true |
| 59 |
} |
| 60 |
|
| 61 |
// isSimpleIdentifier returns true if s matches [A-Za-z_][A-Za-z0-9_]*. |
| 62 |
func isSimpleIdentifier(s string) bool { |
| 63 |
for i, r := range s { |
| 64 |
if i == 0 { |
| 65 |
if !unicode.IsLetter(r) && r != '_' { |
| 66 |
return false |
| 67 |
} |
| 68 |
} else { |
| 69 |
if !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '_' { |
| 70 |
return false |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
return len(s) > 0 |
| 75 |
} |
| 76 |
|
| 77 |
// escapeBracketString escapes backslashes and single‐quotes for inside ['...'] |
| 78 |
func escapeBracketString(s string) string { |
| 79 |
s = strings.ReplaceAll(s, `\`, `\\`) |
| 80 |
s = strings.ReplaceAll(s, `'`, `\'`) |
| 81 |
return s |
| 82 |
} |
| 83 |
|
| 84 |
// ExtractJSONPathsFromValidationErrors takes a slice of ValidationError pointers and returns a slice of JSONPath strings |
| 85 |
func ExtractJSONPathsFromValidationErrors(errors []*jsonschema.ValidationError) []string { |
| 86 |
var paths []string |
| 87 |
for _, err := range errors { |
| 88 |
path := ExtractJSONPathFromValidationError(err) |
| 89 |
if path != "" { |
| 90 |
paths = append(paths, path) |
| 91 |
} |
| 92 |
} |
| 93 |
return paths |
| 94 |
} |
| 95 |
|
| 96 |
// ExtractFieldNameFromInstanceLocation returns the last segment of the instance location as the field name |
| 97 |
func ExtractFieldNameFromInstanceLocation(instanceLocation []string) string { |
| 98 |
if len(instanceLocation) == 0 { |
| 99 |
return "" |
| 100 |
} |
| 101 |
return instanceLocation[len(instanceLocation)-1] |
| 102 |
} |
| 103 |
|
| 104 |
// ExtractFieldNameFromStringLocation returns the last segment of the instance location as the field name |
| 105 |
// when the location is provided as a string path |
| 106 |
func ExtractFieldNameFromStringLocation(instanceLocation string) string { |
| 107 |
if instanceLocation == "" { |
| 108 |
return "" |
| 109 |
} |
| 110 |
|
| 111 |
// Handle string format like "/properties/email" or "/0/name" |
| 112 |
segments := strings.Split(strings.Trim(instanceLocation, "/"), "/") |
| 113 |
if len(segments) == 0 || (len(segments) == 1 && segments[0] == "") { |
| 114 |
return "" |
| 115 |
} |
| 116 |
|
| 117 |
return segments[len(segments)-1] |
| 118 |
} |
| 119 |
|
| 120 |
// ExtractJSONPathFromInstanceLocation creates a JSONPath string from instance location segments |
| 121 |
func ExtractJSONPathFromInstanceLocation(instanceLocation []string) string { |
| 122 |
if len(instanceLocation) == 0 { |
| 123 |
return "" |
| 124 |
} |
| 125 |
|
| 126 |
var b strings.Builder |
| 127 |
b.WriteString("$") |
| 128 |
|
| 129 |
for _, seg := range instanceLocation { |
| 130 |
switch { |
| 131 |
case isNumeric(seg): |
| 132 |
fmt.Fprintf(&b, "[%s]", seg) |
| 133 |
|
| 134 |
case isSimpleIdentifier(seg): |
| 135 |
b.WriteByte('.') |
| 136 |
b.WriteString(seg) |
| 137 |
|
| 138 |
default: |
| 139 |
esc := escapeBracketString(seg) |
| 140 |
b.WriteString("['") |
| 141 |
b.WriteString(esc) |
| 142 |
b.WriteString("']") |
| 143 |
} |
| 144 |
} |
| 145 |
return b.String() |
| 146 |
} |
| 147 |
|
| 148 |
// ExtractJSONPathFromStringLocation creates a JSONPath string from string-based instance location |
| 149 |
func ExtractJSONPathFromStringLocation(instanceLocation string) string { |
| 150 |
if instanceLocation == "" { |
| 151 |
return "" |
| 152 |
} |
| 153 |
|
| 154 |
// Convert string format like "/properties/email" to array format |
| 155 |
segments := strings.Split(strings.Trim(instanceLocation, "/"), "/") |
| 156 |
if len(segments) == 0 || (len(segments) == 1 && segments[0] == "") { |
| 157 |
return "" |
| 158 |
} |
| 159 |
|
| 160 |
return ExtractJSONPathFromInstanceLocation(segments) |
| 161 |
} |
| 162 |
|
| 163 |
// ConvertStringLocationToPathSegments converts a string-based instance location to path segments array |
| 164 |
// Handles edge cases like empty strings and root-only paths |
| 165 |
func ConvertStringLocationToPathSegments(instanceLocation string) []string { |
| 166 |
if instanceLocation == "" { |
| 167 |
return []string{} |
| 168 |
} |
| 169 |
|
| 170 |
segments := strings.Split(strings.Trim(instanceLocation, "/"), "/") |
| 171 |
if len(segments) == 1 && segments[0] == "" { |
| 172 |
return []string{} |
| 173 |
} |
| 174 |
|
| 175 |
return segments |
| 176 |
} |