Documentation
¶
Index ¶
- Variables
- func Email(ctx context.Context, val any) error
- func Flash(s *sessions.Session, f Form, errs Errors)
- func Old(sess *sessions.Session, f Form, errs Errors)
- func Required(_ context.Context, val any) error
- func Unmarshal(r *http.Request, f Form, opts ...Option) error
- func UnmarshalAndValidate(r *http.Request, f Form, opts ...Option) error
- type Converter
- type DecodeError
- type Decoder
- type Errors
- func (e Errors) Add(field string, err error)
- func (e Errors) Error() string
- func (e Errors) First(field string) error
- func (e Errors) Get(field string) []error
- func (e Errors) Is(target error) bool
- func (e Errors) MarshalJSON() ([]byte, error)
- func (e Errors) Merge(errs Errors)
- func (e Errors) UnmarshalJSON(b []byte) error
- type FieldEqualsError
- type Fields
- type File
- type Form
- type LengthError
- type MatchError
- type Option
- type Options
- type Rule
- type Validator
Constants ¶
This section is empty.
Variables ¶
var ErrEmailInvalid = errors.New("not a valid address")
var ErrFieldRequired = errors.New("field is required")
Functions ¶
func Email ¶
Email checks to see if the given value is an email address. This does a very simple pattern match to check for the presence of "@" in the string. Further validation of the email address itself should be done through the sending of a verification email.
func Flash ¶
Flash stores the given Form and Errors to the given session, which can then be retrieved via a subsequent call to Old.
func Old ¶
Old unmarshals the previous Form and Errors that could be found in the given session. If found, they will be deleted from the given session.
func Required ¶
Required checks to see if the given value was provided. If the value is a string, or returns a string via a call to String, then it will determine presence by whether or not that value is empty. If the value is a pointer then it is checked for nilness.
func Unmarshal ¶
Unmarshal parses the given request and unmarshals the data into the given form. If the given request has the Content-Type of application/json, then it will be decoded as such. If the given request has the Content-Type of multipart/form-data, then [net.http.Request.ParseMultipartForm] is called, otherwise [net.http.Request.ParseForm] is called.
Types ¶
type DecodeError ¶
func (DecodeError) Error ¶
func (e DecodeError) Error() string
type Errors ¶
Errors contains the errors that occurred during the validation of a form. Each key in the map will be the field for which the errors occurred.
func (Errors) First ¶
First returns the first error that occurred for the named field. This returns nil if there is no error.
func (Errors) Get ¶
Get the errors that occurred for the named field. This returns nil if there are no errors.
func (Errors) MarshalJSON ¶
func (Errors) Merge ¶
Merge the given errors into the underlying set of errors. This will not merge in duplicates. That is, if an error exists in the given set being merged, and also exists in the underlying set, then it will not be added.
func (Errors) UnmarshalJSON ¶
type FieldEqualsError ¶
type FieldEqualsError string
func (FieldEqualsError) Error ¶
func (e FieldEqualsError) Error() string
type Fields ¶
Fields represents the fields in the form. This is the value that is stored in the session data when a form is flashed to the session. This should not contain sensitive data, such as passwords.
type File ¶
File is a wrapper around the underlying [mime.multipart.File] interface. This also implements the [io.fs.FileInfo] interface. This represents a multipart file that has been sent in a request.
func (*File) MarshalJSON ¶
type Form ¶
Form represents a form into which request data has been unmarshalled into. It wraps the Fields and Validate methods.
Fields returns the Fields type which should contain the fields of the form. This value is used for flashing data into the session.
Validate validates the form data itself. This should return the Errors type containing the errors, if any, that occurred during validation and for which field.
type LengthError ¶
func (LengthError) Error ¶
func (e LengthError) Error() string
func (LengthError) Is ¶
func (e LengthError) Is(target error) bool
type MatchError ¶
func (MatchError) Error ¶
func (e MatchError) Error() string
func (MatchError) Is ¶
func (e MatchError) Is(target error) bool
type Option ¶
type Option func(opts *Options)
func AliasTag ¶
AliasTag specifies the struct tag alias that should be used for mapping data to the struct fields. The default for this is "schema".
func IgnoreUnknownKeys ¶
func IgnoreUnknownKeys() Option
IgnoreUnknownKeys will ignore any keys that exist in the request data but do not exist in the struct being decoded into.
func RegisterConverter ¶
RegisterConverter registers a converter function for a custom type.
type Options ¶
type Options struct {
AliasTag string
IgnoreUnknownKeys bool
MaxSize int
Converters map[any]Converter
ZeroEmpty bool
}
Options are the decoder options that will be configured on the Decoder when decoding the request data. This will be configured via the Option function.
type Rule ¶
Rule is used for validating field values in a form.
func Equals ¶
Equals returns a Rule that checks to ensure the form value matches the given expected value. The given field is the name of the other form field the form value should match. This field value is returned as the FieldEqualsError type.
func Length ¶
Length returns a Rule that checks to ensure the form value is between the min and max length in size. If the form value is not of type string then this errors. If the value falls outside the given bounds then the error type of LengthError is returned.