Documentation
¶
Index ¶
- Variables
- func GetMessage(key string) string
- func MustValidate(m Validator) bool
- type FieldError
- type Messages
- type MessagesChain
- type MessagesCollection
- type Model
- func (model *Model) AddError(field string, msg string)
- func (model *Model) AddErrorf(field string, msg string)
- func (model *Model) AddPlainError(field string, msg string)
- func (model *Model) Errors() []*FieldError
- func (model *Model) GetFieldLabel(field string) string
- func (model *Model) HasError() bool
- func (model *Model) ModelID() string
- func (model *Model) SetFieldLabels(labels map[string]string)
- func (model *Model) SetMessages(m *Messages)
- func (model *Model) SetModelID(id string)
- func (model *Model) Validate() error
- func (model *Model) ValidateField(validated bool, field string, msg string) *ValidatedResult
- func (model *Model) ValidateFieldf(validated bool, field string, msg string) *ValidatedResult
- type ValidatedResult
- type Validator
Constants ¶
This section is empty.
Variables ¶
var DefaultMessagesChain = NewMessagesChain()
DefaultMessagesChain default messages
var ErrModelNotInited = errors.New("model id is empty.You must use inited model . ")
ErrModelNotInited error raised when model id is empty.
var ErrNoValidateMethod = errors.New("error no validate method for model")
ErrNoValidateMethod error rasied when model validate method not overrided.
Functions ¶
func GetMessage ¶
GetMessage get translated string for key from default MessagesChain. Return key if translateed string not exist.
func MustValidate ¶
MustValidate return model validate result. Panic if any error raised.
Types ¶
type FieldError ¶
type FieldError struct {
//Field field name.
Field string
//Label field label.
//If field not found in model's labels,field label is same as field name.
Label string
//Msg error message
Msg string
}
FieldError field error info struct
type Messages ¶
Messages translate messages map.
func (*Messages) GetMessage ¶
GetMessage get translated string for key. Return key if translateed string not exist.
func (*Messages) LoadMessage ¶
LoadMessage check if translated string exists for given key. If string exists,return tranlasted string and true. If string does not exist,return key and false.
type MessagesChain ¶
type MessagesChain []MessagesCollection
MessagesChain use model messages interface list as model messages interface.
func NewMessagesChain ¶
func NewMessagesChain(Messages ...MessagesCollection) *MessagesChain
NewMessagesChain create new message chain with given model messages.
func Use ¶
func Use(Messages ...MessagesCollection) *MessagesChain
Use append new model messages to default MessagesChain.
func (*MessagesChain) GetMessage ¶
func (m *MessagesChain) GetMessage(key string) string
GetMessage get translated string for key. Return key if translateed string not exist. Check all model messages in order.
func (*MessagesChain) LoadMessage ¶
func (m *MessagesChain) LoadMessage(key string) (string, bool)
LoadMessage check if translated string exists for given key. If string exists,return tranlasted string and true. If string does not exist,return key and false. Check all model messages in order.
func (*MessagesChain) Use ¶
func (m *MessagesChain) Use(Messages ...MessagesCollection) *MessagesChain
Use append new model messages to MessagesChain.
type MessagesCollection ¶
type MessagesCollection interface {
//GetMessage get translated string for key.
//Return key if translateed string not exist.
GetMessage(key string) string
//LoadMessage check if translated string exists for given key.
//If string exists,return tranlasted string and true.
//If string does not exist,return key and false.
LoadMessage(key string) (string, bool)
}
MessagesCollection model messages collection interface.
type Model ¶
type Model struct {
// contains filtered or unexported fields
}
Model model struct.
func (*Model) AddErrorf ¶
AddErrorf add error by given field and formatted msg. Msg will be translated.
func (*Model) AddPlainError ¶
AddPlainError add plain error with given field and msg. Msg will not be translated.
func (*Model) GetFieldLabel ¶
GetFieldLabel get label by given label name. Return field name itself if not found in field labels of model.
func (*Model) SetFieldLabels ¶
SetFieldLabels set field labels to model
func (*Model) SetMessages ¶
SetMessages set translate messages to model. Messages will be used to translate msg field in AddPlainError,AddError,AddErrorf ,AddErrorf and ValidateFieldf method.
func (*Model) Validate ¶
Validate method used to validate model. Fail validation will add error to model. Return any error if rasied. You must override this method for your own model,otherwise ErrNoValidateMethod will be raised.
func (*Model) ValidateField ¶
func (model *Model) ValidateField(validated bool, field string, msg string) *ValidatedResult
ValidateField validated field then add error with given field name and plain msg if not validated.
func (*Model) ValidateFieldf ¶
func (model *Model) ValidateFieldf(validated bool, field string, msg string) *ValidatedResult
ValidateFieldf validated field then add error with given field name and formatted msg if not validated.
type ValidatedResult ¶
ValidatedResult result of validation.
type Validator ¶
type Validator interface {
//HasError return if model has any error.
HasError() bool
//Errors return error list of model
Errors() []*FieldError
//AddError add error by given field and plain msg.
AddError(field string, msg string)
//AddErrorf add error by given field and formatted msg.
AddErrorf(field string, msg string)
//Validate method used to validate model.
//Fail validation will add error to model.
//Return any error if rasied.
Validate() error
//ModelID return model id.
ModelID() string
//SetModelID set model id.
SetModelID(string)
}
Validator interface for model that can be validated.