Documentation ¶
Index ¶
- Constants
- func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error)
- func ValidateBody(schema Schema, errorHandler func(http.ResponseWriter, *http.Request, error)) func(next http.Handler) http.Handler
- func ValidateJSON(dataRaw *[]byte, schema Schema) (dataMap map[string]interface{}, err error)
- func ValidateQuery(schema Schema, errorHandler func(http.ResponseWriter, *http.Request, error)) func(next http.Handler) http.Handler
- type AnySchema
- func (a *AnySchema) Default(value interface{}) *AnySchema
- func (a *AnySchema) Equal(value interface{}) *AnySchema
- func (a *AnySchema) Optional() *AnySchema
- func (a *AnySchema) PrependTransform(f func(*Context)) *AnySchema
- func (b *AnySchema) Priority() int
- func (a *AnySchema) Required() *AnySchema
- func (a *AnySchema) Set(value interface{}) *AnySchema
- func (a *AnySchema) SetPriority(priority int) *AnySchema
- func (a *AnySchema) Transform(f func(*Context)) *AnySchema
- func (a *AnySchema) Valid(values ...interface{}) *AnySchema
- func (a *AnySchema) Validate(ctx *Context)
- func (a *AnySchema) When(refPath string, condition interface{}, then Schema) *AnySchema
- type ArraySchema
- func (a *ArraySchema) Check(f func(interface{}) error) *ArraySchema
- func (a *ArraySchema) Default(value interface{}) *ArraySchema
- func (a *ArraySchema) Items(schemas ...Schema) *ArraySchema
- func (a *ArraySchema) Length(length int) *ArraySchema
- func (a *ArraySchema) Max(max int) *ArraySchema
- func (a *ArraySchema) Min(min int) *ArraySchema
- func (a *ArraySchema) Optional() *ArraySchema
- func (a *ArraySchema) PrependTransform(f func(*Context)) *ArraySchema
- func (b *ArraySchema) Priority() int
- func (a *ArraySchema) Required() *ArraySchema
- func (a *ArraySchema) SetPriority(priority int) *ArraySchema
- func (a *ArraySchema) Transform(f func(*Context)) *ArraySchema
- func (a *ArraySchema) Validate(ctx *Context)
- func (a *ArraySchema) When(refPath string, condition interface{}, then Schema) *ArraySchema
- type BoolSchema
- func (b *BoolSchema) Default(value bool) *BoolSchema
- func (b *BoolSchema) Equal(value bool) *BoolSchema
- func (b *BoolSchema) Falsy(values ...interface{}) *BoolSchema
- func (b *BoolSchema) Optional() *BoolSchema
- func (b *BoolSchema) PrependTransform(f func(*Context)) *BoolSchema
- func (b *BoolSchema) Priority() int
- func (b *BoolSchema) Required() *BoolSchema
- func (b *BoolSchema) Set(value bool) *BoolSchema
- func (b *BoolSchema) SetPriority(priority int) *BoolSchema
- func (b *BoolSchema) Transform(f func(*Context)) *BoolSchema
- func (b *BoolSchema) Truthy(values ...interface{}) *BoolSchema
- func (b *BoolSchema) Validate(ctx *Context)
- func (b *BoolSchema) When(refPath string, condition interface{}, then Schema) *BoolSchema
- type Context
- func (ctx *Context) Abort(err error)
- func (ctx *Context) AssertKind(kind reflect.Kind) bool
- func (ctx *Context) FieldPath() string
- func (ctx *Context) Get(name string) (interface{}, bool)
- func (ctx *Context) Ref(refPath string) (value interface{}, ok bool)
- func (ctx *Context) Set(name string, value interface{})
- func (ctx *Context) Skip()
- type K
- type NumberSchema
- func (n *NumberSchema) Ceil() *NumberSchema
- func (n *NumberSchema) Check(f func(float64) error) *NumberSchema
- func (n *NumberSchema) Convert(f func(float64) float64) *NumberSchema
- func (n *NumberSchema) Default(value float64) *NumberSchema
- func (n *NumberSchema) Equal(value float64) *NumberSchema
- func (n *NumberSchema) Floor() *NumberSchema
- func (n *NumberSchema) Integer() *NumberSchema
- func (n *NumberSchema) Max(max float64) *NumberSchema
- func (n *NumberSchema) Min(min float64) *NumberSchema
- func (n *NumberSchema) Optional() *NumberSchema
- func (n *NumberSchema) ParseString() *NumberSchema
- func (n *NumberSchema) PrependTransform(f func(*Context)) *NumberSchema
- func (b *NumberSchema) Priority() int
- func (n *NumberSchema) Required() *NumberSchema
- func (n *NumberSchema) Round() *NumberSchema
- func (n *NumberSchema) Set(value float64) *NumberSchema
- func (n *NumberSchema) SetPriority(priority int) *NumberSchema
- func (n *NumberSchema) Transform(f func(*Context)) *NumberSchema
- func (n *NumberSchema) Valid(values ...float64) *NumberSchema
- func (n *NumberSchema) Validate(ctx *Context)
- func (n *NumberSchema) When(refPath string, condition interface{}, then Schema) *NumberSchema
- type ObjectSchema
- func (o *ObjectSchema) Default(value map[string]interface{}) *ObjectSchema
- func (o *ObjectSchema) Keys(children K) *ObjectSchema
- func (o *ObjectSchema) Optional() *ObjectSchema
- func (o *ObjectSchema) PrependTransform(f func(*Context)) *ObjectSchema
- func (b *ObjectSchema) Priority() int
- func (o *ObjectSchema) Required() *ObjectSchema
- func (o *ObjectSchema) SetPriority(priority int) *ObjectSchema
- func (o *ObjectSchema) Transform(f func(*Context)) *ObjectSchema
- func (o *ObjectSchema) Validate(ctx *Context)
- func (o *ObjectSchema) When(refPath string, condition interface{}, then Schema) *ObjectSchema
- func (o *ObjectSchema) With(keys ...string) *ObjectSchema
- func (o *ObjectSchema) Without(keys ...string) *ObjectSchema
- type Schema
- type StringSchema
- func (s *StringSchema) Alphanum() *StringSchema
- func (s *StringSchema) Check(f func(string) error) *StringSchema
- func (s *StringSchema) Convert(f func(string) string) *StringSchema
- func (s *StringSchema) Default(value string) *StringSchema
- func (s *StringSchema) Equal(value string) *StringSchema
- func (s *StringSchema) Length(length int) *StringSchema
- func (s *StringSchema) Lowercase() *StringSchema
- func (s *StringSchema) Max(max int) *StringSchema
- func (s *StringSchema) Min(min int) *StringSchema
- func (s *StringSchema) Optional() *StringSchema
- func (s *StringSchema) PrependTransform(f func(*Context)) *StringSchema
- func (b *StringSchema) Priority() int
- func (s *StringSchema) Regex(regex string) *StringSchema
- func (s *StringSchema) Required() *StringSchema
- func (s *StringSchema) Set(value string) *StringSchema
- func (s *StringSchema) SetPriority(priority int) *StringSchema
- func (s *StringSchema) Token() *StringSchema
- func (s *StringSchema) Transform(f func(*Context)) *StringSchema
- func (s *StringSchema) Trim() *StringSchema
- func (s *StringSchema) Uppercase() *StringSchema
- func (s *StringSchema) Valid(values ...string) *StringSchema
- func (s *StringSchema) Validate(ctx *Context)
- func (s *StringSchema) When(refPath string, condition interface{}, then Schema) *StringSchema
Constants ¶
const ( // ContextKeyQuery save query map to context with this key ContextKeyQuery contextKey = iota // ContextKeyBody save body map to context with this key ContextKeyBody )
Variables ¶
This section is empty.
Functions ¶
func DefaultErrorHandler ¶
func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error)
DefaultErrorHandler handle and respond the error
func ValidateBody ¶
func ValidateBody(schema Schema, errorHandler func(http.ResponseWriter, *http.Request, error)) func(next http.Handler) http.Handler
ValidateBody validate the request's body using the schema. If the verification fails, the errorHandler will be used to handle the error.
func ValidateJSON ¶
ValidateJSON validate the provided json bytes using the schema.
Types ¶
type AnySchema ¶
type AnySchema struct {
// contains filtered or unexported fields
}
AnySchema match any data type
func (*AnySchema) Optional ¶
Optional mark a key as optional which will allow undefined or null as values. When the value of the key is undefined or null, the following check rule will be skip. Used to annotate the schema for readability as all keys are optional by default.
func (*AnySchema) PrependTransform ¶
PrependTransform run your transform function before othor rules.
func (*AnySchema) Required ¶
Required mark a key as required which will not allow undefined or null as value. All keys are optional by default.
func (*AnySchema) SetPriority ¶
SetPriority set priority to the schema. A schema with a higher priority under the same object will be validate first.
func (*AnySchema) Valid ¶
Valid add the provided values into the allowed whitelist and mark them as the only valid values allowed.
func (*AnySchema) When ¶
When add a conditional schema based on another key value The reference path support use `.` access object property, just like javascript. The condition can be a Schema or value. If condition is a schema, then this condition Schema will be used to verify the reference value. If condition is value, then check the condition is equal to the reference value. When the condition is true, the then schema will be applied to the current key value. Otherwise, nothing will be done.
type ArraySchema ¶
type ArraySchema struct {
// contains filtered or unexported fields
}
ArraySchema match array data type
func (*ArraySchema) Check ¶
func (a *ArraySchema) Check(f func(interface{}) error) *ArraySchema
Check use the provided function to validate the value of the key. Throws an error when the value is not a slice.
func (*ArraySchema) Default ¶
func (a *ArraySchema) Default(value interface{}) *ArraySchema
Default same as AnySchema.Default
func (*ArraySchema) Items ¶
func (a *ArraySchema) Items(schemas ...Schema) *ArraySchema
Items check if this value can pass the validation of any schema.
func (*ArraySchema) Length ¶
func (a *ArraySchema) Length(length int) *ArraySchema
Length check if the length of this slice is equal to the provided length.
func (*ArraySchema) Max ¶
func (a *ArraySchema) Max(max int) *ArraySchema
Max check if the length of this slice is less than or equal to the provided length.
func (*ArraySchema) Min ¶
func (a *ArraySchema) Min(min int) *ArraySchema
Min check if the length of this slice is greater than or equal to the provided length.
func (*ArraySchema) Optional ¶
func (a *ArraySchema) Optional() *ArraySchema
Optional same as AnySchema.Optional
func (*ArraySchema) PrependTransform ¶
func (a *ArraySchema) PrependTransform(f func(*Context)) *ArraySchema
PrependTransform same as AnySchema.PrependTransform
func (*ArraySchema) Required ¶
func (a *ArraySchema) Required() *ArraySchema
Required same as AnySchema.Required
func (*ArraySchema) SetPriority ¶
func (a *ArraySchema) SetPriority(priority int) *ArraySchema
SetPriority same as AnySchema.SetPriority
func (*ArraySchema) Transform ¶
func (a *ArraySchema) Transform(f func(*Context)) *ArraySchema
Transform same as AnySchema.Transform
func (*ArraySchema) Validate ¶
func (a *ArraySchema) Validate(ctx *Context)
Validate same as AnySchema.Validate
func (*ArraySchema) When ¶
func (a *ArraySchema) When(refPath string, condition interface{}, then Schema) *ArraySchema
When same as AnySchema.When
type BoolSchema ¶
type BoolSchema struct {
// contains filtered or unexported fields
}
BoolSchema match bool data type
func (*BoolSchema) Default ¶
func (b *BoolSchema) Default(value bool) *BoolSchema
Default same as AnySchema.Default
func (*BoolSchema) Equal ¶
func (b *BoolSchema) Equal(value bool) *BoolSchema
Equal same as AnySchema.Equal
func (*BoolSchema) Falsy ¶
func (b *BoolSchema) Falsy(values ...interface{}) *BoolSchema
Falsy allow for additional values to be considered valid booleans by converting them to false during validation.
func (*BoolSchema) Optional ¶
func (b *BoolSchema) Optional() *BoolSchema
Optional same as AnySchema.Optional
func (*BoolSchema) PrependTransform ¶
func (b *BoolSchema) PrependTransform(f func(*Context)) *BoolSchema
PrependTransform same as AnySchema.PrependTransform
func (*BoolSchema) Required ¶
func (b *BoolSchema) Required() *BoolSchema
Required same as AnySchema.Required
func (*BoolSchema) SetPriority ¶
func (b *BoolSchema) SetPriority(priority int) *BoolSchema
SetPriority same as AnySchema.SetPriority
func (*BoolSchema) Transform ¶
func (b *BoolSchema) Transform(f func(*Context)) *BoolSchema
Transform same as AnySchema.Transform
func (*BoolSchema) Truthy ¶
func (b *BoolSchema) Truthy(values ...interface{}) *BoolSchema
Truthy allow for additional values to be considered valid booleans by converting them to true during validation.
func (*BoolSchema) Validate ¶
func (b *BoolSchema) Validate(ctx *Context)
Validate same as AnySchema.Validate
func (*BoolSchema) When ¶
func (b *BoolSchema) When(refPath string, condition interface{}, then Schema) *BoolSchema
When same as AnySchema.When
type Context ¶
type Context struct { Value interface{} Err error // contains filtered or unexported fields }
Context contains data and toolkit
func NewContext ¶
func NewContext(data interface{}) *Context
NewContext Generates a context object with the provided data.
func (*Context) AssertKind ¶
AssertKind assert the value type and cache.
func (*Context) Ref ¶
Ref return the reference value. The reference path support use `.` access object property, just like javascript.
type NumberSchema ¶
type NumberSchema struct {
// contains filtered or unexported fields
}
NumberSchema match number data type
func Number ¶
func Number() *NumberSchema
Number Generates a schema object that matches number data type
func (*NumberSchema) Ceil ¶
func (n *NumberSchema) Ceil() *NumberSchema
Ceil convert the value to the least integer value greater than or equal to the value.
func (*NumberSchema) Check ¶
func (n *NumberSchema) Check(f func(float64) error) *NumberSchema
Check use the provided function to validate the value of the key. Throws an error when the value is not float64.
func (*NumberSchema) Convert ¶
func (n *NumberSchema) Convert(f func(float64) float64) *NumberSchema
Convert use the provided function to convert the value of the key. Throws an error when the value is not float64.
func (*NumberSchema) Default ¶
func (n *NumberSchema) Default(value float64) *NumberSchema
Default same as AnySchema.Default
func (*NumberSchema) Equal ¶
func (n *NumberSchema) Equal(value float64) *NumberSchema
Equal same as AnySchema.Equal
func (*NumberSchema) Floor ¶
func (n *NumberSchema) Floor() *NumberSchema
Floor convert the value to the greatest integer value less than or equal to the value.
func (*NumberSchema) Integer ¶
func (n *NumberSchema) Integer() *NumberSchema
Integer check if the value is integer.
func (*NumberSchema) Max ¶
func (n *NumberSchema) Max(max float64) *NumberSchema
Max check if the value is less than or equal to the provided value.
func (*NumberSchema) Min ¶
func (n *NumberSchema) Min(min float64) *NumberSchema
Min check if the value is greater than or equal to the provided value.
func (*NumberSchema) Optional ¶
func (n *NumberSchema) Optional() *NumberSchema
Optional same as AnySchema.Optional
func (*NumberSchema) ParseString ¶
func (n *NumberSchema) ParseString() *NumberSchema
ParseString convert the string value to float64. Validation will be skipped when this value is not string. But if this value is not a valid number, an error will be thrown.
func (*NumberSchema) PrependTransform ¶
func (n *NumberSchema) PrependTransform(f func(*Context)) *NumberSchema
PrependTransform same as AnySchema.PrependTransform
func (*NumberSchema) Required ¶
func (n *NumberSchema) Required() *NumberSchema
Required same as AnySchema.Required
func (*NumberSchema) Round ¶
func (n *NumberSchema) Round() *NumberSchema
Round convert the value to the nearest integer, rounding half away from zero.
func (*NumberSchema) Set ¶
func (n *NumberSchema) Set(value float64) *NumberSchema
Set same as AnySchema.Set
func (*NumberSchema) SetPriority ¶
func (n *NumberSchema) SetPriority(priority int) *NumberSchema
SetPriority same as AnySchema.SetPriority
func (*NumberSchema) Transform ¶
func (n *NumberSchema) Transform(f func(*Context)) *NumberSchema
Transform same as AnySchema.Transform
func (*NumberSchema) Valid ¶
func (n *NumberSchema) Valid(values ...float64) *NumberSchema
Valid same as AnySchema.Valid
func (*NumberSchema) Validate ¶
func (n *NumberSchema) Validate(ctx *Context)
Validate same as AnySchema.Validate
func (*NumberSchema) When ¶
func (n *NumberSchema) When(refPath string, condition interface{}, then Schema) *NumberSchema
When same as AnySchema.When
type ObjectSchema ¶
type ObjectSchema struct {
// contains filtered or unexported fields
}
ObjectSchema match object data type
func Object ¶
func Object() *ObjectSchema
Object Generates a schema object that matches object data type
func (*ObjectSchema) Default ¶
func (o *ObjectSchema) Default(value map[string]interface{}) *ObjectSchema
Default same as AnySchema.Default
func (*ObjectSchema) Keys ¶
func (o *ObjectSchema) Keys(children K) *ObjectSchema
Keys set the object keys's schema
func (*ObjectSchema) Optional ¶
func (o *ObjectSchema) Optional() *ObjectSchema
Optional same as AnySchema.Optional
func (*ObjectSchema) PrependTransform ¶
func (o *ObjectSchema) PrependTransform(f func(*Context)) *ObjectSchema
PrependTransform same as AnySchema.PrependTransform
func (*ObjectSchema) Required ¶
func (o *ObjectSchema) Required() *ObjectSchema
Required same as AnySchema.Required
func (*ObjectSchema) SetPriority ¶
func (o *ObjectSchema) SetPriority(priority int) *ObjectSchema
SetPriority same as AnySchema.SetPriority
func (*ObjectSchema) Transform ¶
func (o *ObjectSchema) Transform(f func(*Context)) *ObjectSchema
Transform same as AnySchema.Transform
func (*ObjectSchema) Validate ¶
func (o *ObjectSchema) Validate(ctx *Context)
Validate same as AnySchema.Validate
func (*ObjectSchema) When ¶
func (o *ObjectSchema) When(refPath string, condition interface{}, then Schema) *ObjectSchema
When same as AnySchema.When
func (*ObjectSchema) With ¶
func (o *ObjectSchema) With(keys ...string) *ObjectSchema
With require the presence of these keys.
func (*ObjectSchema) Without ¶
func (o *ObjectSchema) Without(keys ...string) *ObjectSchema
Without forbids the presence of these keys.
type StringSchema ¶
type StringSchema struct {
// contains filtered or unexported fields
}
StringSchema match string data type
func String ¶
func String() *StringSchema
String Generates a schema object that matches string data type
func (*StringSchema) Alphanum ¶
func (s *StringSchema) Alphanum() *StringSchema
Alphanum check if the string value to only contain a-z, A-Z, and 0-9
func (*StringSchema) Check ¶
func (s *StringSchema) Check(f func(string) error) *StringSchema
Check use the provided function to validate the value of the key. Throws an error when the value is not string.
func (*StringSchema) Convert ¶
func (s *StringSchema) Convert(f func(string) string) *StringSchema
Convert use the provided function to convert the value of the key. Throws an error when the value is not string.
func (*StringSchema) Default ¶
func (s *StringSchema) Default(value string) *StringSchema
Default same as AnySchema.Default
func (*StringSchema) Equal ¶
func (s *StringSchema) Equal(value string) *StringSchema
Equal same as AnySchema.Equal
func (*StringSchema) Length ¶
func (s *StringSchema) Length(length int) *StringSchema
Length check if the length of this string is equal to the provided length.
func (*StringSchema) Lowercase ¶
func (s *StringSchema) Lowercase() *StringSchema
Lowercase convert the string value to lowercase.
func (*StringSchema) Max ¶
func (s *StringSchema) Max(max int) *StringSchema
Max check if the length of this string is less than or equal to the provided length.
func (*StringSchema) Min ¶
func (s *StringSchema) Min(min int) *StringSchema
Min check if the length of this string is greater than or equal to the provided length.
func (*StringSchema) Optional ¶
func (s *StringSchema) Optional() *StringSchema
Optional same as AnySchema.Optional
func (*StringSchema) PrependTransform ¶
func (s *StringSchema) PrependTransform(f func(*Context)) *StringSchema
PrependTransform same as AnySchema.PrependTransform
func (*StringSchema) Regex ¶
func (s *StringSchema) Regex(regex string) *StringSchema
Regex check if the value is matched the regex.
func (*StringSchema) Required ¶
func (s *StringSchema) Required() *StringSchema
Required same as AnySchema.Required
func (*StringSchema) Set ¶
func (s *StringSchema) Set(value string) *StringSchema
Set same as AnySchema.Set
func (*StringSchema) SetPriority ¶
func (s *StringSchema) SetPriority(priority int) *StringSchema
SetPriority same as AnySchema.SetPriority
func (*StringSchema) Token ¶
func (s *StringSchema) Token() *StringSchema
Token check if the string value to only contain a-z, A-Z, 0-9, and underscore _
func (*StringSchema) Transform ¶
func (s *StringSchema) Transform(f func(*Context)) *StringSchema
Transform same as AnySchema.Transform
func (*StringSchema) Trim ¶
func (s *StringSchema) Trim() *StringSchema
Trim emoves whitespace from both sides of the string value.
func (*StringSchema) Uppercase ¶
func (s *StringSchema) Uppercase() *StringSchema
Uppercase convert the string value to uppercase.
func (*StringSchema) Valid ¶
func (s *StringSchema) Valid(values ...string) *StringSchema
Valid same as AnySchema.Valid
func (*StringSchema) Validate ¶
func (s *StringSchema) Validate(ctx *Context)
Validate same as AnySchema.Validate
func (*StringSchema) When ¶
func (s *StringSchema) When(refPath string, condition interface{}, then Schema) *StringSchema
When same as AnySchema.When