jio: github.com/faceair/jio Index | Files

package jio

import "github.com/faceair/jio"

Index

Package Files

any.go array.go bool.go context.go jio.go number.go object.go schema.go string.go

Constants

const (
    // ContextKeyQuery save query map to context with this key
    ContextKeyQuery contextKey = iota
    // ContextKeyBody save body map to context with this key
    ContextKeyBody
)

func DefaultErrorHandler Uses

func DefaultErrorHandler(w http.ResponseWriter, r *http.Request, err error)

DefaultErrorHandler handle and respond the error

func ValidateBody Uses

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 Uses

func ValidateJSON(dataRaw *[]byte, schema Schema) (dataMap map[string]interface{}, err error)

ValidateJSON validate the provided json bytes using the schema.

func ValidateQuery Uses

func ValidateQuery(schema Schema, errorHandler func(http.ResponseWriter, *http.Request, error)) func(next http.Handler) http.Handler

ValidateQuery validate the request's query using the schema.

type AnySchema Uses

type AnySchema struct {
    // contains filtered or unexported fields
}

AnySchema match any data type

func Any Uses

func Any() *AnySchema

Any Generates a schema object that matches any data type

func (*AnySchema) Default Uses

func (a *AnySchema) Default(value interface{}) *AnySchema

Default set a default value if the original value is undefined or null.

func (*AnySchema) Equal Uses

func (a *AnySchema) Equal(value interface{}) *AnySchema

Equal check the provided value is equal to the value of the key.

func (*AnySchema) Optional Uses

func (a *AnySchema) Optional() *AnySchema

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 Uses

func (a *AnySchema) PrependTransform(f func(*Context)) *AnySchema

PrependTransform run your transform function before othor rules.

func (*AnySchema) Priority Uses

func (b *AnySchema) Priority() int

func (*AnySchema) Required Uses

func (a *AnySchema) Required() *AnySchema

Required mark a key as required which will not allow undefined or null as value. All keys are optional by default.

func (*AnySchema) Set Uses

func (a *AnySchema) Set(value interface{}) *AnySchema

Set just set a value for the key and don't care the origin value.

func (*AnySchema) SetPriority Uses

func (a *AnySchema) SetPriority(priority int) *AnySchema

SetPriority set priority to the schema. A schema with a higher priority under the same object will be validate first.

func (*AnySchema) Transform Uses

func (a *AnySchema) Transform(f func(*Context)) *AnySchema

Transform append your transform function to rules.

func (*AnySchema) Valid Uses

func (a *AnySchema) Valid(values ...interface{}) *AnySchema

Valid add the provided values into the allowed whitelist and mark them as the only valid values allowed.

func (*AnySchema) Validate Uses

func (a *AnySchema) Validate(ctx *Context)

Validate a value using the schema

func (*AnySchema) When Uses

func (a *AnySchema) When(refPath string, condition interface{}, then Schema) *AnySchema

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 Uses

type ArraySchema struct {
    // contains filtered or unexported fields
}

ArraySchema match array data type

func Array Uses

func Array() *ArraySchema

Array Generates a schema object that matches array data type

func (*ArraySchema) Check Uses

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 Uses

func (a *ArraySchema) Default(value interface{}) *ArraySchema

Default same as AnySchema.Default

func (*ArraySchema) Items Uses

func (a *ArraySchema) Items(schemas ...Schema) *ArraySchema

Items check if this value can pass the validation of any schema.

func (*ArraySchema) Length Uses

func (a *ArraySchema) Length(length int) *ArraySchema

Length check if the length of this slice is equal to the provided length.

func (*ArraySchema) Max Uses

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 Uses

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 Uses

func (a *ArraySchema) Optional() *ArraySchema

Optional same as AnySchema.Optional

func (*ArraySchema) PrependTransform Uses

func (a *ArraySchema) PrependTransform(f func(*Context)) *ArraySchema

PrependTransform same as AnySchema.PrependTransform

func (*ArraySchema) Priority Uses

