Skip to content

Commit d1a5517

Browse files
author
Igor Lazarev
committed
implement ValidateNil method for NotBlankConstraint and BlankConstraint; update test cases to include nil type
1 parent b974624 commit d1a5517

3 files changed

Lines changed: 32 additions & 9 deletions

File tree

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ linters:
156156
- errorlint
157157
path: validationtest/assertion.go
158158
paths:
159+
- var
159160
- third_party$
160161
- builtin$
161162
- examples$

it/basic.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ func (c NotBlankConstraint[T]) WithMessage(template string, parameters ...valida
7171
return c
7272
}
7373

74+
func (c NotBlankConstraint[T]) ValidateNil(ctx context.Context, validator *validation.Validator, isNil bool) error {
75+
if c.isIgnored || validator.IsIgnoredForGroups(c.groups...) {
76+
return nil
77+
}
78+
if c.allowNil && isNil {
79+
return nil
80+
}
81+
if !isNil {
82+
return nil
83+
}
84+
85+
return c.newViolation(ctx, validator)
86+
}
87+
7488
func (c NotBlankConstraint[T]) ValidateBool(ctx context.Context, validator *validation.Validator, value *bool) error {
7589
if c.isIgnored || validator.IsIgnoredForGroups(c.groups...) {
7690
return nil
@@ -202,6 +216,14 @@ func (c BlankConstraint[T]) WithMessage(template string, parameters ...validatio
202216
return c
203217
}
204218

219+
func (c BlankConstraint[T]) ValidateNil(ctx context.Context, validator *validation.Validator, isNil bool) error {
220+
if c.isIgnored || validator.IsIgnoredForGroups(c.groups...) || isNil {
221+
return nil
222+
}
223+
224+
return c.newViolation(ctx, validator)
225+
}
226+
205227
func (c BlankConstraint[T]) ValidateBool(ctx context.Context, validator *validation.Validator, value *bool) error {
206228
if c.isIgnored || validator.IsIgnoredForGroups(c.groups...) || value == nil || !*value {
207229
return nil

test/constraints_basic_cases_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
var isNotBlankConstraintTestCases = []ConstraintValidationTestCase{
1212
{
1313
name: "IsNotBlank violation on nil",
14-
isApplicableFor: specificValueTypes(boolType, stringType, countableType, timeType),
14+
isApplicableFor: specificValueTypes(nilType, boolType, stringType, countableType, timeType),
1515
constraint: it.IsNotBlank(),
1616
assert: assertHasOneViolation(validation.ErrIsBlank, message.IsBlank),
1717
},
@@ -43,7 +43,7 @@ var isNotBlankConstraintTestCases = []ConstraintValidationTestCase{
4343
},
4444
{
4545
name: "IsNotBlank violation on nil with custom message",
46-
isApplicableFor: specificValueTypes(boolType, stringType, countableType, timeType),
46+
isApplicableFor: specificValueTypes(nilType, boolType, stringType, countableType, timeType),
4747
constraint: it.IsNotBlank().
4848
WithError(ErrCustom).
4949
WithMessage(
@@ -54,7 +54,7 @@ var isNotBlankConstraintTestCases = []ConstraintValidationTestCase{
5454
},
5555
{
5656
name: "IsNotBlank passes on value",
57-
isApplicableFor: specificValueTypes(boolType, stringType, countableType, timeType),
57+
isApplicableFor: specificValueTypes(nilType, boolType, stringType, countableType, timeType),
5858
boolValue: boolValue(true),
5959
intValue: intValue(1),
6060
floatValue: floatValue(0.1),
@@ -68,19 +68,19 @@ var isNotBlankConstraintTestCases = []ConstraintValidationTestCase{
6868
},
6969
{
7070
name: "IsNotBlank passes on nil when allowed",
71-
isApplicableFor: specificValueTypes(boolType, stringType, timeType),
71+
isApplicableFor: specificValueTypes(nilType, boolType, stringType, timeType),
7272
constraint: it.IsNotBlank().WithAllowedNil(),
7373
assert: assertNoError,
7474
},
7575
{
7676
name: "IsNotBlank passes on nil when condition is false",
77-
isApplicableFor: specificValueTypes(boolType, stringType, timeType),
77+
isApplicableFor: specificValueTypes(nilType, boolType, stringType, timeType),
7878
constraint: it.IsNotBlank().When(false),
7979
assert: assertNoError,
8080
},
8181
{
8282
name: "IsNotBlank passes on nil when groups not match",
83-
isApplicableFor: specificValueTypes(boolType, stringType, timeType),
83+
isApplicableFor: specificValueTypes(nilType, boolType, stringType, timeType),
8484
constraint: it.IsNotBlank().WhenGroups(testGroup),
8585
assert: assertNoError,
8686
},
@@ -239,7 +239,7 @@ var isBlankConstraintTestCases = []ConstraintValidationTestCase{
239239
},
240240
{
241241
name: "IsBlank passes on nil",
242-
isApplicableFor: specificValueTypes(boolType, stringType, countableType, timeType),
242+
isApplicableFor: specificValueTypes(nilType, boolType, stringType, countableType, timeType),
243243
constraint: it.IsBlank(),
244244
assert: assertNoError,
245245
},
@@ -259,7 +259,7 @@ var isBlankConstraintTestCases = []ConstraintValidationTestCase{
259259
},
260260
{
261261
name: "IsBlank passes on value when condition is false",
262-
isApplicableFor: specificValueTypes(boolType, stringType, countableType, timeType),
262+
isApplicableFor: specificValueTypes(nilType, boolType, stringType, countableType, timeType),
263263
boolValue: boolValue(true),
264264
intValue: intValue(1),
265265
floatValue: floatValue(0.1),
@@ -273,7 +273,7 @@ var isBlankConstraintTestCases = []ConstraintValidationTestCase{
273273
},
274274
{
275275
name: "IsBlank passes on value when groups not match",
276-
isApplicableFor: specificValueTypes(boolType, stringType, countableType, timeType),
276+
isApplicableFor: specificValueTypes(nilType, boolType, stringType, countableType, timeType),
277277
boolValue: boolValue(true),
278278
intValue: intValue(1),
279279
floatValue: floatValue(0.1),

0 commit comments

Comments
 (0)