Documentation ¶
Overview ¶
Validation utility
Copyright (C) 2014 Yohei Sasaki
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Index ¶
- func Eval(v Validator, t interface{}) error
- type BaseValidator
- type FieldValidationError
- type FormValidationTarget
- type FormValidator
- func (ov *FormValidator) BoolField(name string) *ValidationRegistrar
- func (ov *FormValidator) DateField(name string) *ValidationRegistrar
- func (ov *FormValidator) DateTimeField(name string) *ValidationRegistrar
- func (ov *FormValidator) Eval(t url.Values) error
- func (ov *FormValidator) GetValidationTarget(t interface{}) ValidationTarget
- func (ov *FormValidator) IntField(name string) *ValidationRegistrar
- type ObjectValidationTarget
- type ObjectValidator
- type ValidationError
- type ValidationFunc
- type ValidationRegistrar
- func (vr *ValidationRegistrar) Func(f func(v interface{}) *FieldValidationError) *ValidationRegistrar
- func (vr *ValidationRegistrar) Match(str string) *ValidationRegistrar
- func (vr *ValidationRegistrar) Max(n int) *ValidationRegistrar
- func (vr *ValidationRegistrar) Min(n int) *ValidationRegistrar
- func (vr *ValidationRegistrar) Required() *ValidationRegistrar
- func (vr *ValidationRegistrar) Unmatch(str string) *ValidationRegistrar
- type ValidationTarget
- type Validator
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type BaseValidator ¶
type BaseValidator struct { *ValidationRegistrar // object validators // contains filtered or unexported fields }
func (*BaseValidator) Field ¶
func (ov *BaseValidator) Field(name string) *ValidationRegistrar
func (*BaseValidator) FieldNames ¶
func (ov *BaseValidator) FieldNames() []string
func (*BaseValidator) Registrar ¶
func (ov *BaseValidator) Registrar() *ValidationRegistrar
type FieldValidationError ¶
type FieldValidationError struct { Message string `json:"message"` Properties map[string]interface{} `json:"properties,omitempty"` }
FieldValidationError represents an error in the validated object. It has a Message field which is a error message template, and Properties map which would be applied to the error message.
func NewFieldValidationError ¶
func NewFieldValidationError(message string, properties map[string]interface{}) *FieldValidationError
Create a new ValidationError object.
func (*FieldValidationError) Error ¶
func (v *FieldValidationError) Error() string
Returns string representation of the error.
func (*FieldValidationError) String ¶
func (v *FieldValidationError) String() string
Returns string representation of the error
type FormValidationTarget ¶
type FormValidationTarget struct {
// contains filtered or unexported fields
}
func (*FormValidationTarget) GetFieldValue ¶
func (ovt *FormValidationTarget) GetFieldValue(field string) interface{}
type FormValidator ¶
type FormValidator struct { *BaseValidator // contains filtered or unexported fields }
func NewFormValidator ¶
func NewFormValidator() *FormValidator
func (*FormValidator) BoolField ¶
func (ov *FormValidator) BoolField(name string) *ValidationRegistrar
func (*FormValidator) DateField ¶
func (ov *FormValidator) DateField(name string) *ValidationRegistrar
func (*FormValidator) DateTimeField ¶
func (ov *FormValidator) DateTimeField(name string) *ValidationRegistrar
func (*FormValidator) GetValidationTarget ¶
func (ov *FormValidator) GetValidationTarget(t interface{}) ValidationTarget
func (*FormValidator) IntField ¶
func (ov *FormValidator) IntField(name string) *ValidationRegistrar
type ObjectValidationTarget ¶
type ObjectValidationTarget struct {
// contains filtered or unexported fields
}
func (ObjectValidationTarget) GetFieldValue ¶
func (ovt ObjectValidationTarget) GetFieldValue(field string) interface{}
type ObjectValidator ¶
type ObjectValidator struct {
*BaseValidator
}
ObjectValidator is a utility to validate object.
Example:
ov := NewObjectValidator() ov.Field("Foo").Required().MaxLength(255) ov.Field("Bar").Match(regxp) ov.Eval(t) // returns ObjectValidationError on errors found during validation.
func NewObjectValidator ¶
func NewObjectValidator() *ObjectValidator
func (*ObjectValidator) Eval ¶
func (ov *ObjectValidator) Eval(t interface{}) error
func (*ObjectValidator) GetValidationTarget ¶
func (ov *ObjectValidator) GetValidationTarget(t interface{}) ValidationTarget
type ValidationError ¶
type ValidationError struct { Errors map[string][]*FieldValidationError Value interface{} `json:omit` }
ValidationError is an error object returned by object validations.
func (*ValidationError) Error ¶
func (ve *ValidationError) Error() string
func (*ValidationError) StatusCode ¶
func (ve *ValidationError) StatusCode() int
Returns 400 for HTTP status.
type ValidationFunc ¶
type ValidationFunc func(v interface{}) *FieldValidationError
ValidationFunc represents function to validate. `Fun` should be a function which returns true only when the validation is passed. `Message` should be a error message when validation is failed.
type ValidationRegistrar ¶
type ValidationRegistrar struct {
// contains filtered or unexported fields
}
func (*ValidationRegistrar) Func ¶
func (vr *ValidationRegistrar) Func(f func(v interface{}) *FieldValidationError) *ValidationRegistrar
func (*ValidationRegistrar) Match ¶
func (vr *ValidationRegistrar) Match(str string) *ValidationRegistrar
func (*ValidationRegistrar) Max ¶
func (vr *ValidationRegistrar) Max(n int) *ValidationRegistrar
func (*ValidationRegistrar) Min ¶
func (vr *ValidationRegistrar) Min(n int) *ValidationRegistrar
func (*ValidationRegistrar) Required ¶
func (vr *ValidationRegistrar) Required() *ValidationRegistrar
func (*ValidationRegistrar) Unmatch ¶
func (vr *ValidationRegistrar) Unmatch(str string) *ValidationRegistrar
type ValidationTarget ¶
type ValidationTarget interface {
GetFieldValue(field string) interface{}
}
Returns a value of the specified field which would be validated.
type Validator ¶
type Validator interface { // Returns a *ValidationRegistrar for this validator. Registrar() *ValidationRegistrar // Returns a *ValidationRegistrar for the specifi field. Field(name string) *ValidationRegistrar // Returns a valid field names for the validators. FieldNames() []string // Returns ValidationTarget object of t. GetValidationTarget(t interface{}) ValidationTarget }