func (b *ArraySchema) Priority() int

func (*ArraySchema) Required Uses

func (a *ArraySchema) Required() *ArraySchema

Required same as AnySchema.Required

func (*ArraySchema) SetPriority Uses

func (a *ArraySchema) SetPriority(priority int) *ArraySchema

SetPriority same as AnySchema.SetPriority

func (*ArraySchema) Transform Uses

func (a *ArraySchema) Transform(f func(*Context)) *ArraySchema

Transform same as AnySchema.Transform

func (*ArraySchema) Validate Uses

func (a *ArraySchema) Validate(ctx *Context)

Validate same as AnySchema.Validate

func (*ArraySchema) When Uses

func (a *ArraySchema) When(refPath string, condition interface{}, then Schema) *ArraySchema

When same as AnySchema.When

type BoolSchema Uses

type BoolSchema struct {
    // contains filtered or unexported fields
}

BoolSchema match bool data type

func Bool Uses

func Bool() *BoolSchema

Bool Generates a schema object that matches bool data type

func (*BoolSchema) Default Uses

func (b *BoolSchema) Default(value bool) *BoolSchema

Default same as AnySchema.Default

func (*BoolSchema) Equal Uses

func (b *BoolSchema) Equal(value bool) *BoolSchema

Equal same as AnySchema.Equal

func (*BoolSchema) Falsy Uses

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 Uses

func (b *BoolSchema) Optional() *BoolSchema

Optional same as AnySchema.Optional

func (*BoolSchema) PrependTransform Uses

func (b *BoolSchema) PrependTransform(f func(*Context)) *BoolSchema

PrependTransform same as AnySchema.PrependTransform

func (*BoolSchema) Priority Uses

func (b *BoolSchema) Priority() int

func (*BoolSchema) Required Uses

func (b *BoolSchema) Required() *BoolSchema

Required same as AnySchema.Required

func (*BoolSchema) Set Uses

func (b *BoolSchema) Set(value bool) *BoolSchema

Set same as AnySchema.Set

func (*BoolSchema) SetPriority Uses

func (b *BoolSchema) SetPriority(priority int) *BoolSchema

SetPriority same as AnySchema.SetPriority

func (*BoolSchema) Transform Uses

func (b *BoolSchema) Transform(f func(*Context)) *BoolSchema

Transform same as AnySchema.Transform

func (*BoolSchema) Truthy Uses

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 Uses

func (b *BoolSchema) Validate(ctx *Context)

Validate same as AnySchema.Validate

func (*BoolSchema) When Uses

func (b *BoolSchema) When(refPath string, condition interface{}, then Schema) *BoolSchema

When same as AnySchema.When

type Context Uses

type Context struct {
    Value interface{}
    Err   error
    // contains filtered or unexported fields
}

Context contains data and toolkit

func NewContext Uses

func NewContext(data interface{}) *Context

NewContext Generates a context object with the provided data.

func (*Context) Abort Uses

func (ctx *Context) Abort(err error)

Abort throw an error and skip the following check rules.

func (*Context) AssertKind Uses

func (ctx *Context) AssertKind(kind reflect.Kind) bool

AssertKind assert the value type and cache.

func (*Context) FieldPath Uses

func (ctx *Context) FieldPath() string

FieldPath the field path of the current value.

func (*Context) Get Uses

func (ctx *Context) Get(name string) (interface{}, bool)

Get returns the value for the given key, ie: (value, true).

func (*Context) Ref Uses

func (ctx *Context) Ref(refPath string) (value interface{}, ok bool)

Ref return the reference value. The reference path support use `.` access object property, just like javascript.

func (*Context) Set Uses

func (ctx *Context) Set(name string, value interface{})

Set is used to store a new key/value pair exclusively for this context.

func (*Context) Skip Uses

func (ctx *Context) Skip()

Skip the following check rules.

type K Uses

type K map[string]Schema

K object keys schema alias

type NumberSchema Uses

