123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- package form
- import (
- "context"
- "errors"
- "fmt"
- "strings"
- )
- /* validation errors */
- var (
- EInvalidInteger = errors.New("not a valid integer value")
- EInvalidFloat = errors.New("not a valid float value")
- ERequired = errors.New("this field is required")
- )
- /* ValidLettersGeneric is a validator generator for checking for valid letters in field */
- func ValidLettersGeneric(Letters string, Error error) ValidatorFunc {
- Callback := func(field FormField, ctx context.Context) error {
- for _, Rune := range field.GetString() {
- if strings.IndexRune(Letters, Rune) == -1 {
- return Error
- }
- }
- return nil
- }
- return Callback
- }
- /* ValidRequired makes sure field is not empty. */
- func ValidRequired(field FormField, ctx context.Context) error {
- if field.GetString() == "" {
- return ERequired
- }
- return nil
- }
- /* ValidLength makes sure that a string length is between specified min and max values. */
- func ValidLength(min, max int) ValidatorFunc {
- var ELength = errors.New(
- fmt.Sprintf("must be a string between %d and %d characters in length", min, max))
- return func(field FormField, ctx context.Context) error {
- if len(field.GetString()) != 0 && (len(field.GetString()) < min || len(field.GetString()) > max) {
- return ELength
- }
- return nil
- }
- }
- /* ValidFieldIn verifies if item is within the list of items */
- func ValidFieldIn(list []string) ValidatorFunc {
- var EInvalidValue = errors.New(
- fmt.Sprintf(
- "field value must be one of: %s",
- strings.Join(list, ","),
- ),
- )
- return func(field FormField, ctx context.Context) error {
- for _, item := range list {
- if item == field.GetString() {
- return nil
- }
- }
- return EInvalidValue
- }
- }
- /* ValidInt returns error if field does not contain a valid integer value */
- func ValidInt(field FormField, ctx context.Context) error {
- _, err := field.GetInt()
- if err != nil {
- return EInvalidInteger
- }
- return nil
- }
- /* ValidBetween makes sure that field is integer value within the specified range. */
- func ValidBetween(min, max int) ValidatorFunc {
- var EInvalidInterval = errors.New(
- fmt.Sprintf("must be integer between %d and %d", min, max))
- return func(field FormField, ctx context.Context) error {
- value, err := field.GetInt()
- if err != nil {
- return EInvalidInteger
- }
- if value < min || value > max {
- return EInvalidInterval
- }
- return nil
- }
- }
- /* ValidFloat returns error if field does not contain a valid integer value */
- func ValidFloat(field FormField, ctx context.Context) error {
- _, err := field.GetFloat()
- if err != nil {
- return EInvalidFloat
- }
- return nil
- }
- /* ValidBetweenFloat32 makes sure field is float64 value within the specified range. */
- func ValidBetweenFloat(min, max float64) ValidatorFunc {
- var EInvalidInterval = errors.New(
- fmt.Sprintf("must be float value between %.2f and %.2f", min, max))
- return func(field FormField, ctx context.Context) error {
- value, err := field.GetFloat()
- if err != nil {
- return err
- }
- if value < min || value > max {
- return EInvalidInterval
- }
- return nil
- }
- }
- /* ValidFieldEqualTo is a validator that checks if two fields have the same value. */
- func ValidFieldEqualTo(Other *FormField, err error) ValidatorFunc {
- return func(field FormField, ctx context.Context) error {
- if field.GetString() != Other.GetString() {
- return err
- }
- return nil
- }
- }
|