Documentation ¶
Overview ¶
Package validator provides logic to assist with validation tasks. The logic is designed so that each subsequent validation step short-circuits after the first validation failure; only the first validation failure is reported.
Credit to Fabrizio Milo for sharing the original implementation:
- https://stackoverflow.com/a/23960293/903870 - https://github.com/Mistobaan
Index ¶
- type Validater
- type Validator
- func (v *Validator) Err() error
- func (v *Validator) Error() string
- func (v *Validator) FieldHasSpecificValue(fieldVal string, fieldValDesc string, reqVal string, typeDesc string, ...) bool
- func (v *Validator) FieldHasSpecificValueIfFieldNotEmpty(fieldVal string, fieldValDesc string, reqVal string, typeDesc string, ...) bool
- func (v *Validator) InList(fieldVal string, fieldValDesc string, typeDesc string, validVals []string, ...) bool
- func (v *Validator) InListIfFieldValNotEmpty(fieldVal string, fieldValDesc string, typeDesc string, validVals []string, ...) bool
- func (v *Validator) IsValid() bool
- func (v *Validator) NoNilValuesInCollection(fieldValueDesc string, typeDesc string, baseErr error, items ...interface{}) bool
- func (v *Validator) NotEmptyCollection(fieldValueDesc string, typeDesc string, baseErr error, items ...interface{}) bool
- func (v *Validator) NotEmptyCollectionIfFieldValNotEmpty(fieldVal string, fieldValueDesc string, typeDesc string, baseErr error, ...) bool
- func (v *Validator) NotEmptyValue(fieldVal string, fieldValDesc string, typeDesc string, baseErr error) bool
- func (v *Validator) SelfValidate(items ...Validater) bool
- func (v *Validator) SelfValidateIfXEqualsY(x string, y string, items ...Validater) bool
- func (v *Validator) SuccessfulFuncCall(fn func() error) bool
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Validater ¶
type Validater interface {
Validate() error
}
Validater is the interface shared by all supported types which provide validation of their fields.
type Validator ¶
type Validator struct {
// contains filtered or unexported fields
}
Validator is used to perform validation of given values. Each validation method for this type is designed to exit early in order to preserve any prior validation failure. If a previous validation check failure occurred, the most recent validation check result will
After performing a validation check, the caller is responsible for checking the result to determine if further validation checks should be performed.
Heavily inspired by: https://stackoverflow.com/a/23960293/903870
func (*Validator) FieldHasSpecificValue ¶
func (v *Validator) FieldHasSpecificValue( fieldVal string, fieldValDesc string, reqVal string, typeDesc string, baseErr error, ) bool
FieldHasSpecificValue asserts that fieldVal is reqVal. fieldValDesc describes the field value being validated (e.g., "Type") and typeDesc describes the specific struct or value type whose field we are validating (e.g., "Element").
A true value is returned if the validation step passed. A false value is returned if this or a prior validation step failed.
func (*Validator) FieldHasSpecificValueIfFieldNotEmpty ¶
func (v *Validator) FieldHasSpecificValueIfFieldNotEmpty( fieldVal string, fieldValDesc string, reqVal string, typeDesc string, baseErr error, ) bool
FieldHasSpecificValueIfFieldNotEmpty asserts that fieldVal is reqVal unless fieldVal is empty. fieldValDesc describes the field value being validated (e.g., "Type") and typeDesc describes the specific struct or value type whose field we are validating (e.g., "Element").
A true value is returned if the validation step passed. A false value is returned if this or a prior validation step failed.
func (*Validator) InList ¶
func (v *Validator) InList(fieldVal string, fieldValDesc string, typeDesc string, validVals []string, baseErr error) bool
InList reports whether fieldVal is in validVals. fieldValDesc describes the field value being validated (e.g., "Type") and typeDesc describes the specific struct or value type whose field we are validating (e.g., "Element").
A true value is returned if fieldVal is is in validVals.
A false value is returned if any of:
- a prior validation step failed
- fieldVal is empty
- fieldVal is non-empty and not in validVals
- the validVals collection to compare against is empty
func (*Validator) InListIfFieldValNotEmpty ¶
func (v *Validator) InListIfFieldValNotEmpty(fieldVal string, fieldValDesc string, typeDesc string, validVals []string, baseErr error) bool
InListIfFieldValNotEmpty reports whether fieldVal is in validVals if fieldVal is not empty. fieldValDesc describes the field value being validated (e.g., "Type") and typeDesc describes the specific struct or value type whose field we are validating (e.g., "Element").
A true value is returned if fieldVal is empty or is in validVals.
A false value is returned if any of:
- a prior validation step failed
- fieldVal is not empty and is not in validVals
- the validVals collection to compare against is empty
func (*Validator) IsValid ¶
IsValid indicates whether validation checks performed thus far have all passed.
func (*Validator) NoNilValuesInCollection ¶
func (v *Validator) NoNilValuesInCollection(fieldValueDesc string, typeDesc string, baseErr error, items ...interface{}) bool
NoNilValuesInCollection asserts that the specified items collection does not contain any nil values. fieldValueDesc describes the field for this collection being validated (e.g., "Facts") and typeDesc describes the specific struct or value type whose field we are validating (e.g., "Element").
A true value is returned if the collection does not contain any nil values (even if the collection itself has no values). A false value is returned if a prior validation step failed or if any items in the collection are nil.
func (*Validator) NotEmptyCollection ¶
func (v *Validator) NotEmptyCollection(fieldValueDesc string, typeDesc string, baseErr error, items ...interface{}) bool
NotEmptyCollection asserts that the specified items collection is not empty. fieldValueDesc describes the field for this collection being validated (e.g., "Facts") and typeDesc describes the specific struct or value type whose field we are validating (e.g., "Element").
A true value is returned if the collection is not empty. A false value is returned if a prior validation step failed or if the items collection is empty.
func (*Validator) NotEmptyCollectionIfFieldValNotEmpty ¶
func (v *Validator) NotEmptyCollectionIfFieldValNotEmpty( fieldVal string, fieldValueDesc string, typeDesc string, baseErr error, items ...interface{}, ) bool
NotEmptyCollectionIfFieldValNotEmpty asserts that the specified items collection is not empty if fieldVal is not empty. fieldValueDesc describes the field for this collection being validated (e.g., "Facts") and typeDesc describes the specific struct or value type whose field we are validating (e.g., "Element").
A true value is returned if the collection is not empty. A false value is returned if a prior validation step failed or if the items collection is empty.
func (*Validator) NotEmptyValue ¶
func (v *Validator) NotEmptyValue(fieldVal string, fieldValDesc string, typeDesc string, baseErr error) bool
NotEmptyValue asserts that fieldVal is not empty. fieldValDesc describes the field value being validated (e.g., "Type") and typeDesc describes the specific struct or value type whose field we are validating (e.g., "Element").
A true value is returned if the validation step passed. A false value is returned if this or a prior validation step failed.
func (*Validator) SelfValidate ¶
SelfValidate asserts that each given item can self-validate.
A true value is returned if the validation step passed. A false value is returned if this or a prior validation step failed.
func (*Validator) SelfValidateIfXEqualsY ¶
SelfValidateIfXEqualsY asserts that each given item can self-validate if value x is equal to y.
A true value is returned if the validation step passed. A false value is returned false if this or a prior validation step failed.
func (*Validator) SuccessfulFuncCall ¶
SuccessfulFuncCall accepts fn, a function that returns an error. fn is called in order to determine validation results.
A true value is returned if fn was successful. A false value is returned if a prior validation step failed or if fn returned an error.