Skip to content

Commit 3440e0c

Browse files
committed
schema: Fix unknown scalar types
Also properly validate untyped schema types, and fail if unknown scalar types are present.
1 parent bbe0f82 commit 3440e0c

File tree

5 files changed

+16
-8
lines changed

5 files changed

+16
-8
lines changed

merge/nested_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ var nestedTypeParser = func() Parser {
5555
scalar: string
5656
- name: value
5757
type:
58-
scalar: number
58+
scalar: numeric
5959
- name: listOfLists
6060
list:
6161
elementType:

schema/elements.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ type Atom struct {
7373
}
7474

7575
// Scalar (AKA "primitive") represents a type which has a single value which is
76-
// either numeric, string, or boolean.
76+
// either numeric, string, or boolean, or untyped for any of them.
7777
//
7878
// TODO: split numeric into float/int? Something even more fine-grained?
7979
type Scalar string
@@ -82,6 +82,7 @@ const (
8282
Numeric = Scalar("numeric")
8383
String = Scalar("string")
8484
Boolean = Scalar("boolean")
85+
Untyped = Scalar("untyped")
8586
)
8687

8788
// ElementRelationship is an enum of the different possible relationships

schema/schemaschema.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ var SchemaSchemaYAML = `types:
110110
scalar: string
111111
- name: deduceInvalidDiscriminator
112112
type:
113-
scalar: bool
113+
scalar: boolean
114114
- name: fields
115115
type:
116116
list:

typed/merge.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,12 @@ func (w *mergingWalker) doLeaf() {
113113
w.rule(w)
114114
}
115115

116-
func (w *mergingWalker) doScalar(t *schema.Scalar) (errs ValidationErrors) {
117-
errs = append(errs, validateScalar(t, w.lhs, "lhs: ")...)
118-
errs = append(errs, validateScalar(t, w.rhs, "rhs: ")...)
119-
if len(errs) > 0 {
120-
return errs
116+
func (w *mergingWalker) doScalar(t *schema.Scalar) ValidationErrors {
117+
// Make sure at least one side is a valid scalar.
118+
lerrs := validateScalar(t, w.lhs, "lhs: ")
119+
rerrs := validateScalar(t, w.rhs, "rhs: ")
120+
if len(lerrs) > 0 && len(rerrs) > 0 {
121+
return append(lerrs, rerrs...)
121122
}
122123

123124
// All scalars are leaf fields.

typed/validate.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ func validateScalar(t *schema.Scalar, v value.Value, prefix string) (errs Valida
102102
if !v.IsBool() {
103103
return errorf("%vexpected boolean, got %v", prefix, v)
104104
}
105+
case schema.Untyped:
106+
if !v.IsFloat() && !v.IsInt() && !v.IsString() && !v.IsBool() {
107+
return errorf("%vexpected any scalar, got %v", prefix, v)
108+
}
109+
default:
110+
return errorf("%vunexpected scalar type in schema: %v", prefix, *t)
105111
}
106112
return nil
107113
}

0 commit comments

Comments
 (0)