Documentation
¶
Index ¶
- Constants
- func Date(input any) (any, error)
- func DateTime(input any) (any, error)
- func Email(input any) (any, error)
- func JSON(input any) (any, error)
- func Latitude(input any) (any, error)
- func Longitude(input any) (any, error)
- func NonEmptyString(input any) (any, error)
- func Password(input any) (any, error)
- func Time(input any) (any, error)
- func URL(input any) (any, error)
- func UUID(input any) (any, error)
- func Validate(validations []Validation) error
- type Issue
- type IssueDTO
- type Rule
- func DateAfter(target time.Time, inclusive bool) Rule
- func DateBefore(target time.Time, inclusive bool) Rule
- func DateEqual(target time.Time) Rule
- func Enum(enumValues ...string) Rule
- func Equals(targetName string, targetValue any) Rule
- func GreaterThan(target any) Rule
- func HasPrefix(prefix string) Rule
- func HasSuffix(suffix string) Rule
- func Length(length int) Rule
- func LessThan(target any) Rule
- func Max(target any) Rule
- func Min(target any) Rule
- func NotHasPrefix(prefix string) Rule
- func NotHasSuffix(suffix string) Rule
- func Regexp(pattern string) Rule
- type Validation
- type ValidationErrors
Constants ¶
const ( PATTERN_EMAIL = `^[^@]+@[^@]+\.[^@]+$` PATTERN_UUID = `^[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}$` // Password strength rules: // - Minimum eight characters // - At least one uppercase letter // - One lowercase letter // - One number // - One special character // **Note**: the pattern will match weak passwords instead of strong passwords PATTERN_PASSWORD_STRENGTH = `^(.{0,7}|[^0-9]*|[^A-Z]*|[^a-z]*|[a-zA-Z0-9]*)$` )
const ( CODE_UNKNOWN = "unknown" CODE_NON_EMPTY_STRING = "non-empty-string" CODE_LENGTH = "length" CODE_MIN = "min" CODE_MAX = "max" CODE_LESS_THAN = "less-than" CODE_EMAIL = "email" CODE_HAS_PREFIX = "has-prefix" CODE_HAS_SUFFIX = "has-suffix" CODE_NOT_HAS_PREFIX = "not-has-prefix" CODE_NOT_HAS_SUFFIX = "not-has-suffix" CODE_EQUALS = "equals" CODE_ENUM = "enum" CODE_URL = "url" CODE_REGEXP = "regexp" CODE_UUID = "uuid" CODE_PASSWORD = "password" CODE_JSON = "json" CODE_DATE_TIME = "date-time" CODE_DATE = "date" CODE_TIME = "time" CODE_DATE_EQUAL = "date-equal" // TODO: merge with `Equals` CODE_DATE_BEFORE = "date-before" // TODO: merge with `LessThan` CODE_DATE_AFTER = "date-after" // TODO: merge with `GreaterThan` CODE_LATITUDE = "latitude" CODE_LONGITUDE = "longitude" )
Variables ¶
This section is empty.
Functions ¶
func Date ¶
Date check if the provided input is a valid date-only string. Date string must be in format e.g. 2023-10-05 [Link](https://pkg.go.dev/time#pkg-constants).
func DateTime ¶
DateTime check if the provided input is a valid string and a valid ISO timestamp according to RFC3339: [Link](https://pkg.go.dev/time#pkg-constants).
func NonEmptyString ¶
NonEmptyString check if provided input is a non-empty string.
func Password ¶
Password check if the provided input is a valid string and a reasonably strong password.
func Time ¶
Time check if the provided input is a valid string and a valid time-only string. Time string must be in 24-hours format: e.g. 10:20:00 [Link](https://pkg.go.dev/time#pkg-constants).
func Validate ¶
func Validate(validations []Validation) error
Types ¶
type IssueDTO ¶
type IssueDTO struct {
Code string `json:"code"`
Message string `json:"message"`
Value any `json:"value"`
}
The `Issue` struct implements the error interface, which makes it tricky to serialize. This `IssueDTO` struct is required because we do need serialization of the issues.
type Rule ¶
func DateAfter ¶
DateAfter check if the provided input is a date after the target date. If inclusive is set to true, target date will be included. TODO: merge into GreaterThan
func DateBefore ¶
DateBefore check if the provided input is a date before (but not equal) to the target date. TODO: merge into LessThan
func Enum ¶
Enum check if the provided input matches any of the listed enumerations values TODO: allow numeric enums has well
func Equals ¶
Equals check if the provided input is the same as the required input. TODO: merge DateEquals into this TODO: extend to allow comparison of numbers
func GreaterThan ¶
GreaterThan if the provided number is an int / float(64), check input is more than (but not equal) to the target. If the provided input is a string, check its length is more than (but not equal) to the target.
func HasPrefix ¶
HasPrefix check if the provided input is a valid string and starts with the provided substring.
func HasSuffix ¶
HasSuffix check if the provided input is a valid string and ends with the provided substring.
func Length ¶
Length check if the provided input is a string and its length is equal to the provided length.
func LessThan ¶
LessThan if the provided number is an int / float(64), check input is less than (but not equal) to the target. If the provided input is a string, check its length is less than (but not equal) to the target.
func Max ¶
Max if the provided number is an int / float(64), check input is less than or equal to the target. If the provided input is a string, check its length is less than or equal to the target.
func Min ¶
Min if the provided number is an int / float(64), check input is greater than or equal to the target. If the provided input is a string, check its length is more than or equal to the target.
func NotHasPrefix ¶
NotHasPrefix check if the provided input is a valid string and doesn't starts with the provided substring.
func NotHasSuffix ¶
NotHasSuffix check if the provided input is a valid string and ends with the provided substring.
type Validation ¶
type ValidationErrors ¶
func (ValidationErrors) Error ¶
func (v ValidationErrors) Error() string