Skip to content

Commit

Permalink
Configure the schema format if the email validator is set
Browse files Browse the repository at this point in the history
This commit set the schema format to "email" if the email validator is
set on a field.
  • Loading branch information
mcorbin committed Jul 1, 2022
1 parent aaea309 commit d6a9219
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
4 changes: 4 additions & 0 deletions openapi/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,10 @@ func (g *Generator) updateSchemaValidation(schema *Schema, sf reflect.StructFiel
if t == "dive" || t == "keys" {
break
}
if t == "email" {
schema.Format = "email"
break
}
// Tags can be joined together with an OR operator.
parts := strings.Split(t, "|")

Expand Down
17 changes: 17 additions & 0 deletions openapi/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,23 @@ func TestNewSchemaFromStructFieldErrors(t *testing.T) {
assert.Equal(t, reflect.Bool, fe.Type.Kind())
}

func TestNewSchemaFromStructFieldFormat(t *testing.T) {
g := gen(t)

type T struct {
A string `validate:"email" default:"foobar"`
}
typ := reflect.TypeOf(T{})

// Field A is required and has a default value.
sor := g.newSchemaFromStructField(typ.Field(0), true, "A", typ)
assert.NotNil(t, sor)
assert.Len(t, g.Errors(), 1)
assert.Implements(t, (*error)(nil), g.Errors()[0])
assert.NotEmpty(t, g.Errors()[0].Error())
assert.Equal(t, sor.Schema.Format, "email")
}

func diffJSON(a, b []byte) (bool, error) {
var j, j2 interface{}
if err := json.Unmarshal(a, &j); err != nil {
Expand Down

0 comments on commit d6a9219

Please sign in to comment.