type NumberSchema struct {
    // contains filtered or unexported fields
}

NumberSchema match number data type

func Number Uses

func Number() *NumberSchema

Number Generates a schema object that matches number data type

func (*NumberSchema) Ceil Uses

func (n *NumberSchema) Ceil() *NumberSchema

Ceil convert the value to the least integer value greater than or equal to the value.

func (*NumberSchema) Check Uses

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 Uses

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 Uses

func (n *NumberSchema) Default(value float64) *NumberSchema

Default same as AnySchema.Default

func (*NumberSchema) Equal Uses

func (n *NumberSchema) Equal(value float64) *NumberSchema

Equal same as AnySchema.Equal

func (*NumberSchema) Floor Uses

func (n *NumberSchema) Floor() *NumberSchema

Floor convert the value to the greatest integer value less than or equal to the value.

func (*NumberSchema) Integer Uses

func (n *NumberSchema) Integer() *NumberSchema

Integer check if the value is integer.

func (*NumberSchema) Max Uses

func (n *NumberSchema) Max(max float64) *NumberSchema

Max check if the value is less than or equal to the provided value.

func (*NumberSchema) Min Uses

func (n *NumberSchema) Min(min float64) *NumberSchema

Min check if the value is greater than or equal to the provided value.

func (*NumberSchema) Optional Uses

func (n *NumberSchema) Optional() *NumberSchema

Optional same as AnySchema.Optional

func (*NumberSchema) ParseString Uses

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 Uses

func (n *NumberSchema) PrependTransform(f func(*Context)) *NumberSchema

PrependTransform same as AnySchema.PrependTransform

func (*NumberSchema) Priority Uses

func (b *NumberSchema) Priority() int

func (*NumberSchema) Required Uses

func (n *NumberSchema) Required() *NumberSchema

Required same as AnySchema.Required

func (*NumberSchema) Round Uses

func (n *NumberSchema) Round() *NumberSchema

Round convert the value to the nearest integer, rounding half away from zero.

func (*NumberSchema) Set Uses

func (n *NumberSchema) Set(value float64) *NumberSchema

Set same as AnySchema.Set

func (*NumberSchema) SetPriority Uses

func (n *NumberSchema) SetPriority(priority int) *NumberSchema

SetPriority same as AnySchema.SetPriority

func (*NumberSchema) Transform Uses

func (n *NumberSchema) Transform(f func(*Context)) *NumberSchema

Transform same as AnySchema.Transform

func (*NumberSchema) Valid Uses

func (n *NumberSchema) Valid(values ...float64) *NumberSchema

Valid same as AnySchema.Valid

func (*NumberSchema) Validate Uses

func (n *NumberSchema) Validate(ctx *Context)

Validate same as AnySchema.Validate

func (*NumberSchema) When Uses

func (n *NumberSchema) When(refPath string, condition interface{}, then Schema) *NumberSchema

When same as AnySchema.When

type ObjectSchema Uses

type ObjectSchema struct {
    // contains filtered or unexported fields
}

ObjectSchema match object data type

func Object Uses

func Object() *ObjectSchema

Object Generates a schema object that matches object data type

func (*ObjectSchema) Default Uses

func (o *ObjectSchema) Default(value map[string]interface{}) *ObjectSchema

Default same as AnySchema.Default

func (*ObjectSchema) Keys Uses

func (o *ObjectSchema) Keys(children K) *ObjectSchema

Keys set the object keys's schema

func (*ObjectSchema) Optional Uses

func (o *ObjectSchema) Optional() *ObjectSchema

Optional same as AnySchema.Optional

func (*ObjectSchema) PrependTransform Uses

func (o *ObjectSchema) PrependTransform(f func(*Context)) *ObjectSchema

PrependTransform same as AnySchema.PrependTransform

func (*ObjectSchema) Priority Uses

func (b *ObjectSchema) Priority() int

func (*ObjectSchema) Required Uses

func (o *ObjectSchema) Required() *ObjectSchema

Required same as AnySchema.Required

