Documentation
¶
Index ¶
- Variables
- func Define(name string, obj any) error
- func DumpJSON() []byte
- func ParseDuration(durationString string) (time.Duration, error)
- func SetTagName(name string) error
- func Split(input string, separator string) []string
- func UnMarshal(data []byte, value any) error
- func ValidateByName(name string, text string) error
- type Item
- func (i *Item) AddField(v Item) *Item
- func (i *Item) Copy() *Item
- func (item *Item) ParseTag(tag string) error
- func (i *Item) SetEnums(values ...any) *Item
- func (i *Item) SetField(index int, v Item) *Item
- func (i *Item) SetForeignKeys(b bool) *Item
- func (i *Item) SetMatchCase(b bool) *Item
- func (i *Item) SetMaxLength(l int) *Item
- func (i *Item) SetMaxValue(v any) *Item
- func (i *Item) SetMinLength(l int) *Item
- func (i *Item) SetMinValue(v any) *Item
- func (i *Item) SetName(name string) *Item
- func (i *Item) SetRequired(required bool) *Item
- func (i *Item) String() string
- func (i *Item) Validate(text string) error
- type Type
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
Dictionary is the map that retains named validators. When a validator is defined of type struct, it is stored in the Dictionary. This allows recursive references to a validator to be resolved by a dictionary lookup. Because validator definitions are thread-safe, a mutex lock serializes access to the dictionary.
var ErrArrayLengthOutOfRange = NewError("array length out of range")
Predefined validation errors.
var ErrEmptyTag = NewError("empty tag")
var ErrEmptyTagValue = NewError("empty tag value")
var ErrInvalidBaseTag = NewError("invalid base tag (only allowed on arrays and maps)")
var ErrInvalidData = NewError("invalid data")
var ErrInvalidDuration = NewError("invalid duration value")
var ErrInvalidEnumType = NewError("invalid field type for enum, must be string or int")
var ErrInvalidEnumeratedValue = NewError("invalid enumerated value")
var ErrInvalidFieldName = NewError("invalid field name")
var ErrInvalidInteger = NewError("invalid integer value")
var ErrInvalidKeyword = NewError("invalid keyword")
var ErrInvalidListTag = NewError("invalid list tag for item type")
var ErrInvalidName = NewError("invalid name")
var ErrInvalidTagName = NewError("invalid tag name")
var ErrInvalidValidator = NewError("invalid JSON instance of validator")
var ErrMaxDepthExceeded = NewError("maximum validation depth exceeded")
var ErrMissingEnumValue = NewError("missing enum values")
var ErrNameAlreadyExists = NewError("name already exists")
var ErrNilValidator = NewError("nil validator")
var ErrNotAMap = NewError("keyword only valid with map type")
var ErrRequired = NewError("required field missing")
var ErrUndefinedStructure = NewError("undefined structure")
var ErrUnimplemented = NewError("unimplemented type")
var ErrUnsupportedType = NewError("unsupported type")
var ErrValueLengthOutOfRange = NewError("value length out of range")
var ErrValueOutOfRange = NewError("value out of range")
var TypeNames = map[Type]string{ TypePointer: "pointer", TypeInvalid: "invalid", TypeString: "string", TypeInt: "int", TypeFloat: "float", TypeBool: "bool", TypeStruct: "struct", TypeArray: "array", TypeAny: "any", TypeUUID: "uuid.UUID", TypeTime: "time.Time", TypeDuration: "time.Duration", TypeMap: "map[string]any", TypeList: "stringList", }
Map used to convert Type values to a string name.
Functions ¶
func Define ¶
Define a new validator by name. The validator is created using normal reflection, and the resulting validator is stored in the Dictionary. This method serializes access to the dictionary. If the name already exists in the Dictionary, an error is returned.
func DumpJSON ¶ added in v0.1.5
func DumpJSON() []byte
DumpJSON is a diagnostic function that allows the user of the validator package to dump the contents of the Dictionary in JSON format.
func ParseDuration ¶ added in v0.1.7
ParseDuration parses a duration string and returns the corresponding time.Duration. It handles extensions that allow specifying days in a duration, which are converted to hours before using the builtin time.ParseDuration function.
func SetTagName ¶ added in v0.1.7
SetTagNAme sets the name of the tag used to define validation rules for fields. The default is "validate". If you need to change the tag string name, this must be called before using a function that reads a structure to create a validator.
func Split ¶ added in v0.1.8
Split a string into separate components, using a defined separator character. If the separator is not provided, the function defaults to a comma ",". The split ignores separators enclosed within single quotes or parentheses.
func UnMarshal ¶
UnMarshal is a helper function that combines JSON validation and conversion of the representation into the destination structure.
It will return errors if the validation tags on the structure definition are not correctly defines, if the JSON is not correctly formatted, or if the JSON contains fields or values that violate the validation rules.
This is not recommended for use when the same structure is going to be validate many times, as it does not cache the validator structure.
func ValidateByName ¶
ValidateByName validates a JSON string against a named validator. If the named validator is not found, it returns an error. If the JSON string is valid according to the named validator, it returns nil.
Types ¶
type Item ¶
type Item struct {
// The name of this item. This is usually the field name in a
// structure. For other types, this is an empty string.
Name string `json:"name,omitempty"`
// IF this validator is a reference to another validator (or even a
// references to the same validator), this will be the name of an
// alias object in the dictionary. When set, the only other value
// set in the validator is the ItemType value.
Alias string `json:"alias,omitempty"`
// The type of this item. This controls how the value in the JSON
// payload being validated is interpreted, and controls what validations
// are permitted for this item.
ItemType Type `json:"type,omitempty"`
// This is a list of the allowed values for this item. This is only
// used for string, integer, and the key values for map types. IF there
// are no enumerated values (enums), this will be an empty slice.
Enums []string `json:"enums,omitempty"`
// This is a list of the fields in the current structure. This is only
// used for struct types. This will be an empty slice if there are no fields.
Fields []*Item `json:"fields,omitempty"`
// IF this is a pointer to another validator or an array of validators, this
// is a pointer to the validator for the underlying type. If the current
// validator is not a pointer or array, this will be nil.
BaseType *Item `json:"base_type,omitempty"`
// If there is a rule specifying a minimum length for a string, array, or
// map key set, this will be the minimum length. The HasMinLength boolean
// will be true if this value is used.
MinLength int `json:"min_length,omitempty"`
// If there is a rule specifying a maximum length for a string, array, or
// map key set, this will be the maximum length. The HasMaxLength boolean
// will be true if this value is used.
MaxLength int `json:"max_length,omitempty"`
// If there is a rule specifying a minimum value for a numeric field, this
// will be the minimum value. The HasMinValue boolean will be true if
// this value is used.
MinValue any `json:"min_value,omitempty"`
// If there is a rule specifying a maximum value for a numeric field, this
// will be the maximum value. The HasMaxValue boolean will be true if
// this value is used.
MaxValue any `json:"max_value,omitempty"`
// If there is a rule specifying that this field must always be present
// in a json payload, this will be true.
Required bool `json:"required,omitempty"`
// If there is a rule specifying that the json can contain field names
// not explicitly defined in the validator, this will be true. The default
// is false, which means the json cannot contain field names not explicitly
// defined in the validator.
AllowForeignKey bool `json:"allow_foreign_key,omitempty"`
// If there is a minimum length specified for this item, this is true.
HasMinLength bool `json:"has_min_length,omitempty"`
// If there is a maximum length specified for this item, this is true.
HasMaxLength bool `json:"has_max_length,omitempty"`
// If there is a minimum value specified for this item, this is true.
HasMinValue bool `json:"has_min_value,omitempty"`
// If there is a maximum value specified for this item, this is true.
HasMaxValue bool `json:"has_max_value,omitempty"`
// IF there is a rule specifying enumerated values, this is true when
// the values are case-sensitive. By default, string values are not
// case-sensitive.
CaseSensitive bool `json:"case_sensitive,omitempty"`
}
Define an individual validator. This structure is used for any structure, field, map, array, or individual value. It contains flags representing all the validation rules defined for this item.
IF any of these json tag names are changed, the corresponding field in the checkFields() function will need to be updated.
func New ¶
New accepts any value and returns a new validator for it. If the value is a structure, the validator will contain any rules specified in structure tags within the structure definition. Additional validation rules can be defined by calling Parse() with a tag string on the validator.
func NewJSON ¶ added in v0.1.3
NewJSON converts a JSON byte array into a validator item. IF the JSON did not contain a valid validator item, it will return an error.
func NewType ¶ added in v0.1.3
NewType creates a new validator with the given type. There is no other information stored in the validator, so the caller should use ParseTag() or the explicit rule creation methods to add rules to the validator.
func (*Item) AddField ¶ added in v0.1.3
AddField adds a field validator to an existing structure validator. The field validator must have been previously completely defined (you cannot update a field after it is defined in the structure). The field is appended to the list of existing fields.
func (*Item) Copy ¶ added in v0.1.5
Copy creates a deep copy of the Item structure. The copied structure is completely independent of the original structure.
func (*Item) ParseTag ¶
ParseTag updates an existing validator to reflect the rules specified in the tag string. This tag string is normally extracted from struct field tags automatically when a new validator is created, but can also be used to set validator rules on a non-struct validator type.
func (*Item) SetEnums ¶ added in v0.1.3
SetEnums sets the allowed values for this item. The allowed values can be any string, integer, or any other type that can be converted to a string. This test can be applied to a string value, an array or strings or integers, or a string list where the values are comma-separated.
func (*Item) SetField ¶ added in v0.1.3
SetField adds a field validator to an existing structure validator. The field validator must have been previously completely defined (you cannot update a field after it is defined in the structure). The index is the zero-based index of the field in the structure.
IF the validator is not for a structure or the index is negative, no change is made to the item.
func (*Item) SetForeignKeys ¶ added in v0.1.3
SetForeignKeys sets the allow foreign key flag. By default, foreign keys (field names not defined in the validator) are not allowed. If the validator should instead ignore key values not defined in the validator, set this flag to true.
func (*Item) SetMatchCase ¶ added in v0.1.3
SetMatchCase sets the match case flag. By default, enumerated values are not case-sensitive when they are string values. If these are meant to be case-sensitive, set this flag to true.
func (*Item) SetMaxLength ¶ added in v0.1.3
SetMaxLength sets the maximum allowed length for this item. The maximum length can be any integer value. This applies to string length, array length, or number of map keys.
func (*Item) SetMaxValue ¶ added in v0.1.3
SetMaxValue sets the maximum allowed value for this item. The maximum value can be any numeric value.
func (*Item) SetMinLength ¶ added in v0.1.3
SetMinLength sets the minimum allowed length for this item. The minimum length can be any integer value. This applies to string length, array length, or number of map keys.
func (*Item) SetMinValue ¶ added in v0.1.3
SetMinValue sets the minimum allowed value for this item. The minimum value can be any numeric value.
func (*Item) SetName ¶ added in v0.1.3
SetName sets the field name for this item. The field name defaults to the field name in the struct tag. If the struct tag does not have a name field, it looks to see if there is a json tag and uses the name from that tag. Finally, it assumes the field name is the same as the variable name in the structure. This function can be used to override any of these defaults.
func (*Item) SetRequired ¶ added in v0.1.7
SetRequired sets whether this item is required or not. By default, a json payload does not have to explicitly specify fields. If the field must be present in the JSON, then set this to true.
func (*Item) String ¶ added in v0.1.3
String converts a validator item into a JSON-formatted string.
func (*Item) Validate ¶
For a given validator, determine if the provided JSON string is valid according to the rules for the given validator. If the JSON is not correctly formed or if a validation rule is violated, an error is returned. IF the provided JSON includes recursive or nested values that exceed the maximum recursion depth, an error is returned. If the JSON string is valid, it returns nil.
type Type ¶
type Type int
const ( TypeInvalid Type = iota TypeString TypeInt TypeFloat TypeBool TypeStruct TypeArray TypePointer TypeMap // Any map type falls in this bucket. Only validations are for key values TypeUUID TypeTime TypeAny TypeList TypeDuration )
Enumerate all possible types that an item/field can have. Always add to the end to avoid breaking any existing JSON representation of a validator.
func TypeFromString ¶ added in v0.1.9
For a given type name, return the corresponding Type value. If the name is not recognized, return TypeInvalid and an error.
type ValidationError ¶
type ValidationError struct {
// contains filtered or unexported fields
}
ValidationError represents a validation error. This includes the original error, the context of the validation (e.g., the field name), the actual value, and the expected values. If any of context, value, or expected values are empty, they are not included in the formatted error message string.
func NewError ¶
func NewError(msg string) *ValidationError
Create a new validation error with the given message.
func (*ValidationError) Context ¶
func (e *ValidationError) Context(context string) *ValidationError
Context adds a context value to the validation error. This returns the validation error with the updated context as a new validation error.
func (*ValidationError) Error ¶
func (e *ValidationError) Error() string
Error returns the formatted error message string. Supporting this function makes validation errors match the error interface in Go. The error is formatted with the original error message, context, value, and expected values. If any of context, value, or expected values are empty, they are not included in the formatted error message string.
func (*ValidationError) Expected ¶
func (e *ValidationError) Expected(expected ...any) *ValidationError
Expected adds expected values to the validation error. This is used when an enumeration rule is violated. The expected values can be a list of values, a slice of values, or a single value. This returns a copy of the validation error with the updated expected values as a new validation error.
func (*ValidationError) Value ¶
func (e *ValidationError) Value(value any) *ValidationError
Value adds a value to the validation error. This returns the validation error with the updated value as a new validation error. If the value is nil, the value field is not included in the formatted error message.