forms

package
v0.0.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2024 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package forms implements utilities to parse and validate data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CheckFunc

type CheckFunc func(Field) error

CheckFunc is a function which validates a Field.

func BoolRequired

func BoolRequired() CheckFunc

Checks that a bool is not blank.

func DecBtw

func DecBtw(n, m string) CheckFunc

Checks that a decimal is between n and m.

func DecGt

func DecGt(n string) CheckFunc

Checks that a decimal is more than n.

func DecGte

func DecGte(n string) CheckFunc

Checks that a decimal is greater than or equal to n.

func DecLt

func DecLt(n string) CheckFunc

Checks that a decimal is less than n.

func DecLte

func DecLte(n string) CheckFunc

Checks that a decimal is less than or equal to n.

func DecRequired

func DecRequired() CheckFunc

Checks that a decimal is not blank.

func DtAfter

func DtAfter(n string) CheckFunc

Checks that a date is after n (yyyy-mm-dd).

func DtBefore

func DtBefore(n string) CheckFunc

Checks that a date is before n (yyyy-mm-dd).

func DtInFuture

func DtInFuture() CheckFunc

Checks that a date is in the future.

func DtInPast

func DtInPast() CheckFunc

Checks that a date is in the past.

func DtRequired

func DtRequired() CheckFunc

Checks that a date is not blank.

func FltBtw

func FltBtw(n, m float64) CheckFunc

Checks that a float is between n and m.

func FltGt

func FltGt(n float64) CheckFunc

Checks that a float is more than n.

func FltGte

func FltGte(n float64) CheckFunc

Checks that a float is more than or equal to n.

func FltIn

func FltIn(choices []float64) CheckFunc

Checks that a float is a member of the given choices.

func FltLt

func FltLt(n float64) CheckFunc

Checks that a float is less than n.

func FltLte

func FltLte(n float64) CheckFunc

Checks that a float is less than or equal to n.

func FltRequired

func FltRequired() CheckFunc

Checks that a float is not blank.

func IntBtw

func IntBtw(n, m int) CheckFunc

Checks that an int is between n and m.

func IntGt

func IntGt(n int) CheckFunc

Checks that an int is more than n.

func IntGte

func IntGte(n int) CheckFunc

Checks that an int is more than or equal to n.

func IntIn

func IntIn(choices []int) CheckFunc

Checks that an int is a member of the given choices.

func IntLt

func IntLt(n int) CheckFunc

Checks that an int is less than n.

func IntLte

func IntLte(n int) CheckFunc

Checks that an int is less than or equal to n.

func IntRequired

func IntRequired() CheckFunc

Checks that an int is not blank.

func StrBtw

func StrBtw(n, m int) CheckFunc

Checks that a string's length is between n and m.

func StrEmail

func StrEmail() CheckFunc

Checks that a string is an email.

func StrGt

func StrGt(n int) CheckFunc

Checks that a string's length is greater than n.

func StrGte

func StrGte(n int) CheckFunc

Checks that a string's length is greater than or equal to n.

func StrIn

func StrIn(choices []string) CheckFunc

Checks that a string is a member of the given choices.

func StrLt

func StrLt(n int) CheckFunc

Checks that a string's length is less than n.

func StrLte

func StrLte(n int) CheckFunc

Checks that a string's length is less than or equal to n.

func StrMatches

func StrMatches(rx *regexp.Regexp, errMsg string) CheckFunc

Checks that a string matches the given regex.

func StrRequired

func StrRequired() CheckFunc

Checks that a string is not blank.

func StrUrl

func StrUrl() CheckFunc

Checks that a string is a URL.

type Field

type Field struct {
	String  string
	Integer int
	Float   float64
	Bool    bool
	Decimal big.Rat
	Date    time.Time
	// contains filtered or unexported fields
}

Field represents a parsed value.

func (Field) Err added in v0.0.8

func (f Field) Err() error

Err returns the error associated with the field's parsing or validation.

func (Field) IsBlank added in v0.0.8

func (f Field) IsBlank() bool

IsBlank returns true if the field's original value is empty. This is used to distinguish between the field's zero value and if the field is empty.

func (Field) Value added in v0.0.8

func (f Field) Value() string

Value returns the field's original value.

type Form

type Form struct {
	// contains filtered or unexported fields
}

Form is a type which parses and validates form data.

Values can be parsed into a desired type, like an int, date, bool, etc. Validation functions can also be provided, which ensures that the parsed type is properly vetted before being retrieved.

func New

func New() *Form

New constructs and returns a Form.

func (*Form) CleanBool

func (f *Form) CleanBool(name, value string, funcs ...CheckFunc)

CleanBool cleans the given value as a bool.

func (*Form) CleanDate

func (f *Form) CleanDate(name, value string, funcs ...CheckFunc)

CleanDate cleans the given value as a date.

func (*Form) CleanDecimal

func (f *Form) CleanDecimal(name, value string, funcs ...CheckFunc)

CleanDecimal cleans the given value as a decimal.

func (*Form) CleanExtra added in v0.0.6

func (f *Form) CleanExtra(cond bool, err error)

CleanExtra adds the error to the extra errors list if the condition is true.

func (*Form) CleanFloat

func (f *Form) CleanFloat(name, value string, funcs ...CheckFunc)

CleanFloat cleans the given value as a float.

func (*Form) CleanInteger

func (f *Form) CleanInteger(name, value string, funcs ...CheckFunc)

CleanInteger cleans the given value as a integer.

func (*Form) CleanString

func (f *Form) CleanString(name, value string, funcs ...CheckFunc)

CleanString cleans the given value as a string.

func (*Form) CleanedBool

func (f *Form) CleanedBool(name string) bool

CleanedBool retrieves the named field as a bool.

func (*Form) CleanedDate

func (f *Form) CleanedDate(name string) time.Time

CleanedDate retrieves the named field as a date.

func (*Form) CleanedDecimal

func (f *Form) CleanedDecimal(name string) big.Rat

CleanedDecimal retrieves the named field as a decimal.

func (*Form) CleanedFloat

func (f *Form) CleanedFloat(name string) float64

CleanedFloat retrieves the named field as a float.

func (*Form) CleanedInteger

func (f *Form) CleanedInteger(name string) int

CleanedInteger retrieves the named field as an integer.

func (*Form) CleanedString

func (f *Form) CleanedString(name string) string

CleanedString retrieves the named field as a string.

func (*Form) ExtraErrors added in v0.0.6

func (f *Form) ExtraErrors() []error

ExtraErrors returns the extra errors slice.

func (*Form) Field

func (f *Form) Field(name string) (Field, bool)

Field returns the Field mapped to the given name.

func (*Form) HasError

func (f *Form) HasError(target any) bool

HasError returns true if the errors map contains the target error.

func (*Form) IsValid

func (f *Form) IsValid() bool

IsValid returns true if there are no field errors and no extra errors.

func (*Form) MustField

func (f *Form) MustField(name string) Field

MustField returns the desired Field and panics if it does not exist.

func (*Form) Names added in v0.0.8

func (f *Form) Names() []string

Names returns the field names.

type ParseError

type ParseError struct {
	Msg string
}

ParseError represents when a value fails to parse into a specific type.

func (ParseError) Error

func (p ParseError) Error() string

func (ParseError) String

func (p ParseError) String() string

type ParseFunc

type ParseFunc func(string) Field

ParseFunc is a function which parses a value into the desired type, as a Field.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL