Skip to content

Commit

Permalink
refactor: make variable name explicit
Browse files Browse the repository at this point in the history
BREAKING CHANGE: rename Params to TagParams in FuncOption
  • Loading branch information
utahta committed May 20, 2019
1 parent 15030f7 commit 35b7e5c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func ExampleValidateStruct_setFunc() {
v := validator.New(
validator.WithFunc("contentType", func(_ context.Context, f validator.Field, opt validator.FuncOption) (bool, error) {
v := f.Value().String()
for _, param := range opt.Params {
for _, param := range opt.TagParams {
if v == param {
return true, nil
}
Expand Down
26 changes: 13 additions & 13 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ type (

// FuncOption is the argument of validating function.
FuncOption struct {
// Params represents the validation tag parameters.
// TagParams represents the validation tag parameters.
// e.g. len(1|2) -> []string{"1", "2"}
Params []string
TagParams []string

// v is a Validator instance that is validating.
v *Validator
Expand Down Expand Up @@ -288,8 +288,8 @@ func isHalfWidth(_ context.Context, f Field, _ FuncOption) (bool, error) {

func minLength(_ context.Context, f Field, opt FuncOption) (bool, error) {
var minStr string
if len(opt.Params) == 1 {
minStr = opt.Params[0]
if len(opt.TagParams) == 1 {
minStr = opt.TagParams[0]
} else {
return false, fmt.Errorf("invalid params len")
}
Expand Down Expand Up @@ -336,8 +336,8 @@ func minLength(_ context.Context, f Field, opt FuncOption) (bool, error) {

func maxLength(_ context.Context, f Field, opt FuncOption) (bool, error) {
var maxStr string
if len(opt.Params) == 1 {
maxStr = opt.Params[0]
if len(opt.TagParams) == 1 {
maxStr = opt.TagParams[0]
} else {
return false, fmt.Errorf("invalid params len")
}
Expand Down Expand Up @@ -383,10 +383,10 @@ func maxLength(_ context.Context, f Field, opt FuncOption) (bool, error) {
}

func eqLength(_ context.Context, f Field, opt FuncOption) (bool, error) {
if len(opt.Params) != 1 {
if len(opt.TagParams) != 1 {
return false, fmt.Errorf("invalid params len")
}
str := opt.Params[0]
str := opt.TagParams[0]

v := f.current
switch v.Kind() {
Expand Down Expand Up @@ -429,20 +429,20 @@ func eqLength(_ context.Context, f Field, opt FuncOption) (bool, error) {
}

func length(ctx context.Context, f Field, opt FuncOption) (bool, error) {
switch len(opt.Params) {
switch len(opt.TagParams) {
case 1:
eq, err := eqLength(ctx, f, FuncOption{Params: opt.Params})
eq, err := eqLength(ctx, f, FuncOption{TagParams: opt.TagParams})
if err != nil {
return false, err
}
return eq, nil

case 2:
min, err := minLength(ctx, f, FuncOption{Params: opt.Params[:1]})
min, err := minLength(ctx, f, FuncOption{TagParams: opt.TagParams[:1]})
if err != nil {
return false, err
}
max, err := maxLength(ctx, f, FuncOption{Params: opt.Params[1:]})
max, err := maxLength(ctx, f, FuncOption{TagParams: opt.TagParams[1:]})
if err != nil {
return false, err
}
Expand All @@ -453,7 +453,7 @@ func length(ctx context.Context, f Field, opt FuncOption) (bool, error) {
}

func or(_ context.Context, f Field, opt FuncOption) (bool, error) {
for _, rawTag := range opt.Params {
for _, rawTag := range opt.TagParams {
err := opt.v.ValidateVar(f.Interface(), rawTag)
if err == nil {
return true, nil
Expand Down
2 changes: 1 addition & 1 deletion validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (v *Validator) validate(ctx context.Context, field Field, chunk *tagChunk)

var errs Errors
for _, tag := range chunk.GetTags() {
valid, err := tag.validateFn(ctx, field, FuncOption{Params: tag.params, v: v})
valid, err := tag.validateFn(ctx, field, FuncOption{TagParams: tag.params, v: v})
if !valid || err != nil {
errs = append(errs, &fieldError{
field: field,
Expand Down

0 comments on commit 35b7e5c

Please sign in to comment.