Skip to content

Commit 4f746db

Browse files
author
jennybuckley
committed
Fix useless error message 'invalid atom'
1 parent 4c5ecdc commit 4f746db

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

typed/helpers.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,27 @@ func resolveSchema(s *schema.Schema, tr schema.TypeRef, v *value.Value, ah atomH
114114
return handleAtom(a, tr, ah)
115115
}
116116

117-
func deduceAtom(a schema.Atom, v *value.Value) schema.Atom {
117+
// deduceAtom determines which of the possible types in atom 'atom' applies to value 'val'.
118+
// If val is of a type allowed by atom, return a copy of atom with all other types set to nil.
119+
// if val is nil, or is not of a type allowed by atom, just return the original atom,
120+
// and validation will fail at a later stage. (with a more useful error)
121+
func deduceAtom(atom schema.Atom, val *value.Value) schema.Atom {
118122
switch {
119-
case v == nil:
120-
case v.FloatValue != nil, v.IntValue != nil, v.StringValue != nil, v.BooleanValue != nil:
121-
return schema.Atom{Scalar: a.Scalar}
122-
case v.ListValue != nil:
123-
return schema.Atom{List: a.List}
124-
case v.MapValue != nil:
125-
return schema.Atom{Map: a.Map}
123+
case val == nil:
124+
case val.FloatValue != nil, val.IntValue != nil, val.StringValue != nil, val.BooleanValue != nil:
125+
if atom.Scalar != nil {
126+
return schema.Atom{Scalar: atom.Scalar}
127+
}
128+
case val.ListValue != nil:
129+
if atom.List != nil {
130+
return schema.Atom{List: atom.List}
131+
}
132+
case val.MapValue != nil:
133+
if atom.Map != nil {
134+
return schema.Atom{Map: atom.Map}
135+
}
126136
}
127-
return a
137+
return atom
128138
}
129139

130140
func handleAtom(a schema.Atom, tr schema.TypeRef, ah atomHandler) ValidationErrors {

typed/validate.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,11 @@ func (v *validatingObjectWalker) visitMapItems(t *schema.Map, m *value.Map) (err
198198
} else {
199199
v2 := v.prepareDescent(pe, t.ElementType)
200200
v2.value = item.Value
201-
errs = append(errs, v2.validate()...)
201+
if (t.ElementType == schema.TypeRef{}) {
202+
errs = append(errs, v2.errorf("field not declared in schema")...)
203+
} else {
204+
errs = append(errs, v2.validate()...)
205+
}
202206
v2.doNode()
203207
v.finishDescent(v2)
204208
}

typed/validate_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package typed_test
1818

1919
import (
2020
"fmt"
21+
"strings"
2122
"testing"
2223

2324
"sigs.k8s.io/structured-merge-diff/schema"
@@ -216,6 +217,8 @@ var validationCases = []validationTestCase{{
216217
invalidObjects: []typed.YAMLObject{
217218
`{"key":true,"value":1}`,
218219
`{"list":{"key":true,"value":1}}`,
220+
`{"list":true}`,
221+
`true`,
219222
`{"list":[{"key":true,"value":1}]}`,
220223
`{"list":[{"key":[],"value":1}]}`,
221224
`{"list":[{"key":{},"value":1}]}`,
@@ -261,6 +264,9 @@ func (tt validationTestCase) test(t *testing.T) {
261264
if err == nil {
262265
t.Errorf("Object should fail: %v\n%v", err, iv)
263266
}
267+
if strings.Contains(err.Error(), "invalid atom") {
268+
t.Errorf("Error should be useful, but got: %v\n%v", err, iv)
269+
}
264270
})
265271
}
266272
}

0 commit comments

Comments
 (0)