Permalink
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
49 lines (44 sloc) 1.28 KB
// Copyright 2018 The SQLer Authors. All rights reserved.
// Use of this source code is governed by a Apache 2.0
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"strings"
"github.com/asaskevich/govalidator"
)
// Validate - validates the specified data against the specified validators
func Validate(data map[string]interface{}, validators map[string][]string) map[string][]string {
invalid, result := 0, map[string][]string{}
for k, rules := range validators {
result[k] = []string{}
value, exists := data[k]
valuestr := strings.TrimSpace(fmt.Sprintf("%v", value))
for _, r := range rules {
if r == "required" && !exists || valuestr == "" {
invalid++
result[k] = append(result[k], "required")
} else if ruler, ok := govalidator.TagMap[r]; ok && !ruler(valuestr) {
invalid++
result[k] = append(result[k], r)
} else {
parts := strings.SplitN(r, ":", 2)
if len(parts) < 2 {
parts = append(parts, "")
}
r, args := parts[0], parts[1]
args = strings.TrimSpace(args)
if ruler, ok := govalidator.ParamTagMap[r]; ok {
if !ruler(valuestr, strings.Split(args, ",")...) {
invalid++
result[k] = append(result[k], r)
}
}
}
}
if len(result[k]) < 1 {
delete(result, k)
}
}
return result
}