Documentation
¶
Index ¶
- func GetFunctionName(i interface{}) string
- func HtmlEscape(s string) string
- func IsValidationError(err error) bool
- func RegisterFielder(key string, schema *Fielder) error
- func RetInvalidType(f *Fielder) error
- func RetInvalidValue(f *Fielder) error
- func RetMissing(f *Fielder) error
- func SetReflectValue(rcv reflect.Value, v reflect.Value) bool
- func SetRule(field string, rule *Rule)
- func UnmarshalValidate(dst, src any) error
- type Fielder
- type Rule
- type Schema
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetFunctionName ¶
func GetFunctionName(i interface{}) string
func HtmlEscape ¶
func IsValidationError ¶
func RegisterFielder ¶
func RetInvalidType ¶
func RetInvalidValue ¶
func RetMissing ¶
func SetReflectValue ¶
try convert and set a value of v in rcv
func SetRule ¶
ex:
c3po.SetRules("min",&c3po.Rule{Message:"min value: {value}"}) // '{value}' be replace by "Rule.Value"
c3po.SetRules("max",&c3po.Rule{Message:"max value: {value}"}) // '{value}' be replace by "Rule.Value"
c3po.SetRules("format",&c3po.Rule{Validate:func(value any) bool {...}})
type User struct {
Age int `c3po:"min=18"`
}
func UnmarshalValidate ¶
Types ¶
type Fielder ¶
type Fielder struct {
Name string
Type reflect.Kind
Tags map[string]string
Default any
Schema any
RealName string
Rules map[string]*Rule
IsMAP,
IsSlice,
IsStruct,
IsPointer bool
SliceType,
MapKeyType,
MapValueType *Fielder
SkipValidate bool // default: false
Walk bool // default: true -> deep validation
Recurcive bool // default: false -> for embed struct
Children map[string]*Fielder //
FieldsByIndex map[int]string //
SuperIndex *int // if a field to a struct
Required bool // default: false
Nullable bool // default: true
SkipError bool // default: false
OmitEmpty bool // default: false
}
func GetFielder ¶
func ParseSchema ¶
usage:
c3po.ParseSchema(struct{}) => struct{}
c3po.ParseSchema(&struct{}) => *struct{}
c3po.ParseSchema(&struct{Field:Value}) => *struct{Field: value} // with default value
type Schema struct{
Field `c3po:"-"` // omit this field
Field `c3po:"name"` // string: name of validation (default realName)
Field `c3po:"walk"` // bool: deep validation (default true)
Field `c3po:"escape"` // bool: escape html value (default false)
Field `c3po:"required"` // bool: ... (default false)
Field `c3po:"nullable"` // bool: if true, allow nil value (default true)
Field `c3po:"recursive"` // bool: for embbed data (default false)
Field `c3po:"skiperr"` // bool: omit on error (default false)
Field `c3po:"skip"` // bool: set value without validate (default false)
Field `c3po:"min=18"` // numbers only (int8, 16..., float32, ...)
Field `c3po:"max=65"` // numbers only (int8, 16..., float32, ...)
Field `c3po:"minlength=1"` // if a value can len, is valid. else skip
Field `c3po:"maxlength=100"` // if a value can len, is valid. else skip
}
func ParseSchemaWithTag ¶
type Rule ¶
type Schema ¶
func Validate ¶
use 'validate' in tags
type User struct{
Name string `validate:"minlen=4"`
Age int `validate:"min=18"`
}
sch := c3po.Validate(&User{},map[string]any{})
if sch.HasErrors(){
err := sch.Errors()
....
} else {
user := sch.Value().(*User)
}
func ValidateSchema ¶
type ValidationError ¶
func (ValidationError) Error ¶
func (v ValidationError) Error() string
Source Files
¶
Click to show internal directories.
Click to hide internal directories.