formrules

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package formrules parses validation tags and checks submitted form values against them. kit uses it at run time; codegen uses it to write the same rules for the browser, where packages/tern/src/runtime/validate.ts applies them with the same messages. testdata/cases.json keeps the two in step.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check

func Check(f Field, values []string, other func(name string) string, labelOf func(name string) string, t func(key string, params map[string]any) string) string

Check returns why the posted values of f are invalid, or "". values are the field's posted values; other returns another field's first value, for same=. t translates Tern's texts (i18n.Catalog.TB); nil uses English.

func Humanize

func Humanize(name string) string

Humanize turns a field name into a label: "firstName" and "first_name" become "First name".

func ParseTime

func ParseTime(kind Kind, s string) (time.Time, bool)

ParseTime reads a date, or a date and time, as forms post them.

func Truthy

func Truthy(s string) bool

Truthy reports whether a checkbox's value means checked.

Types

type Field

type Field struct {
	Name     string `json:"name"`
	Label    string `json:"label"`
	Kind     Kind   `json:"kind"`
	Required bool   `json:"required,omitempty"`
	Rules    []Rule `json:"rules,omitempty"`
	// Message replaces every failure message for the field.
	Message string `json:"message,omitempty"`
	// Secret fields, such as passwords, are never sent back to refill a form.
	Secret bool `json:"secret,omitempty"`
	// contains filtered or unexported fields
}

Field is a form field's rules.

func FromReflect

func FromReflect(t reflect.Type) ([]Field, error)

FromReflect reads the fields of a struct type for decoding and checking.

func FromTypes

func FromTypes(t types.Type) ([]Field, error)

FromTypes reads the fields of a struct type as go/types sees it, for codegen.

func (*Field) Index

func (f *Field) Index() []int

Index is the field's position in its struct, for reflect.Value.FieldByIndex.

type Kind

type Kind string

Kind is how a field's text is read.

const (
	String   Kind = "string"
	Int      Kind = "int"
	Float    Kind = "float"
	Bool     Kind = "bool"
	Date     Kind = "date"     // YYYY-MM-DD
	DateTime Kind = "datetime" // YYYY-MM-DDTHH:MM, as datetime-local inputs post
	Strings  Kind = "strings"  // several values, such as checkboxes with one name
)

The kinds of field, from the Go type: text, whole and decimal numbers, checkboxes, dates and lists.

type Rule

type Rule struct {
	Name string `json:"name"`
	Arg  string `json:"arg,omitempty"`
}

Rule is one check from a validate tag, such as min=3.

func ParseTag

func ParseTag(tag string) (required bool, rules []Rule, err error)

ParseTag reads a validate tag: comma-separated rules, where pattern= takes the rest of the tag so its regular expression may contain commas.

Jump to

Keyboard shortcuts

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