Documentation ¶
Overview ¶
Package changeset used to cast and validate data before saving it to the database.
Package changeset used to cast and validate data before saving it to the database.
Index ¶
- Variables
- func AddError(ch *Changeset, field string, message string)
- func ApplyString(ch *Changeset, field string, fn func(string) string)
- func CastAssoc(ch *Changeset, field string, fn ChangeFunc, opts ...Option)
- func CheckConstraint(ch *Changeset, field string, opts ...Option)
- func DeleteChange(ch *Changeset, field string)
- func EscapeString(ch *Changeset, fields ...string)
- func ForeignKeyConstraint(ch *Changeset, field string, opts ...Option)
- func PutAssoc(ch *Changeset, field string, value interface{}, opts ...Option)
- func PutChange(ch *Changeset, field string, value interface{}, opts ...Option)
- func PutDefault(ch *Changeset, field string, value interface{}, opts ...Option)
- func UnescapeString(ch *Changeset, field string)
- func UniqueConstraint(ch *Changeset, field string, opts ...Option)
- func ValidateExclusion(ch *Changeset, field string, values []interface{}, opts ...Option)
- func ValidateInclusion(ch *Changeset, field string, values []interface{}, opts ...Option)
- func ValidateMax(ch *Changeset, field string, max int, opts ...Option)
- func ValidateMin(ch *Changeset, field string, min int, opts ...Option)
- func ValidatePattern(ch *Changeset, field string, pattern string, opts ...Option)
- func ValidateRange(ch *Changeset, field string, min int, max int, opts ...Option)
- func ValidateRegexp(ch *Changeset, field string, exp *regexp.Regexp, opts ...Option)
- func ValidateRequired(ch *Changeset, fields []string, opts ...Option)
- type ChangeFunc
- type Changeset
- func (c *Changeset) Apply(doc *rel.Document, mut *rel.Mutation)
- func (c Changeset) Changes() map[string]interface{}
- func (c Changeset) Constraints() Constraints
- func (c Changeset) Error() error
- func (c Changeset) Errors() []error
- func (c Changeset) Fetch(field string) interface{}
- func (c Changeset) Get(field string) interface{}
- func (c Changeset) Types() map[string]reflect.Type
- func (c Changeset) Values() map[string]interface{}
- type Constraint
- type Constraints
- type Error
- type Option
- type Options
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var CastAssocErrorMessage = "{field} is invalid"
CastAssocErrorMessage is the default error message for CastAssoc when its invalid.
var CastAssocRequiredMessage = "{field} is required"
CastAssocRequiredMessage is the default error message for CastAssoc when its missing.
var CastErrorMessage = "{field} is invalid"
CastErrorMessage is the default error message for Cast.
var CheckConstraintMessage = "{field} is invalid"
CheckConstraintMessage is the default error message for CheckConstraint.
var ForeignKeyConstraintMessage = "does not exist"
ForeignKeyConstraintMessage is the default error message for ForeignKeyConstraint.
var PutAssocErrorMessage = "{field} is invalid"
PutAssocErrorMessage is the default error message for PutAssoc.
var PutChangeErrorMessage = "{field} is invalid"
PutChangeErrorMessage is the default error message for PutChange.
var PutDefaultErrorMessage = "{field} is invalid"
PutDefaultErrorMessage is the default error message for PutDefault.
var UniqueConstraintMessage = "{field} has already been taken"
UniqueConstraintMessage is the default error message for UniqueConstraint.
var ValidateExclusionErrorMessage = "{field} must not be any of {values}"
ValidateExclusionErrorMessage is the default error message for ValidateExclusion.
var ValidateInclusionErrorMessage = "{field} must be one of {values}"
ValidateInclusionErrorMessage is the default error message for ValidateInclusion.
var ValidateMaxErrorMessage = "{field} must be less than {max}"
ValidateMaxErrorMessage is the default error message for ValidateMax.
var ValidateMinErrorMessage = "{field} must be more than {min}"
ValidateMinErrorMessage is the default error message for ValidateMin.
var ValidatePatternErrorMessage = "{field}'s format is invalid"
ValidatePatternErrorMessage is the default error message for ValidatePattern.
var ValidateRangeErrorMessage = "{field} must be between {min} and {max}"
ValidateRangeErrorMessage is the default error message for ValidateRange.
var ValidateRegexpErrorMessage = "{field}'s format is invalid"
ValidateRegexpErrorMessage is the default error message for ValidateRegexp.
var ValidateRequiredErrorMessage = "{field} is required"
ValidateRequiredErrorMessage is the default error message for ValidateRequired.
Functions ¶
func AddError ¶
AddError adds an error to changeset.
ch := changeset.Cast(user, params, fields) changeset.AddError(ch, "field", "error") ch.Errors() // []errors.Error{{Field: "field", Message: "error"}}
func ApplyString ¶
ApplyString apply a function for string value.
func CastAssoc ¶
func CastAssoc(ch *Changeset, field string, fn ChangeFunc, opts ...Option)
CastAssoc casts association changes using changeset function. Repo insert or update won't persist any changes generated by CastAssoc.
func CheckConstraint ¶
CheckConstraint adds an unique constraint to changeset.
func EscapeString ¶
EscapeString escapes special characters like "<" to become "<". this is helper for html.EscapeString
func ForeignKeyConstraint ¶
ForeignKeyConstraint adds an unique constraint to changeset.
func PutDefault ¶
PutDefault to changeset.
func UnescapeString ¶
UnescapeString unescapes entities like "<" to become "<". this is helper for html.UnescapeString.
func UniqueConstraint ¶
UniqueConstraint adds an unique constraint to changeset.
func ValidateExclusion ¶
ValidateExclusion validates a change is not included in the given values.
func ValidateInclusion ¶
ValidateInclusion validates a change is included in the given values.
func ValidateMax ¶
ValidateMax validates the value of given field is not larger than max. Validation can be performed against string, slice and numbers.
func ValidateMin ¶
ValidateMin validates the value of given field is not smaller than min. Validation can be performed against string, slice and numbers.
func ValidatePattern ¶
ValidatePattern validates the value of given field to match given pattern.
func ValidateRange ¶
ValidateRange validates the value of given field is not larger than max and not smaller than min. Validation can be performed against string, slice and numbers.
func ValidateRegexp ¶
ValidateRegexp validates the value of given field to match given regexp.
func ValidateRequired ¶
ValidateRequired validates that one or more fields are present in the changeset. It'll add error to changeset if field in the changes is nil or string made only of whitespace.
Types ¶
type ChangeFunc ¶
ChangeFunc is changeset function.
type Changeset ¶
type Changeset struct {
// contains filtered or unexported fields
}
Changeset used to cast and validate data before saving it to the database.
func Cast ¶
Cast params as changes for the given data according to the permitted fields. Returns a new changeset. params will only be added as changes if it does not have the same value as the field in the data.
Example ¶
type User struct { ID int Name string } user := User{} input := params.Map{ "id": 1, "name": "name", } ch := Cast(user, input, []string{"name"}) fmt.Println(ch.Changes())
Output: map[name:name]
Example (InvalidType) ¶
type User struct { ID int Name string } user := User{} input := params.Map{ "id": 1, "name": true, } ch := Cast(user, input, []string{"name"}) fmt.Println(ch.Error())
Output: name is invalid
Example (InvalidTypeWithCustomError) ¶
type User struct { ID int Name string } user := User{} input := params.Map{ "id": 1, "name": true, } ch := Cast(user, input, []string{"name"}, Message("{field} tidak valid")) fmt.Println(ch.Error())
Output: name tidak valid
func Change ¶
Change make a new changeset without changes and build from given schema. Returns new Changeset.
func Convert ¶
func Convert(data interface{}) *Changeset
Convert a struct as changeset, every field's value will be treated as changes. Returns a new changeset.
func (Changeset) Constraints ¶
func (c Changeset) Constraints() Constraints
Constraints of changeset.
type Constraint ¶
type Constraint struct { Field string Message string Code int Name string Exact bool Type rel.ConstraintType }
Constraint defines information to infer constraint error.
type Constraints ¶
type Constraints []Constraint
Constraints is slice of Constraint
func (Constraints) GetError ¶
func (constraints Constraints) GetError(err error) error
GetError converts error based on constraints. If the original error is constraint error, and it's defined in the constraint list, then it'll be updated with constraint's message. If the original error is constraint error but not defined in the constraint list, it'll be converted to unexpected error. else it'll not modify the error.
type Error ¶
type Error struct { Message string `json:"message"` Field string `json:"field,omitempty"` Code int `json:"code,omitempty"` Err error `json:"-"` }
Error struct.
type Option ¶
type Option func(*Options)
Option for changeset operation.
func ChangeOnly ¶
ChangeOnly is used to define if validate is only check change
func EmptyValues ¶
func EmptyValues(values ...interface{}) Option
EmptyValues defines list of empty values when casting. default to [""]
func SourceField ¶
SourceField to define used field name in params.
Source Files ¶
- add_error.go
- apply_string.go
- cast.go
- cast_assoc.go
- change.go
- changeset.go
- check_constraint.go
- constraint.go
- convert.go
- delete_change.go
- escape_string.go
- foreign_key_constraint.go
- options.go
- put_assoc.go
- put_change.go
- put_default.go
- reflect.go
- schema.go
- unescape_string.go
- unique_constraint.go
- validate_exclusion.go
- validate_inclusion.go
- validate_max.go
- validate_min.go
- validate_pattern.go
- validate_range.go
- validate_regexp.go
- validate_required.go