Commit 278f8eb1 authored by slene's avatar slene

add ValidFormer interface, can custom valid

parent 658a671b
......@@ -7,6 +7,10 @@ import (
"strings"
)
type ValidFormer interface {
Valid(*Validation)
}
type ValidationError struct {
Message, Key, Name, Field, Tmpl string
Value interface{}
......@@ -21,9 +25,35 @@ func (e *ValidationError) String() string {
return e.Message
}
// A ValidationResult is returned from every validation method.
// It provides an indication of success, and a pointer to the Error (if any).
type ValidationResult struct {
Error *ValidationError
Ok bool
}
func (r *ValidationResult) Key(key string) *ValidationResult {
if r.Error != nil {
r.Error.Key = key
}
return r
}
func (r *ValidationResult) Message(message string, args ...interface{}) *ValidationResult {
if r.Error != nil {
if len(args) == 0 {
r.Error.Message = message
} else {
r.Error.Message = fmt.Sprintf(message, args...)
}
}
return r
}
// A Validation context manages data validation and error messages.
type Validation struct {
Errors []*ValidationError
Errors []*ValidationError
ErrorsMap map[string]*ValidationError
}
func (v *Validation) Clear() {
......@@ -38,13 +68,7 @@ func (v *Validation) HasErrors() bool {
// If there are multiple validation errors associated with a single key, the
// first one "wins". (Typically the first validation will be the more basic).
func (v *Validation) ErrorMap() map[string]*ValidationError {
m := map[string]*ValidationError{}
for _, e := range v.Errors {
if _, ok := m[e.Key]; !ok {
m[e.Key] = e
}
}
return m
return v.ErrorsMap
}
// Add an error to the validation context.
......@@ -57,31 +81,6 @@ func (v *Validation) Error(message string, args ...interface{}) *ValidationResul
return result
}
// A ValidationResult is returned from every validation method.
// It provides an indication of success, and a pointer to the Error (if any).
type ValidationResult struct {
Error *ValidationError
Ok bool
}
func (r *ValidationResult) Key(key string) *ValidationResult {
if r.Error != nil {
r.Error.Key = key
}
return r
}
func (r *ValidationResult) Message(message string, args ...interface{}) *ValidationResult {
if r.Error != nil {
if len(args) == 0 {
r.Error.Message = message
} else {
r.Error.Message = fmt.Sprintf(message, args...)
}
}
return r
}
// Test that the argument is non-nil and non-empty (if string or list)
func (v *Validation) Required(obj interface{}, key string) *ValidationResult {
return v.apply(Required{key}, obj)
......@@ -192,7 +191,7 @@ func (v *Validation) apply(chk Validator, obj interface{}) *ValidationResult {
Tmpl: MessageTmpls[Name],
LimitValue: chk.GetLimitValue(),
}
v.Errors = append(v.Errors, err)
v.setError(err)
// Also return it in the result.
return &ValidationResult{
......@@ -201,6 +200,22 @@ func (v *Validation) apply(chk Validator, obj interface{}) *ValidationResult {
}
}
func (v *Validation) setError(err *ValidationError) {
v.Errors = append(v.Errors, err)
if v.ErrorsMap == nil {
v.ErrorsMap = make(map[string]*ValidationError)
}
if _, ok := v.ErrorsMap[err.Field]; !ok {
v.ErrorsMap[err.Field] = err
}
}
func (v *Validation) SetError(fieldName string, errMsg string) *ValidationError {
err := &ValidationError{Key: fieldName, Field: fieldName, Tmpl: errMsg, Message: errMsg}
v.setError(err)
return err
}
// Apply a group of validators to a field, in order, and return the
// ValidationResult from the first one that fails, or the last one that
// succeeds.
......@@ -241,5 +256,12 @@ func (v *Validation) Valid(obj interface{}) (b bool, err error) {
}
}
}
if !v.HasErrors() {
if form, ok := obj.(ValidFormer); ok {
form.Valid(v)
}
}
return !v.HasErrors(), nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment