Skip to content

Commit

Permalink
Remove deprecated FromFunc, Nested, NestedMulti and RegexpMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
RussellLuo committed May 31, 2021
1 parent a08a4eb commit 6c56ee2
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 159 deletions.
41 changes: 0 additions & 41 deletions builtin.go
Expand Up @@ -17,9 +17,6 @@ func (f Func) Validate(field Field) Errors {
return f(field)
}

// FromFunc is DEPRECATED.
type FromFunc = Func

// validateSchema do the validation per the given schema, which is associated
// with the given field.
func validateSchema(schema Schema, field Field, prefixFunc func(string) string) (errs Errors) {
Expand Down Expand Up @@ -204,41 +201,6 @@ func Not(validator Validator) (mv *MessageValidator) {
return
}

// Nested is a composite validator factory used to create a validator, which will
// delegate the actual validation to its inner schema.
//
// This composite validator is DEPRECATED.
func Nested(schema Schema) Validator {
return Func(func(field Field) Errors {
nestedSchema := make(Schema, len(schema))
for f, v := range schema {
nestedSchema[F(field.Name+f.Name, f.ValuePtr)] = v
}
return Validate(nestedSchema)
})
}

// NestedMulti is a composite validator factory used to create a validator, which will
// delegate the actual validation to its inner multiple schemas, which are
// returned by calling f.
//
// This composite validator is DEPRECATED.
func NestedMulti(f func() []Schema) Validator {
schemas := f()
validators := make([]Validator, len(schemas))
for i, schema := range schemas {
validators[i] = Nested(schema)
}
return Func(func(field Field) (errs Errors) {
for _, v := range validators {
if err := v.Validate(field); err != nil {
errs.Extend(err)
}
}
return
})
}

// Lazy is a composite validator factory used to create a validator, which will
// call f only as needed, to delegate the actual validation to
// the validator returned by f.
Expand Down Expand Up @@ -862,6 +824,3 @@ func Match(re *regexp.Regexp) (mv *MessageValidator) {
}
return
}

// RegexpMatch is DEPRECATED.
var RegexpMatch = Match
119 changes: 1 addition & 118 deletions validating_test.go
@@ -1,7 +1,6 @@
package validating

import (
"fmt"
"math"
"reflect"
"regexp"
Expand Down Expand Up @@ -229,56 +228,6 @@ func TestNot(t *testing.T) {
}
}

func TestNested(t *testing.T) {
cases := []struct {
schemaMaker func() Schema
errs Errors
}{
{
func() Schema {
post := Post{}
return Schema{
F("author", &post.Author): Nested(Schema{}),
}
},
nil,
},
{
func() Schema {
post := Post{}
return Schema{
F("author", &post.Author): Nested(Schema{
F(".name", &post.Author.Name): Nonzero(),
F(".age", &post.Author.Age): Nonzero(),
}),
}
},
Errors{
NewError("author.name", ErrInvalid, "is zero valued"),
NewError("author.age", ErrInvalid, "is zero valued"),
},
},
{
func() Schema {
post := Post{Author: Author{"russell", 10}}
return Schema{
F("author", &post.Author): Nested(Schema{
F(".name", &post.Author.Name): Nonzero(),
F(".age", &post.Author.Age): Nonzero(),
}),
}
},
nil,
},
}
for _, c := range cases {
errs := Validate(c.schemaMaker())
if !reflect.DeepEqual(makeErrsMap(errs), makeErrsMap(c.errs)) {
t.Errorf("Got (%+v) != Want (%+v)", errs, c.errs)
}
}
}

func TestMap(t *testing.T) {
cases := []struct {
schemaMaker func() Schema
Expand Down Expand Up @@ -349,7 +298,7 @@ func TestMap(t *testing.T) {
}
}

func TestNestedSlice(t *testing.T) {
func TestSlice(t *testing.T) {
cases := []struct {
schemaMaker func() Schema
errs Errors
Expand Down Expand Up @@ -417,72 +366,6 @@ func TestNestedSlice(t *testing.T) {
}
}

func TestNestedMulti(t *testing.T) {
cases := []struct {
schemaMaker func() Schema
errs Errors
}{
{
func() Schema {
post := Post{}
return Schema{
F("comments", &post.Comments): NestedMulti(func() []Schema {
return nil
}),
}
},
nil,
},
{
func() Schema {
post := Post{Comments: []Comment{
{"", time.Time{}},
}}
return Schema{
F("comments", &post.Comments): NestedMulti(func() (schemas []Schema) {
for i := range post.Comments {
schemas = append(schemas, Schema{
F(fmt.Sprintf("[%d].content", i), &post.Comments[i].Content): Nonzero(),
F(fmt.Sprintf("[%d].created_at", i), &post.Comments[i].CreatedAt): Nonzero(),
})
}
return
}),
}
},
Errors{
NewError("comments[0].content", ErrInvalid, "is zero valued"),
NewError("comments[0].created_at", ErrInvalid, "is zero valued"),
},
},
{
func() Schema {
post := Post{Comments: []Comment{
{Content: "thanks", CreatedAt: time.Now()},
}}
return Schema{
F("comments", &post.Comments): NestedMulti(func() (schemas []Schema) {
for i := range post.Comments {
schemas = append(schemas, Schema{
F(fmt.Sprintf("[%d].content", i), &post.Comments[i].Content): Nonzero(),
F(fmt.Sprintf("[%d].created_at", i), &post.Comments[i].CreatedAt): Nonzero(),
})
}
return
}),
}
},
nil,
},
}
for _, c := range cases {
errs := Validate(c.schemaMaker())
if !reflect.DeepEqual(makeErrsMap(errs), makeErrsMap(c.errs)) {
t.Errorf("Got (%+v) != Want (%+v)", errs, c.errs)
}
}
}

func TestLazy(t *testing.T) {
cases := []struct {
schemaMaker func(*bool) Schema
Expand Down

0 comments on commit 6c56ee2

Please sign in to comment.