Skip to content

Commit

Permalink
Merge pull request #21 from Barberrrry/fix-pointer-nullable-validation
Browse files Browse the repository at this point in the history
Fix custom validation of nullable pointer
  • Loading branch information
vpbarb authored Dec 29, 2017
2 parents 9a95f1e + 1705d53 commit 540a20a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 7 deletions.
1 change: 1 addition & 0 deletions entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func toPrimitiveType(fieldType types.TypeDef, validateTags types.ValidatableTags

if len(validateTags) == 1 &&
(validateTags.ContainsTag(types.SimpleTag{Name: types.PointerNotNullKey})) ||
(validateTags.ContainsTag(types.SimpleTag{Name: types.PointerNullableKey})) ||
(validateTags.ContainsTag(types.SimpleTag{Name: types.StructFuncKey})) {
return fieldType
}
Expand Down
15 changes: 8 additions & 7 deletions examples/aliases/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ type FloatType float64
type MapType map[string]int

type User struct {
FirstName StringType `validate:"min_len=2,max_len=15"`
LastName string `validate:"min_len=1,max_len=15"`
NonEmptyString StringType `validate:"func=.ValidateNotEmpty"`
FamilyMembers IntType `validate:"min=1,max=100"`
SomeFloat FloatType `validate:"min=2.55,max=99.99"`
SomeMap MapType `validate:"min_items=2,key=[max_len=64],value=[min=-35,max=34]"`
SomePointer *StringType `validate:"not_null,min_len=20,max_len=150"`
FirstName StringType `validate:"min_len=2,max_len=15"`
LastName string `validate:"min_len=1,max_len=15"`
NonEmptyString StringType `validate:"func=.ValidateNotEmpty"`
FamilyMembers IntType `validate:"min=1,max=100"`
SomeFloat FloatType `validate:"min=2.55,max=99.99"`
SomeMap MapType `validate:"min_items=2,key=[max_len=64],value=[min=-35,max=34]"`
SomePointer *StringType `validate:"not_null,min_len=20,max_len=150"`
SomePointerNullable *StringType `validate:"nullable"`
}
19 changes: 19 additions & 0 deletions examples/aliases/entities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,24 @@ func Test_User_Validate(t *testing.T) {
require.NotNil(t, err)
assert.Equal(t, `[NonEmptyString: string is empty]`, err.Error())
})

t.Run("SomePointerNullable: valid", func(*testing.T) {
r := validUser
invalidString := StringType("aaaaaa")
r.SomePointerNullable = &invalidString

err := r.Validate()
require.NoError(t, err)
})

t.Run("SomePointerNullable: not valid", func(*testing.T) {
r := validUser
invalidString := StringType("a")
r.SomePointerNullable = &invalidString

err := r.Validate()
require.NotNil(t, err)
assert.Equal(t, `[SomePointerNullable: shorter than 3 chars]`, err.Error())
})
})
}
5 changes: 5 additions & 0 deletions examples/aliases/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,10 @@ func (r User) Validate() error {
errs.AddFieldf("SomePointer", "longer than 150 chars")
}
}
if r.SomePointerNullable != nil {
if err := r.SomePointerNullable.Validate(); err != nil {
errs.AddField("SomePointerNullable", err)
}
}
return errs.ErrorOrNil()
}

0 comments on commit 540a20a

Please sign in to comment.