Skip to content

Commit

Permalink
Address comment
Browse files Browse the repository at this point in the history
  • Loading branch information
cici37 committed Jul 20, 2023
1 parent c46ffc2 commit f50e742
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -325,19 +325,17 @@ func unescapeSingleQuote(s string) (string, error) {
// ValidFieldPath validates that jsonPath is a valid JSON Path containing only field and map accessors
// that are valid for the given schema, and returns a field.Path representation of the validated jsonPath or an error.
func ValidFieldPath(jsonPath string, schema *schema.Structural) (validFieldPath *field.Path, err error) {
appendToPath := func(name string) error {
if schema.AdditionalProperties != nil {
appendToPath := func(name string, isNamed bool) error {
if !isNamed {
validFieldPath = validFieldPath.Key(name)
schema = schema.AdditionalProperties.Structural
} else if schema.Properties != nil {
} else {
validFieldPath = validFieldPath.Child(name)
val, ok := schema.Properties[name]
if !ok {
return fmt.Errorf("does not refer to a valid field")
}
schema = &val
} else {
return fmt.Errorf("does not refer to a valid field")
}
return nil
}
Expand Down Expand Up @@ -382,6 +380,7 @@ func ValidFieldPath(jsonPath string, schema *schema.Structural) (validFieldPath
})

var tok string
var isNamed bool
for scanner.Scan() {
tok = scanner.Text()
switch tok {
Expand All @@ -397,7 +396,15 @@ func ValidFieldPath(jsonPath string, schema *schema.Structural) (validFieldPath
if err != nil {
return nil, fmt.Errorf("invalid string literal: %v", err)
}
if err := appendToPath(unescaped); err != nil {

if schema.Properties != nil {
isNamed = true
} else if schema.AdditionalProperties != nil {
isNamed = false
} else {
return nil, fmt.Errorf("does not refer to a valid field")
}
if err := appendToPath(unescaped, isNamed); err != nil {
return nil, err
}
if !scanner.Scan() {
Expand All @@ -412,7 +419,14 @@ func ValidFieldPath(jsonPath string, schema *schema.Structural) (validFieldPath
return nil, fmt.Errorf("unexpected end of JSON path")
}
tok = scanner.Text()
if err := appendToPath(tok); err != nil {
if schema.Properties != nil {
isNamed = true
} else if schema.AdditionalProperties != nil {
isNamed = false
} else {
return nil, fmt.Errorf("does not refer to a valid field")
}
if err := appendToPath(tok, isNamed); err != nil {
return nil, err
}
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2706,6 +2706,11 @@ func TestValidateFieldPath(t *testing.T) {
Type: "number",
},
},
"'foo'bar": {
Generic: schema.Generic{
Type: "number",
},
},
"a": {
Generic: schema.Generic{
Type: "object",
Expand Down Expand Up @@ -2799,6 +2804,20 @@ func TestValidateFieldPath(t *testing.T) {
schema: &sts,
validFieldPath: path.Child("a"),
},
{
name: "Valid 'foo'bar",
fieldPath: "['\\'foo\\'bar']",
pathOfFieldPath: path,
schema: &sts,
validFieldPath: path.Child("'foo'bar"),
},
{
name: "Invalid 'foo'bar",
fieldPath: ".\\'foo\\'bar",
pathOfFieldPath: path,
schema: &sts,
errDetail: "does not refer to a valid field",
},
{
name: "Invalid with whitespace",
fieldPath: ". a",
Expand Down Expand Up @@ -2987,7 +3006,7 @@ func TestValidateFieldPath(t *testing.T) {
t.Errorf("expected error to contain: %v, but get: %v", tc.errDetail, err)
}
if tc.validFieldPath != nil && tc.validFieldPath.String() != path.Child(validField.String()).String() {
t.Errorf("expected %v, got %v", tc.validFieldPath, validField)
t.Errorf("expected %v, got %v", tc.validFieldPath, path.Child(validField.String()))
}
})
}
Expand Down

0 comments on commit f50e742

Please sign in to comment.