Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ Foo *string `validate:"required"`
passes if `*Foo != ""` NOT if `Foo != nil`.

It validates both public and private fields, as long as they have
the validation tags.
the validation tags. To skip a field entirely (including nested
structs), use `validate:"-"`.

Non-goals:

Expand All @@ -46,6 +47,7 @@ rather than in struct tags "perlisms".

| Check | Description | Domain |
| -------------- | ------------------------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| - | skip field validation | `any` |
| required | must NOT be `IsZero()` | `any` |
| regex:`<rx>` | must match `<rx>` | `string`, `Stringer` |
| eq:`<number>` | must == `number` | [CanInt](https://pkg.go.dev/reflect#Value.CanInt), [CanUint](https://pkg.go.dev/reflect#Value.CanUint), [CanFloat](https://pkg.go.dev/reflect#Value.CanFloat), Can[Len](https://pkg.go.dev/reflect#Value.Len) |
Expand Down
8 changes: 4 additions & 4 deletions coverage-badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/alexaandru/vali

go 1.25.4
go 1.25.6
4 changes: 4 additions & 0 deletions vali.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ func (v *Validator) validate(val reflect.Value, tag string, scope ...string) (er
reflTag := iType.Tag
tag = strings.TrimSpace(reflTag.Get(v.tag))

if tag == "-" {
continue
}

iVal := val.Field(i)
for iVal.Kind() == reflect.Pointer {
iVal = iVal.Elem()
Expand Down
41 changes: 41 additions & 0 deletions vali_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,47 @@ func TestValidate(t *testing.T) { //nolint:funlen // ok
{struct {
username string `validate:"required,alphanum,min:3,max:20"`
}{username: "ab"}, nil, "", "username: min check failed: len 2 is less than 3", ErrCheckFailed},

// Skip field with validate:"-".
{struct {
Foo string `validate:"-"`
}{Foo: ""}, nil, "", "", nil},
{struct {
foo string `validate:"-"`
}{foo: ""}, nil, "", "", nil},
{struct {
Foo string `validate:"-"`
}{Foo: "invalid-email"}, nil, "", "", nil},

// Skip nested struct.
{struct {
Nested struct {
Bar string `validate:"required"`
} `validate:"-"`
}{}, nil, "", "", nil},

// Skip with pointers.
{struct {
Foo *string `validate:"-"`
}{Foo: nil}, nil, "", "", nil},
{struct {
Foo **string `validate:"-"`
}{Foo: nil}, nil, "", "", nil},

// Skip with whitespace.
{struct {
Foo string `validate:" - "`
}{Foo: ""}, nil, "", "", nil},

// Mixed: some fields skipped, some validated.
{struct {
Skip string `validate:"-"`
Check string `validate:"required"`
}{Skip: "", Check: ""}, nil, "", "Check: required check failed: value missing", ErrRequired},
{struct {
Skip string `validate:"-"`
Check string `validate:"required"`
}{Skip: "", Check: "valid"}, nil, "", "", nil},
}

for _, tc := range testCases {
Expand Down
Loading