validation

package
v0.0.0-...-f26991d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 25, 2015 License: MIT Imports: 8 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Eval

func Eval(v Validator, t interface{}) error

Evaluate the object t and return the validation result. nil would be returned on no validation errors.

Types

type BaseValidator

type BaseValidator struct {
	*ValidationRegistrar // object validators
	// contains filtered or unexported fields
}

func NewBaseValidator

func NewBaseValidator() *BaseValidator

Create a new validator for t object.

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) Eval

func (ov *FormValidator) Eval(t url.Values) error

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 (*ValidationRegistrar) Max

func (*ValidationRegistrar) Min

func (*ValidationRegistrar) Required

func (vr *ValidationRegistrar) Required() *ValidationRegistrar

func (*ValidationRegistrar) Unmatch

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
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL