Skip to content

Commit 9d3e5c6

Browse files
committed
comments
1 parent 0f325e4 commit 9d3e5c6

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

jsonpointer.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ import (
2020
"strings"
2121
)
2222

23-
// YieldOperation returns resolution back to jsonpointer's internal resolution.
23+
// YieldOperation returns resolution back to jsonpointer. This error can be
24+
// utilized within methods satisfying Resolver (ResolveJSONPointer), Assigner
25+
// (AssignByJSONPointer), and Deleter (DeleteByJSONPointer) as an escape hatch.
26+
//
27+
// The intent is is that there may only be certain fields that your application
28+
// would like to manually resolve. For the rest, you'd return YieldOperation as
29+
// the error.
2430
var YieldOperation = errors.New("yield resolution to jsonpointer")
2531

2632
var (
@@ -107,6 +113,14 @@ func (p JSONPointer) PrependString(token string) JSONPointer {
107113
return p.Prepend(Token(encoder.Replace(token)))
108114
}
109115

116+
// Validate performs validation on p. The following checks are performed:
117+
//
118+
// - p must be either empty or start with '/
119+
//
120+
// - p must be properly encoded, meaning that '~' must be immediately followed
121+
// by a '0' or '1'.
122+
//
123+
//
110124
func (p JSONPointer) Validate() (err error) {
111125
if err = p.validateStart(); err != nil {
112126
return err
@@ -118,8 +132,11 @@ func (p JSONPointer) validateeEncoding() error {
118132
if len(p) == 0 {
119133
return nil
120134
}
135+
if p[len(p)-1] == '~' {
136+
return ErrMalformedEncoding
137+
}
121138
for i := len(p) - 1; i >= 0; i-- {
122-
if p[i] == '~' && (i == len(p)-1 || (p[i+1] != '0' && p[i+1] != '1')) {
139+
if p[i] == '~' && (p[i+1] != '0' && p[i+1] != '1') {
123140
return ErrMalformedEncoding
124141
}
125142
}

0 commit comments

Comments
 (0)