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: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1119,13 +1119,13 @@ the above will check the properties in order specified by their <code>order:</co
The `v8n` tag can also support custom tokens which can be registered using `valix.RegisterCustomTagToken`.
Any registered custom tag tokens can be used in the `v8n` tag and will be processed when building a validator for a struct using `valix.ValidatorFor`

An example of how this is used can be found in [examples/custom_tag_tokens_test.go](https://github.com/marrow16/valix/blob/master/examples/custom_tag_tokens_test.go)
An example of how this is used can be found in [_examples/custom_tag_tokens_test.go](https://github.com/marrow16/valix/blob/master/examples/custom_tag_tokens_test.go)

#### Tag token aliases

If you find that you're using the same `v8n` tag tokens repeatedly - you can create aliases for these and then just reference the alias using a `$` prefix.

An example of how this is used can be found in [examples/tag_aliases_test.go](https://github.com/marrow16/valix/blob/master/examples/tag_aliases_test.go)
An example of how this is used can be found in [_examples/tag_aliases_test.go](https://github.com/marrow16/valix/blob/master/examples/tag_aliases_test.go)

#### Abbreviating constraints in tags

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
18 changes: 14 additions & 4 deletions validator_for.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,23 @@ func buildPropertyValidators(ty reflect.Type, ignoreOas bool) (Properties, error
newTy := reflect.New(ty)
for i := 0; i < cnt; i++ {
fld := ty.Field(i)
actualFld := newTy.Elem().FieldByName(fld.Name)
if actualFld.CanSet() {
pv, fn, err := propertyValidatorFromField(fld, ignoreOas)
if fld.Anonymous {
embeddedPtys, err := buildPropertyValidators(fld.Type, ignoreOas)
if err != nil {
return nil, err
}
result[fn] = pv
for k, v := range embeddedPtys {
result[k] = v
}
} else {
actualFld := newTy.Elem().FieldByName(fld.Name)
if actualFld.CanSet() {
pv, fn, err := propertyValidatorFromField(fld, ignoreOas)
if err != nil {
return nil, err
}
result[fn] = pv
}
}
}
return result, nil
Expand Down
34 changes: 34 additions & 0 deletions validator_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package valix
import (
"errors"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
"time"
Expand Down Expand Up @@ -779,3 +780,36 @@ func TestOptionIgnoreOasTags(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, v)
}

func TestValidatorFor_Embedded(t *testing.T) {
type abstractStruct struct {
Aaa string `json:"aaa"`
}
type baseStruct struct {
abstractStruct
Foo string `json:"foo"`
Bar string `json:"bar"`
}
type myStruct struct {
baseStruct
Baz string `json:"baz"`
}
v, err := ValidatorFor(myStruct{}, nil)
require.NoError(t, err)
require.NotNil(t, v)
require.Equal(t, 4, len(v.Properties))

testJson := `{
"aaa": "aaa",
"foo": "foo",
"bar": "bar",
"baz": "baz"}`
my := &myStruct{}
ok, violations, _ := v.ValidateStringInto(testJson, my)
require.True(t, ok)
require.Equal(t, 0, len(violations))
assert.Equal(t, "aaa", my.Aaa)
assert.Equal(t, "foo", my.Foo)
assert.Equal(t, "bar", my.Bar)
assert.Equal(t, "baz", my.Baz)
}
Loading