okay

package module
v0.0.0-...-d2cfcf4 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2022 License: MIT Imports: 5 Imported by: 0

README

Okay

Simple validation library for Go.

type UserRegistrationPayload struct {
   Username, Email, Password, City string
   Age uint8
}

func (u UserRegistrationPayload) Okay() (ValidationErrors, error) {
   o := okay.New()

   o.Text(u.Username, "username").Required().MinLength(6)
   o.Text(u.Email, "email").Required().IsEmail().DoesNotEndWith(".edu")
   o.Text(u.City, "city").Required().Is("London")
   ...

   return o.Errors()
}

func (h *Handler) Register(w http.ResponseWriter, r *http.Request) {
   var u UserRegistrationPayload
   json.NewEncoder(r.Body).Encode(&u)
   validationErrs, err := okay.Validate(u) // calls u.Okay, under the hood
   if err != nil {
   	// unexpected error
   	// should probably give an internal server error
   }
   if len(validationErrs) > 0 {
   	// set headers 
   	// send the errors
   	// ...
   }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type O

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

func New

func New() *O

func (*O) Errors

func (o *O) Errors() (ValidationErrors, error)

func (*O) Text

func (o *O) Text(val string, fieldName string) *TextValue

a validator function to validate inputs of type string (aka text)

type TextValue

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

func (*TextValue) Contains

func (v *TextValue) Contains(str string) *TextValue

func (*TextValue) DoesNotEndWith

func (v *TextValue) DoesNotEndWith(str string) *TextValue

func (*TextValue) DoesNotStartWith

func (v *TextValue) DoesNotStartWith(str string) *TextValue

func (*TextValue) EndsWith

func (v *TextValue) EndsWith(str string) *TextValue

func (*TextValue) Is

func (v *TextValue) Is(compare string) *TextValue

text value is "compare". (string equality)

func (*TextValue) IsAlpha

func (v *TextValue) IsAlpha() *TextValue

func (*TextValue) IsAlphanumeric

func (v *TextValue) IsAlphanumeric() *TextValue

func (*TextValue) IsEmail

func (v *TextValue) IsEmail() *TextValue

func (*TextValue) IsIPv4

func (v *TextValue) IsIPv4() *TextValue

func (*TextValue) IsIPv6

func (v *TextValue) IsIPv6() *TextValue

func (*TextValue) IsOnlyDigits

func (v *TextValue) IsOnlyDigits() *TextValue

func (*TextValue) MaxLength

func (v *TextValue) MaxLength(length uint) *TextValue

func (*TextValue) MinLength

func (v *TextValue) MinLength(length uint) *TextValue

Set minimum length value for value.

func (*TextValue) Required

func (v *TextValue) Required() *TextValue

Value is required. The value cannot have a zero value.

func (*TextValue) StartsWith

func (v *TextValue) StartsWith(str string) *TextValue

type ValidationErrors

type ValidationErrors = []string

func Validate

func Validate(input Validator) (ValidationErrors, error)

type Validator

type Validator interface {
	Okay() (ValidationErrors, error)
}

An interface for structs that are going to be validated using okay.Validate, to implement.

Jump to

Keyboard shortcuts

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