Skip to content

Commit

Permalink
feat: extend validation for default enum value (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
slipros authored Mar 16, 2023
1 parent d6793de commit 35f1896
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
19 changes: 18 additions & 1 deletion schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,24 @@ func isValidDefault(schema Schema, def any) (any, bool) {
case Null:
return nullDefault, def == nil

case String, Bytes, Enum, Fixed:
case Enum:
v, ok := def.(string)
if !ok || len(v) == 0 {
return def, false
}

enumSchema := schema.(*EnumSchema)
found := false
for i := range enumSchema.symbols {
if def == enumSchema.symbols[i] {
found = true
break
}
}

return def, found

case String, Bytes, Fixed:
if _, ok := def.(string); ok {
return def, true
}
Expand Down
22 changes: 20 additions & 2 deletions schema_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,28 @@ func TestIsValidDefault(t *testing.T) {
s, _ := NewEnumSchema("foo", "", []string{"BAR"})
return s
},
def: "test",
want: "test",
def: "BAR",
want: "BAR",
wantOk: true,
},
{
name: "Enum Invalid Default",
schemaFn: func() Schema {
s, _ := NewEnumSchema("foo", "", []string{"BAR"})
return s
},
def: "BUP",
wantOk: false,
},
{
name: "Enum Empty string",
schemaFn: func() Schema {
s, _ := NewEnumSchema("foo", "", []string{"BAR"})
return s
},
def: "",
wantOk: false,
},
{
name: "Enum Invalid Type",
schemaFn: func() Schema {
Expand Down

0 comments on commit 35f1896

Please sign in to comment.