Skip to content

Commit

Permalink
update example and fixed returning err not found
Browse files Browse the repository at this point in the history
  • Loading branch information
restuwahyu13 committed Apr 29, 2021
1 parent 71e0346 commit ff54989
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
18 changes: 9 additions & 9 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewBindValidator(validator validators) *binding {
return &binding{validator: validator}
}

func (b *binding) BindValidator(s interface{}, config []ErrorMetaConfig) *errorResponse {
func (b *binding) BindValidator(s interface{}, config []ErrorMetaConfig) (*errorResponse, int) {

mergeObject := make(map[int]interface{})

Expand All @@ -32,16 +32,16 @@ func (b *binding) BindValidator(s interface{}, config []ErrorMetaConfig) *errorR

var errors errorResponse
validate := b.validator.validator(s, mergeObject)

errDataCollection := make(map[string][]map[string]interface{})

for i, v := range validate {
errorResults := make(map[string]interface{})
errorResults[i] = v
errDataCollection["errors"] = append(errDataCollection["errors"], errorResults)
if len(validate) > 0 {
for i, v := range validate {
errorResults := make(map[string]interface{})
errorResults[i] = v
errDataCollection["errors"] = append(errDataCollection["errors"], errorResults)
}
errors.Results = errDataCollection
}

errors.Results = errDataCollection

return &errors
return &errors , len(validate)
}
6 changes: 4 additions & 2 deletions example/gin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ func main() {
},
}

errResponse := bind.BindValidator(&input, config.Options)
errResponse, errCount := bind.BindValidator(&input, config.Options)

if errResponse != nil {
if errCount > 0 {
ctx.JSON(http.StatusBadRequest, errResponse)
ctx.AbortWithStatus(http.StatusBadRequest)
return
} else {
ctx.JSON(http.StatusBadRequest, gin.H{"message": "register new account successfully"})
}
Expand Down
9 changes: 7 additions & 2 deletions example/go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ func main() {
},
}

errResponse := bind.BindValidator(&input, config.Options)
fmt.Println(errResponse)
errResponse, errCount := bind.BindValidator(&input, config.Options)

if errCount > 0 {
fmt.Println(errResponse)
} else {
fmt.Println("not error found")
}
}

0 comments on commit ff54989

Please sign in to comment.