jamieomatthews / validation Goto Github PK
View Code? Open in Web Editor NEWPerform Data Validation For Structs in Go
Perform Data Validation For Structs in Go
This code:
func (ur UserRegistration) Validation(errors binding.Errors, req *http.Request) binding.Errors {
v := validation.NewValidation(errors, ur)
v.Validate(&ur.Name).Key("fullname").MaxLength(20)
v.Validate(&ur.Email).Default("Custom Email Validation Message").Classify("email-class").Email()
v.Validate(&ur.PlainPassword).TrimSpace().MinLength(6)
return *v.Errors.(*binding.Errors)
}
produce some errors:
cannot use errors (type binding.Errors) as type validation.Errors in function argument:
binding.Errors does not implement validation.Errors (Add method has pointer receiver)
v.Validate(&ur.Email).Default undefined (type *validation.Set has no field or method Default)
Validation.getKeyForField
compare v.Obj
's filed value to passedField value to figure out the key. If two fields have the same value, it picks the first matched field and may return an incorrect key.
package main
import (
"fmt"
//"github.com/jamieomatthews/validation"
"github.com/sillyousu/validation"
)
type User struct {
Name string `form:"username"`
NickName string `form:"nickname"`
}
func main() {
user := &User{Name: "su21", NickName: "su21"}
//I want to get an empty `validation.Errors{}`
errors := validation.NewErrors()
v := validation.New(errors, user)
v.Validate(user.NickName).MinLength(5)
fmt.Println("============")
for i := 0; i < v.Errors.Len(); i++ {
err := v.Errors.At(i)
fmt.Printf("\nerr: %#v\n", err)
}
}
Result
TypeObj: *main.User
Field: su21 passedField: su21
Is Equal!!
Got Key? username
Min Length: ' su21 '
============
err: &validation.error{fields:[]string{"username"}, classification:"", message:"Minimum Length is 5"}
I think v.Validate("FieldName")
is more reasonable.
I can't seem to use this with the given example and binding:
cannot use errors (type binding.Errors) as type validation.Errors in function argument:
binding.Errors does not implement validation.Errors (missing Add method)
impossible type assertion:
*binding.Errors does not implement validation.Errors (missing Add method)
It's the latest binding (from today)
Also what is the interface part in NewValidation?
Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.