Documentation
¶
Overview ¶
Package validate provides a declarative, rules-based validation framework for validating user-supplied data.
The types in this package are designed to be used in conjunction with the handler.WsHandler type which your application will use to represent web service endpoints (see the package documentation for ws, ws/handler and https://granitic.io/ref/web-service-handlers )
The purpose of Granitic's validation framework is to automate as much of the 'boiler-plate' validation from validated the data supplied with the a web service call. Simple checks for ensuring a field is present, well-formed and within an allowed range can clutter application code.
Granitic's validation framework and patterns are covered in detail at https://granitic.io/ref/validation but a brief overview of the key types and concepts follows.
RuleValidator ¶
Each instance of handler.WsHandler in your application has an optional field
RuleValidator *validate.RuleValidator
If an AutoValidator is provided, its Validate method will be invoked before your handler's Logic.Process method is called. If errors are detected by the RuleValidator, processing stops and an error response will be served to the web service caller.
A RuleValidator is declared in your component definition file in manner similar to:
{ "createRecordHandler": { "type": "handler.WsHandler", "HTTPMethod": "POST", "Logic": "ref:createRecordLogic", "PathPattern": "^/record$", "AutoValidator": "ref:createRecordValidator" }, "createRecordValidator": { "type": "validate.RuleValidator", "DefaultErrorCode": "CREATE_RECORD", "Rules": "conf:createRecordRules" } }
Each RuleValidator requires a set of rules, which must be defined in your application's configuration file like:
{ "createRecordRules": [ ["CatalogRef", "STR", "REQ:CATALOG_REF_MISSING", "HARDTRIM", "BREAK", "REG:^[A-Z]{3}-[\\d]{6}$:CATALOG_REF"], ["Name", "STR:RECORD_NAME", "REQ", "HARDTRIM", "LEN:1-128"], ["Artist", "STR:ARTIST_NAME", "REQ", "HARDTRIM", "LEN:1-64"], ["Tracks", "SLICE:TRACK_COUNT", "LEN:1-100", "ELEM:trackName"] ] }
(The spacing in the example above is to illustrate the components of a rule and has no effect on behaviour.)
Rule structure ¶
Rules consist of three components: a field name, type and one or more operations.
The field name is a field in the WsRequest.Body object that is to be validated.
The type is a shorthand for the the type of the field to be validated (see https://granitic.io/ref/validation )
The operations are either checks that should be performed against the field (length checks, regexs etc), processing instructions (break processing if the previous check failed) or manipulations of the data to be validated (trim a string, etc). See https://granitic.io/ref/validation for more detail.
For checks and processing instructions, the order in which they appear in the rule is significant as checks are made from left to right.
Error codes ¶
Error codes determine what error is sent to a web service caller if a check fails. Error codes can be defined in three levels of granularity - on an operation, on a rule or on a RuleValidator. The most specific error available is always used.
Using the validation framework requires the ServiceErrorManager facility to be enabled (see https://granitic.io/ref/service-error-management )
Sharing rules ¶
Sometimes it is useful for a rule to be defined once and re-used by multiple RuleValidators. This is also required to use some advanced techniques for deep validation of the elements of a slice. This technique is described in detail at https://granitic.io/ref/validation rule manager.
Decomposing the application of a rule ¶
The first rule in the example above is:
["CatalogRef", "STR", "REQ:CATALOG_REF_MISSING", "HARDTRIM", "BREAK", "REG:^[A-Z]{3}-[\\d]{6}$:CATALOG_REF"]
It is a very typical example of a string validation rule and breaks down as follows.
1. The field CatalogRef on the web service's WsRequest.Body will be validated.
2. The field will be treated as a string. Note, no error code is defined with the type so the RuleValidator's DefaultErrorCode will be used.
3. The field is REQuired. If the field is not set, the error CATALOG_REF_MISSING will be included in the eventual response to the web service call.
4. The field will be HARDTRIMmed - the actual value of CatalogRef will be permanently modified to remove leading and trailing spaces before further validation checks are applied (an alternative TRIM will mean validation occurs on a trimmed copy of the string, but the underlying data is not permanently modified.
5. If previous checks, in this case the REQ check, failed, processing will BREAK and the next validation rule will be processed.
6. The value of CatalogRef is compared to the regex ^[A-Z]{3}-[\\d]{6}$ If there is no match, the error CATALOG_REF will be included in the eventual response to the web service call.
Advanced techniques ¶
The Granitic validation framework is deep and flexible and you are encouraged to read the reference at https://granitic.io/ref/validation Advanced techniques include cross field mutual exclusivity, deep validation of slice elements and cross-field dependencies.
Programmatic creation of rules ¶
It is possible to define rules in your application code. Each type of rule supports a fluent-style interface to make application code more readable in this case. The rule above could be expressed as
sv := NewStringValidator("CatalogRef", "CREATE_RECORD"). Required("CATALOG_REF_MISSING"). HardTrim(). Break(). Regex("^[A-Z]{3}-[\\d]{6}$", "CATALOG_REF")
Index ¶
- type BoolValidationRule
- func (bv *BoolValidationRule) CodesInUse() types.StringSet
- func (bv *BoolValidationRule) DependsOnFields() types.StringSet
- func (bv *BoolValidationRule) Is(v bool, code ...string) *BoolValidationRule
- func (bv *BoolValidationRule) IsSet(field string, subject interface{}) (bool, error)
- func (bv *BoolValidationRule) MEx(fields types.StringSet, code ...string) *BoolValidationRule
- func (bv *BoolValidationRule) Required(code ...string) *BoolValidationRule
- func (bv *BoolValidationRule) StopAll() *BoolValidationRule
- func (bv *BoolValidationRule) StopAllOnFail() bool
- func (bv *BoolValidationRule) Validate(vc *ValidationContext) (result *ValidationResult, unexpected error)
- type ExternalFloat64Validator
- type ExternalInt64Validator
- type ExternalStringValidator
- type FieldErrors
- type FloatValidationRule
- func (fv *FloatValidationRule) Break() *FloatValidationRule
- func (fv *FloatValidationRule) CodesInUse() types.StringSet
- func (fv *FloatValidationRule) DependsOnFields() types.StringSet
- func (fv *FloatValidationRule) ExternalValidation(v ExternalFloat64Validator, code ...string) *FloatValidationRule
- func (fv *FloatValidationRule) In(set []float64, code ...string) *FloatValidationRule
- func (fv *FloatValidationRule) IsSet(field string, subject interface{}) (bool, error)
- func (fv *FloatValidationRule) MEx(fields types.StringSet, code ...string) *FloatValidationRule
- func (fv *FloatValidationRule) Range(checkMin, checkMax bool, min, max float64, code ...string) *FloatValidationRule
- func (fv *FloatValidationRule) Required(code ...string) *FloatValidationRule
- func (fv *FloatValidationRule) StopAll() *FloatValidationRule
- func (fv *FloatValidationRule) StopAllOnFail() bool
- func (fv *FloatValidationRule) Validate(vc *ValidationContext) (result *ValidationResult, unexpected error)
- type IntValidationRule
- func (iv *IntValidationRule) Break() *IntValidationRule
- func (iv *IntValidationRule) CodesInUse() types.StringSet
- func (iv *IntValidationRule) DependsOnFields() types.StringSet
- func (iv *IntValidationRule) ExternalValidation(v ExternalInt64Validator, code ...string) *IntValidationRule
- func (iv *IntValidationRule) In(set []string, code ...string) *IntValidationRule
- func (iv *IntValidationRule) IsSet(field string, subject interface{}) (bool, error)
- func (iv *IntValidationRule) MEx(fields types.StringSet, code ...string) *IntValidationRule
- func (iv *IntValidationRule) Range(checkMin, checkMax bool, min, max int64, code ...string) *IntValidationRule
- func (iv *IntValidationRule) Required(code ...string) *IntValidationRule
- func (iv *IntValidationRule) StopAll() *IntValidationRule
- func (iv *IntValidationRule) StopAllOnFail() bool
- func (iv *IntValidationRule) Validate(vc *ValidationContext) (result *ValidationResult, unexpected error)
- type ObjectValidationRule
- func (ov *ObjectValidationRule) CodesInUse() types.StringSet
- func (ov *ObjectValidationRule) DependsOnFields() types.StringSet
- func (ov *ObjectValidationRule) IsSet(field string, subject interface{}) (bool, error)
- func (ov *ObjectValidationRule) MEx(fields types.StringSet, code ...string) *ObjectValidationRule
- func (ov *ObjectValidationRule) Required(code ...string) *ObjectValidationRule
- func (ov *ObjectValidationRule) StopAll() *ObjectValidationRule
- func (ov *ObjectValidationRule) StopAllOnFail() bool
- func (ov *ObjectValidationRule) Validate(vc *ValidationContext) (result *ValidationResult, unexpected error)
- type RuleValidator
- func (ov *RuleValidator) ComponentName() string
- func (ov *RuleValidator) Container(container *ioc.ComponentContainer)
- func (ov *RuleValidator) ErrorCodesInUse() (codes types.StringSet, sourceName string)
- func (ov *RuleValidator) SetComponentName(name string)
- func (ov *RuleValidator) StartComponent() error
- func (ov *RuleValidator) Validate(ctx context.Context, subject *SubjectContext) ([]*FieldErrors, error)
- func (ov *RuleValidator) ValidateMissing() bool
- type SliceValidationRule
- func (sv *SliceValidationRule) CodesInUse() types.StringSet
- func (sv *SliceValidationRule) DependsOnFields() types.StringSet
- func (sv *SliceValidationRule) Elem(v ValidationRule, code ...string) *SliceValidationRule
- func (sv *SliceValidationRule) IsSet(field string, subject interface{}) (bool, error)
- func (sv *SliceValidationRule) Length(min, max int, code ...string) *SliceValidationRule
- func (sv *SliceValidationRule) MEx(fields types.StringSet, code ...string) *SliceValidationRule
- func (sv *SliceValidationRule) Required(code ...string) *SliceValidationRule
- func (sv *SliceValidationRule) StopAll() *SliceValidationRule
- func (sv *SliceValidationRule) StopAllOnFail() bool
- func (sv *SliceValidationRule) Validate(vc *ValidationContext) (result *ValidationResult, unexpected error)
- type StringValidationRule
- func (sv *StringValidationRule) Break() *StringValidationRule
- func (sv *StringValidationRule) CodesInUse() types.StringSet
- func (sv *StringValidationRule) DependsOnFields() types.StringSet
- func (sv *StringValidationRule) ExternalValidation(v ExternalStringValidator, code ...string) *StringValidationRule
- func (sv *StringValidationRule) HardTrim() *StringValidationRule
- func (sv *StringValidationRule) In(set []string, code ...string) *StringValidationRule
- func (sv *StringValidationRule) IsSet(field string, subject interface{}) (bool, error)
- func (sv *StringValidationRule) Length(min, max int, code ...string) *StringValidationRule
- func (sv *StringValidationRule) MEx(fields types.StringSet, code ...string) *StringValidationRule
- func (sv *StringValidationRule) Regex(r *regexp.Regexp, code ...string) *StringValidationRule
- func (sv *StringValidationRule) Required(code ...string) *StringValidationRule
- func (sv *StringValidationRule) StopAll() *StringValidationRule
- func (sv *StringValidationRule) StopAllOnFail() bool
- func (sv *StringValidationRule) Trim() *StringValidationRule
- func (sv *StringValidationRule) Validate(vc *ValidationContext) (result *ValidationResult, unexpected error)
- type SubjectContext
- type UnparsedRuleManager
- type ValidationContext
- type ValidationResult
- type ValidationRule
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BoolValidationRule ¶
type BoolValidationRule struct {
// contains filtered or unexported fields
}
BoolValidationRule is a ValidationRule for checking a bool or NilableBool field on an object. See the method definitions on this type for the supported operations.
func NewBoolValidationRule ¶
func NewBoolValidationRule(field, defaultErrorCode string) *BoolValidationRule
NewBoolValidationRule creates a new BoolValidationRule to check the named field and the supplied default error code.
func (*BoolValidationRule) CodesInUse ¶
func (bv *BoolValidationRule) CodesInUse() types.StringSet
CodesInUse implements ValidationRule.CodesInUse
func (*BoolValidationRule) DependsOnFields ¶
func (bv *BoolValidationRule) DependsOnFields() types.StringSet
DependsOnFields implements ValidationRule.DependsOnFields
func (*BoolValidationRule) Is ¶
func (bv *BoolValidationRule) Is(v bool, code ...string) *BoolValidationRule
Is adds a check to implements if the field is set to the supplied value.
func (*BoolValidationRule) IsSet ¶
func (bv *BoolValidationRule) IsSet(field string, subject interface{}) (bool, error)
IsSet returns true if the field to be validation is a bool or is a NilableBool which has been explicitly set.
func (*BoolValidationRule) MEx ¶
func (bv *BoolValidationRule) MEx(fields types.StringSet, code ...string) *BoolValidationRule
MEx adds a check to implements if any other of the fields with which this field is mutually exclusive have been set.
func (*BoolValidationRule) Required ¶
func (bv *BoolValidationRule) Required(code ...string) *BoolValidationRule
Required adds a check implements if the field under validation has been set.
func (*BoolValidationRule) StopAll ¶
func (bv *BoolValidationRule) StopAll() *BoolValidationRule
StopAll indicates that no further rules should be rule if this one fails.
func (*BoolValidationRule) StopAllOnFail ¶
func (bv *BoolValidationRule) StopAllOnFail() bool
StopAllOnFail implements ValidationRule.StopAllOnFail
func (*BoolValidationRule) Validate ¶
func (bv *BoolValidationRule) Validate(vc *ValidationContext) (result *ValidationResult, unexpected error)
Validate implements ValidationRule.Validate
type ExternalFloat64Validator ¶
type ExternalFloat64Validator interface { // ValidFloat64 returns true if the object considers the supplied float64 to be valid. ValidFloat64(float64) (bool, error) }
ExternalFloat64Validator is an object able to evaulate the supplied float64 to see if it meets some definition of validity.
type ExternalInt64Validator ¶
type ExternalInt64Validator interface { // ValidInt64 returns true if the implementation considers the supplied int64 to be valid. ValidInt64(int64) (bool, error) }
ExternalInt64Validator is an object able to evaluate the supplied int64 to check if it meets some definition of validity.
type ExternalStringValidator ¶
type ExternalStringValidator interface { // ValidString returns true if the implementation considers the supplied string to be valid. ValidString(string) (bool, error) }
An ExternalStringValidator is an object able to evaluate the supplied string to see if it meets some definition of validity.
type FieldErrors ¶
type FieldErrors struct { // The name of a field, or field[x] where x is a slice index if the field's type was slice Field string // The errors found on that field. ErrorCodes []string }
FieldErrors is a summary of all the errors found while validating an object
type FloatValidationRule ¶
type FloatValidationRule struct {
// contains filtered or unexported fields
}
FloatValidationRule is a ValidationRule for checking a float32, float64 or NilableFloat64 field on an object. implements the method definitions on this type for the supported operations. Note that float32 are converted to float64 before validation.
func NewFloatValidationRule ¶
func NewFloatValidationRule(field, defaultErrorCode string) *FloatValidationRule
NewFloatValidationRule creates a new FloatValidationRule to check the named field and the supplied default error code.
func (*FloatValidationRule) Break ¶
func (fv *FloatValidationRule) Break() *FloatValidationRule
Break adds a check to stop processing this rule if the previous check has failed.
func (*FloatValidationRule) CodesInUse ¶
func (fv *FloatValidationRule) CodesInUse() types.StringSet
CodesInUse implements ValidationRule.CodesInUse
func (*FloatValidationRule) DependsOnFields ¶
func (fv *FloatValidationRule) DependsOnFields() types.StringSet
DependsOnFields implements ValidationRule.DependsOnFields
func (*FloatValidationRule) ExternalValidation ¶
func (fv *FloatValidationRule) ExternalValidation(v ExternalFloat64Validator, code ...string) *FloatValidationRule
ExternalValidation adds a check to call the supplied object to ask it to check the validity of the float in question.
func (*FloatValidationRule) In ¶
func (fv *FloatValidationRule) In(set []float64, code ...string) *FloatValidationRule
In adds a check to implements if the float under validation is exactly equal to one of the float values specified.
func (*FloatValidationRule) IsSet ¶
func (fv *FloatValidationRule) IsSet(field string, subject interface{}) (bool, error)
IsSet returns true if the field to be validated is a float32, float64 or a NilableFloat64 whose value has been explicitly set.
func (*FloatValidationRule) MEx ¶
func (fv *FloatValidationRule) MEx(fields types.StringSet, code ...string) *FloatValidationRule
MEx adds a check to implements if any other of the fields with which this field is mutually exclusive have been set.
func (*FloatValidationRule) Range ¶
func (fv *FloatValidationRule) Range(checkMin, checkMax bool, min, max float64, code ...string) *FloatValidationRule
Range adds a check to implements if the float under validation is in the supplied range. checkMin/Max are set to false if no minimum or maximum bound is in effect.
func (*FloatValidationRule) Required ¶
func (fv *FloatValidationRule) Required(code ...string) *FloatValidationRule
Required adds a check to implements if the field under validation has been set.
func (*FloatValidationRule) StopAll ¶
func (fv *FloatValidationRule) StopAll() *FloatValidationRule
StopAll indicates that no further rules should be rule if this one fails.
func (*FloatValidationRule) StopAllOnFail ¶
func (fv *FloatValidationRule) StopAllOnFail() bool
StopAllOnFail implements ValidationRule.StopAllOnFail
func (*FloatValidationRule) Validate ¶
func (fv *FloatValidationRule) Validate(vc *ValidationContext) (result *ValidationResult, unexpected error)
Validate implements ValidationRule.Validate
type IntValidationRule ¶
type IntValidationRule struct {
// contains filtered or unexported fields
}
IntValidationRule is a ValidationRule for checking a native signed int type or NilableInt64 field on an object. See the method definitions on this type for the supported operations. Note that any native int types are converted to int64 before validation.
func NewIntValidationRule ¶
func NewIntValidationRule(field, defaultErrorCode string) *IntValidationRule
NewIntValidationRule creates a new IntValidationRule to check the named field with the supplied default error code.
func (*IntValidationRule) Break ¶
func (iv *IntValidationRule) Break() *IntValidationRule
Break adds a check to stop processing this rule if the previous check has failed.
func (*IntValidationRule) CodesInUse ¶
func (iv *IntValidationRule) CodesInUse() types.StringSet
CodesInUse implements ValidationRule.CodesInUse
func (*IntValidationRule) DependsOnFields ¶
func (iv *IntValidationRule) DependsOnFields() types.StringSet
DependsOnFields implements ValidationRule.DependsOnFields
func (*IntValidationRule) ExternalValidation ¶
func (iv *IntValidationRule) ExternalValidation(v ExternalInt64Validator, code ...string) *IntValidationRule
ExternalValidation adds a check to call the supplied object to ask it to check the validity of the int in question.
func (*IntValidationRule) In ¶
func (iv *IntValidationRule) In(set []string, code ...string) *IntValidationRule
In adds a check to see if the float under validation is exactly equal to one of the int values specified.
func (*IntValidationRule) IsSet ¶
func (iv *IntValidationRule) IsSet(field string, subject interface{}) (bool, error)
IsSet returns true if the field to be validated is a native intxx type or a NilableInt64 whose value has been explicitly set.
func (*IntValidationRule) MEx ¶
func (iv *IntValidationRule) MEx(fields types.StringSet, code ...string) *IntValidationRule
MEx adds a check to see if any other of the fields with which this field is mutually exclusive have been set.
func (*IntValidationRule) Range ¶
func (iv *IntValidationRule) Range(checkMin, checkMax bool, min, max int64, code ...string) *IntValidationRule
Range adds a check to see if the float under validation is in the supplied range. checkMin/Max are set to false if no minimum or maximum bound is in effect.
func (*IntValidationRule) Required ¶
func (iv *IntValidationRule) Required(code ...string) *IntValidationRule
Required adds a check to see if the field under validation has been set.
func (*IntValidationRule) StopAll ¶
func (iv *IntValidationRule) StopAll() *IntValidationRule
StopAll indicates that no further rules should be rule if this one fails.
func (*IntValidationRule) StopAllOnFail ¶
func (iv *IntValidationRule) StopAllOnFail() bool
StopAllOnFail implements ValidationRule.StopAllOnFail
func (*IntValidationRule) Validate ¶
func (iv *IntValidationRule) Validate(vc *ValidationContext) (result *ValidationResult, unexpected error)
Validate implements ValidationRule.Validate
type ObjectValidationRule ¶
type ObjectValidationRule struct {
// contains filtered or unexported fields
}
ObjectValidationRule is a ValidationRule for checking a struct or map field on an object. See the method definitions on this type for the supported operations.
func NewObjectValidationRule ¶
func NewObjectValidationRule(field, defaultErrorCode string) *ObjectValidationRule
NewObjectValidationRule creates a new ObjectValidationRule to check the specified field.
func (*ObjectValidationRule) CodesInUse ¶
func (ov *ObjectValidationRule) CodesInUse() types.StringSet
CodesInUse implements ValidationRule.CodesInUse
func (*ObjectValidationRule) DependsOnFields ¶
func (ov *ObjectValidationRule) DependsOnFields() types.StringSet
DependsOnFields implements ValidationRule.DependsOnFields
func (*ObjectValidationRule) IsSet ¶
func (ov *ObjectValidationRule) IsSet(field string, subject interface{}) (bool, error)
IsSet returns true if the field to be validated is a non-nil struct or map
func (*ObjectValidationRule) MEx ¶
func (ov *ObjectValidationRule) MEx(fields types.StringSet, code ...string) *ObjectValidationRule
MEx adds a check to see if any other of the fields with which this field is mutually exclusive have been set.
func (*ObjectValidationRule) Required ¶
func (ov *ObjectValidationRule) Required(code ...string) *ObjectValidationRule
Required adds a check to see if the field under validation has been set.
func (*ObjectValidationRule) StopAll ¶
func (ov *ObjectValidationRule) StopAll() *ObjectValidationRule
StopAll indicates that no further rules should be rule if this one fails.
func (*ObjectValidationRule) StopAllOnFail ¶
func (ov *ObjectValidationRule) StopAllOnFail() bool
StopAllOnFail implements ValidationRule.StopAllOnFail
func (*ObjectValidationRule) Validate ¶
func (ov *ObjectValidationRule) Validate(vc *ValidationContext) (result *ValidationResult, unexpected error)
Validate implements ValidationRule.Validate
type RuleValidator ¶
type RuleValidator struct { // A component able to look up ioc components by their name (normally the container itself) ComponentFinder ioc.ComponentLookup // The error code used to lookup error definitions if no error code is defined on a rule or rule operation. DefaultErrorCode string // Do not check to see if there are error definitions for all of the error codes referenced by the RuleValidator and its rules. DisableCodeValidation bool //Inject by the Granitic framework (an application Logger, not a framework Logger). Log logging.Logger //A source for rules that are shared across multiple RuleValidators RuleManager *UnparsedRuleManager //The text representation of rules in the order in which they should be applied. Rules [][]string // contains filtered or unexported fields }
RuleValidator coordinates the parsing and application of rules to validate a specific object. Normally an instance of this type is unique to an instance of ws.WsHandler.
func (*RuleValidator) ComponentName ¶
func (ov *RuleValidator) ComponentName() string
ComponentName implements ComponentNamer.ComponentName
func (*RuleValidator) Container ¶
func (ov *RuleValidator) Container(container *ioc.ComponentContainer)
Container accepts a reference to the IoC container.
func (*RuleValidator) ErrorCodesInUse ¶
func (ov *RuleValidator) ErrorCodesInUse() (codes types.StringSet, sourceName string)
ErrorCodesInUse returns all of the unique error codes used by this validator, its rules and their operations.
func (*RuleValidator) SetComponentName ¶
func (ov *RuleValidator) SetComponentName(name string)
SetComponentName implements ComponentNamer.SetComponentName
func (*RuleValidator) StartComponent ¶
func (ov *RuleValidator) StartComponent() error
StartComponent is called by the IoC container. Parses the rules into ValidationRule objects.
func (*RuleValidator) Validate ¶
func (ov *RuleValidator) Validate(ctx context.Context, subject *SubjectContext) ([]*FieldErrors, error)
Validate the object in the supplied subject according to the rules defined on this RuleValidator. Returns a summary of any problems found.
func (*RuleValidator) ValidateMissing ¶
func (ov *RuleValidator) ValidateMissing() bool
ValidateMissing returns true if all error codes reference by this RuleValidator must have corresponding definitions
type SliceValidationRule ¶
type SliceValidationRule struct {
// contains filtered or unexported fields
}
SliceValidationRule is a ValidationRule able to validate a slice field and the individual elements of that slice. See the method definitions on this type for the supported operations.
func NewSliceValidationRule ¶
func NewSliceValidationRule(field, defaultErrorCode string) *SliceValidationRule
NewSliceValidationRule creates a new NewSliceValidationRule to check the specified field.
func (*SliceValidationRule) CodesInUse ¶
func (sv *SliceValidationRule) CodesInUse() types.StringSet
CodesInUse implements ValidationRule.CodesInUse
func (*SliceValidationRule) DependsOnFields ¶
func (sv *SliceValidationRule) DependsOnFields() types.StringSet
DependsOnFields implements ValidationRule.DependsOnFields
func (*SliceValidationRule) Elem ¶
func (sv *SliceValidationRule) Elem(v ValidationRule, code ...string) *SliceValidationRule
Elem supplies a ValidationRule that can be used to checked the validity of the elements of the slice.
func (*SliceValidationRule) IsSet ¶
func (sv *SliceValidationRule) IsSet(field string, subject interface{}) (bool, error)
IsSet returns true if the field to be validated is a non-nil slice.
func (*SliceValidationRule) Length ¶
func (sv *SliceValidationRule) Length(min, max int, code ...string) *SliceValidationRule
Length adds a check to see if the slice under consideration has an element count between the supplied min and max values.
func (*SliceValidationRule) MEx ¶
func (sv *SliceValidationRule) MEx(fields types.StringSet, code ...string) *SliceValidationRule
MEx adds a check to see if any other of the fields with which this field is mutually exclusive have been set.
func (*SliceValidationRule) Required ¶
func (sv *SliceValidationRule) Required(code ...string) *SliceValidationRule
Required adds a check to see if the field under validation has been set.
func (*SliceValidationRule) StopAll ¶
func (sv *SliceValidationRule) StopAll() *SliceValidationRule
StopAll indicates that no further rules should be rule if this one fails.
func (*SliceValidationRule) StopAllOnFail ¶
func (sv *SliceValidationRule) StopAllOnFail() bool
StopAllOnFail implements ValidationRule.StopAllOnFail
func (*SliceValidationRule) Validate ¶
func (sv *SliceValidationRule) Validate(vc *ValidationContext) (result *ValidationResult, unexpected error)
Validate implements ValidationRule.Validate
type StringValidationRule ¶
type StringValidationRule struct {
// contains filtered or unexported fields
}
StringValidationRule is a ValidationRule able to validate a string or NilableString field. See the method definitions on this type for the supported operations.
func NewStringValidationRule ¶
func NewStringValidationRule(field, defaultErrorCode string) *StringValidationRule
NewStringValidationRule creates a new NewStringValidationRule to check the specified field.
func (*StringValidationRule) Break ¶
func (sv *StringValidationRule) Break() *StringValidationRule
Break adds a check to stop processing this rule if the previous check has failed.
func (*StringValidationRule) CodesInUse ¶
func (sv *StringValidationRule) CodesInUse() types.StringSet
CodesInUse implements ValidationRule.CodesInUse
func (*StringValidationRule) DependsOnFields ¶
func (sv *StringValidationRule) DependsOnFields() types.StringSet
DependsOnFields implements ValidationRule.DependsOnFields
func (*StringValidationRule) ExternalValidation ¶
func (sv *StringValidationRule) ExternalValidation(v ExternalStringValidator, code ...string) *StringValidationRule
ExternalValidation adds a check to call the supplied object to ask it to check the validity of the string in question.
func (*StringValidationRule) HardTrim ¶
func (sv *StringValidationRule) HardTrim() *StringValidationRule
HardTrim specifies that the string to be validated should be trimmed before validation. This affects the underlying value permanently. To have the same functionality without modifying the string, use the Trim method instead.
func (*StringValidationRule) In ¶
func (sv *StringValidationRule) In(set []string, code ...string) *StringValidationRule
In adds a check to confirm that the string exactly matches one of those in the supplied set.
func (*StringValidationRule) IsSet ¶
func (sv *StringValidationRule) IsSet(field string, subject interface{}) (bool, error)
IsSet returns false if the field is a string or if it is a nil or unset NilableString
func (*StringValidationRule) Length ¶
func (sv *StringValidationRule) Length(min, max int, code ...string) *StringValidationRule
Length adds a check to see if the string has a length between the supplied min and max values.
func (*StringValidationRule) MEx ¶
func (sv *StringValidationRule) MEx(fields types.StringSet, code ...string) *StringValidationRule
MEx adds a check to see if any other of the fields with which this field is mutually exclusive have been set.
func (*StringValidationRule) Regex ¶
func (sv *StringValidationRule) Regex(r *regexp.Regexp, code ...string) *StringValidationRule
Regex adds a check to confirm that the string being validated matches the supplied regular expression.
func (*StringValidationRule) Required ¶
func (sv *StringValidationRule) Required(code ...string) *StringValidationRule
Required adds a check to see if the field under validation has been set.
func (*StringValidationRule) StopAll ¶
func (sv *StringValidationRule) StopAll() *StringValidationRule
StopAll indicates that no further rules should be rule if this one fails.
func (*StringValidationRule) StopAllOnFail ¶
func (sv *StringValidationRule) StopAllOnFail() bool
StopAllOnFail implements ValidationRule.StopAllOnFail
func (*StringValidationRule) Trim ¶
func (sv *StringValidationRule) Trim() *StringValidationRule
Trim specifies that all validation checks should be performed on a copy of the string with leading and trailing whitespace removed.
func (*StringValidationRule) Validate ¶
func (sv *StringValidationRule) Validate(vc *ValidationContext) (result *ValidationResult, unexpected error)
Validate implements ValidationRule.Validate
type SubjectContext ¶
type SubjectContext struct {
//An instance of a object to be validated.
Subject interface{}
}
SubjectContext is a wrapper for an object (the subject) to be validated
type UnparsedRuleManager ¶
type UnparsedRuleManager struct { // A map between a name for a rule and the rule's unparsed definition. Rules map[string][]string }
UnparsedRuleManager is a container for rules that are shared between multiple RuleValidator instances. The rules are stored in their raw and unparsed JSON representation.
func (*UnparsedRuleManager) Exists ¶
func (rm *UnparsedRuleManager) Exists(ref string) bool
Exists returns true if a rule with the supplied name exists.
func (*UnparsedRuleManager) Rule ¶
func (rm *UnparsedRuleManager) Rule(ref string) []string
Rule returns the unparsed representation of the rule with the supplied name.
type ValidationContext ¶
type ValidationContext struct { // The object containing the field to be validated OR (if DirectSubject is true) the value to be validated. Subject interface{} // Other fields on the object that are known to have been set (for mutual exclusivity/dependency checking) KnownSetFields types.StringSet // Use this field name, rather than the one associated with the rule that this context will be passed to. OverrideField string //Indicate that the Subject in this context IS the value to be validated, rather than the container of a field. DirectSubject bool }
ValidationContext is a wrapper for, and meta-data about, a field on an object to be validated.
type ValidationResult ¶
type ValidationResult struct { // A map of field names to the errors encountered on this field. In the case of non-slice types, there will // be only one entry in the map. For slices, there will be an entry for every index into the slice where problems // were found. ErrorCodes map[string][]string // If the field that was to be validated was 'unset' (definition varies by type) Unset bool }
ValidationResult contains the result of applying a rule to a field on an object.
func NewPopulatedValidationResult ¶
func NewPopulatedValidationResult(field string, codes []string) *ValidationResult
NewPopulatedValidationResult with the supplied errors recorded against the supplied field name
func NewValidationResult ¶
func NewValidationResult() *ValidationResult
NewValidationResult creates an empty ValidationResult ready to record errors.
func (*ValidationResult) AddForField ¶
func (vr *ValidationResult) AddForField(field string, codes []string)
AddForField captures the name of a field or slice index and the codes of all errors found for that field/index or if the field/index previously had errors records, adds these new errors.
func (*ValidationResult) ErrorCount ¶
func (vr *ValidationResult) ErrorCount() int
ErrorCount returns the total number of errors recorded in this result (NOT the number of unique error codes encountered).
type ValidationRule ¶
type ValidationRule interface { // Check the subject provided in the supplied context and return any found errors. Validate(vc *ValidationContext) (result *ValidationResult, unexpected error) // Tell the coordinating object that no other rules should be processed if errors are found by this rule. StopAllOnFail() bool // Summarise all of the unique error codes referenced by checks in this rule. CodesInUse() types.StringSet // Declare other fields that must be set in order for this field to be valid. DependsOnFields() types.StringSet // Check whether or not the supplied object is 'set' according to this rule's definition of set. IsSet(string, interface{}) (bool, error) }
ValidationRule is a validation rule associated with a named field on an object able to be applied in a generic fashion. Implementations exist for each type (bool, string etc) supported by the validation framework.