formspec

package module
v0.0.0-...-4dbb965 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2015 License: BSD-3-Clause Imports: 5 Imported by: 1

README

go-formspec

Build Status

Package github.com/ToQoz/formspec validates a form. So it will expresses spec for form. This is generally used in http.Handler, but you can use *formspec.Result as a return value of your validation func in models.

Requirement

  • go1.1 or later

Documentation

Overview

Package github.com/ToQoz/formspec validates a form. So it will expresses **spec** for form. This is generally used in http.Handler, but you can use *formspec.Result as a retuen value of your validation func in models.

ExampleApp: https://github.com/ToQoz/go-formspec/tree/master/_example

Simple usage in http.Handler.

package main

import (
	"fmt"
	"github.com/ToQoz/go-formspec"
	"log"
	"net/http"
)

func main() {
	s := formspec.New()
	s.Rule("name", formspec.RuleRequired())
	s.Rule("age", formspec.RuleInt()).Message("must be integer. ok?").AllowBlank()
	s.Rule("nick", formspec.RuleRequired()).FullMessage("Please enter your cool nickname.")

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		vr := s.Validate(r)

		if !vr.Ok {
			w.WriteHeader(403)

			for _, verr := range vr.Errors {
				w.Write([]byte(fmt.Sprintf("Validation error in %s. %s\n", verr.Field, verr.Message)))
			}

			return
		}

		w.Write([]byte("ok"))
	})

	log.Fatal(http.ListenAndServe(":8888", nil))
}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (

	// regexp for number format. You change override.
	RuleFormatNumber = regexp.MustCompile(`\A[+-]?\d+\.?\d*\z`)
	// regexp for integer format. You change override.
	RuleFormatInt = regexp.MustCompile(`\A[+-]?\d+\z`)

	RuleMessageRequired    = "is required."
	RuleMessageMaxLen      = "is too long. Max is %d character."
	RuleMessageMinLen      = "is too short. Min is %d character."
	RuleInvalidMessage     = "is invalid."
	RuleMessageNumber      = "must be number."
	RuleMessageInt         = "must be integer."
	RuleMessageLessThan    = "must be less than %d"
	RuleMessageGreaterThan = "must be greater than %d"
)

Functions

This section is empty.

Types

type Error

type Error struct {
	Field   string `json:"field"`
	Message string `json:"message"`
}

func NewError

func NewError(field, message string) *Error

func (*Error) Error

func (e *Error) Error() string

type FilterFunc

type FilterFunc func(string) string

type Form

type Form interface {
	FormValue(string) string
}

type Formspec

type Formspec struct {
	Rules []*Rule
}
Example (Basic)
package main

import (
	"fmt"
	"github.com/ToQoz/go-formspec"
)

type exampleForm struct {
	form map[string]string
}

func (e *exampleForm) Set(key, value string) {
	e.form[key] = value
}

func (e *exampleForm) FormValue(value string) string {
	return ""
}

func main() {
	aFormspec := formspec.New()
	aFormspec.Rule("name", formspec.RuleRequired())
	aFormspec.Rule("age", formspec.RuleInt()).Message("must be integer. ok?").AllowBlank()
	aFormspec.Rule("nick", formspec.RuleRequired()).FullMessage("Please enter your cool nick.")

	f := &exampleForm{}

	// f.Set("name", "ToQoz")
	f.Set("age", "invalid int")
	// f.Set("age", "22")
	// f.Set("nick", "Toqoz")

	vr := aFormspec.Validate(f)

	if !vr.Ok {
		for _, verr := range vr.Errors {
			fmt.Printf("Validation error in %s. Message is %s.\n", verr.Field, verr.Message)
		}
	}
}
Output:

func New

func New() *Formspec

func (*Formspec) Clone

func (f *Formspec) Clone() *Formspec

func (*Formspec) Rule

func (f *Formspec) Rule(field string, ruleFunc RuleFunc) *Rule

func (*Formspec) Validate

func (f *Formspec) Validate(form Form) *Result

type Result

type Result struct {
	Ok     bool     `json:"-"`
	Errors []*Error `json:"errors"`
}

func NewNgResult

func NewNgResult() *Result

func NewOkResult

func NewOkResult() *Result

type Rule

type Rule struct {
	Field       string
	RuleFunc    RuleFunc
	FilterFuncs []FilterFunc
	// contains filtered or unexported fields
}

func (*Rule) AllowBlank

func (r *Rule) AllowBlank() *Rule

func (*Rule) Call

func (r *Rule) Call(f Form) error

func (*Rule) Filter

func (r *Rule) Filter(filterFunc FilterFunc) *Rule

func (*Rule) FullMessage

func (r *Rule) FullMessage(m string) *Rule

FullMessage sets Rule.fullMessage.

func (*Rule) Message

func (r *Rule) Message(m string) *Rule

Message sets Rule.message.

type RuleFunc

type RuleFunc func(value string, f Form) error

func RuleFloatGreaterThan

func RuleFloatGreaterThan(a float64) RuleFunc

func RuleFloatLessThan

func RuleFloatLessThan(a float64) RuleFunc

func RuleFormat

func RuleFormat(r *regexp.Regexp) RuleFunc

func RuleInt

func RuleInt() RuleFunc

func RuleIntGreaterThan

func RuleIntGreaterThan(a int) RuleFunc

func RuleIntLessThan

func RuleIntLessThan(a float64) RuleFunc

func RuleMaxLen

func RuleMaxLen(maxLen int) RuleFunc

func RuleMinLen

func RuleMinLen(minLen int) RuleFunc

func RuleNumber

func RuleNumber() RuleFunc

func RuleRequired

func RuleRequired() RuleFunc

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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