func (*ObjectSchema) SetPriority Uses

func (o *ObjectSchema) SetPriority(priority int) *ObjectSchema

SetPriority same as AnySchema.SetPriority

func (*ObjectSchema) Transform Uses

func (o *ObjectSchema) Transform(f func(*Context)) *ObjectSchema

Transform same as AnySchema.Transform

func (*ObjectSchema) Validate Uses

func (o *ObjectSchema) Validate(ctx *Context)

Validate same as AnySchema.Validate

func (*ObjectSchema) When Uses

func (o *ObjectSchema) When(refPath string, condition interface{}, then Schema) *ObjectSchema

When same as AnySchema.When

func (*ObjectSchema) With Uses

func (o *ObjectSchema) With(keys ...string) *ObjectSchema

With require the presence of these keys.

func (*ObjectSchema) Without Uses

func (o *ObjectSchema) Without(keys ...string) *ObjectSchema

Without forbids the presence of these keys.

type Schema Uses

type Schema interface {
    Priority() int
    Validate(*Context)
}

Schema interface

type StringSchema Uses

type StringSchema struct {
    // contains filtered or unexported fields
}

StringSchema match string data type

func String Uses

func String() *StringSchema

String Generates a schema object that matches string data type

func (*StringSchema) Alphanum Uses

func (s *StringSchema) Alphanum() *StringSchema

Alphanum check if the string value to only contain a-z, A-Z, and 0-9

func (*StringSchema) Check Uses

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 Uses

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 Uses

func (s *StringSchema) Default(value string) *StringSchema

Default same as AnySchema.Default

func (*StringSchema) Equal Uses

func (s *StringSchema) Equal(value string) *StringSchema

Equal same as AnySchema.Equal

func (*StringSchema) Length Uses

func (s *StringSchema) Length(length int) *StringSchema

Length check if the length of this string is equal to the provided length.

func (*StringSchema) Lowercase Uses

func (s *StringSchema) Lowercase() *StringSchema

Lowercase convert the string value to lowercase.

func (*StringSchema) Max Uses

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 Uses

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 Uses

func (s *StringSchema) Optional() *StringSchema

Optional same as AnySchema.Optional

func (*StringSchema) PrependTransform Uses

func (s *StringSchema) PrependTransform(f func(*Context)) *StringSchema

PrependTransform same as AnySchema.PrependTransform

func (*StringSchema) Priority Uses

func (b *StringSchema) Priority() int

func (*StringSchema) Regex Uses

func (s *StringSchema) Regex(regex string) *StringSchema

Regex check if the value is matched the regex.

func (*StringSchema) Required Uses

func (s *StringSchema) Required() *StringSchema

Required same as AnySchema.Required

func (*StringSchema) Set Uses

func (s *StringSchema) Set(value string) *StringSchema

Set same as AnySchema.Set

func (*StringSchema) SetPriority Uses

func (s *StringSchema) SetPriority(priority int) *StringSchema

SetPriority same as AnySchema.SetPriority

func (*StringSchema) Token Uses

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 Uses

func (s *StringSchema) Transform(f func(*Context)) *StringSchema

Transform same as AnySchema.Transform

func (*StringSchema) Trim Uses

func (s *StringSchema) Trim() *StringSchema

Trim emoves whitespace from both sides of the string value.

func (*StringSchema) Uppercase Uses

func (s *StringSchema) Uppercase() *StringSchema

Uppercase convert the string value to uppercase.

func (*StringSchema) Valid Uses

func (s *StringSchema) Valid(values ...string) *StringSchema

Valid same as AnySchema.Valid

func (*StringSchema) Validate Uses

func (s *StringSchema) Validate(ctx *Context)

Validate same as AnySchema.Validate

func (*StringSchema) When Uses

func (s *StringSchema) When(refPath string, condition interface{}, then Schema) *StringSchema

When same as AnySchema.When

Package jio imports 13 packages (graph). Updated 2020-05-08. Refresh now. Tools for package owners.