Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/url"
"testing"
"time"
)

var (
Expand Down Expand Up @@ -133,3 +134,41 @@ func TestConvertersFloat64Converter(t *testing.T) {
t.Fatalf("Expected %v. Got %v", 3.14159, ex)
}
}

func TestConvertersTimeConverter(t *testing.T) {
f := &Form{
Fields: []Field{
{Name: "foo", Converter: TimeConverter},
},
}
res := f.Load(create_req(url.Values{
"foo": {"1978-07-10"},
}))
if ex, ok := res.Errors["foo"]; ok || ex != nil {
t.Fatalf("Expected %v. Got %v", nil, ex)
}
rval := res.Value.(map[string]interface{})
tm := time.Date(1978, 7, 10, 0, 0, 0, 0, time.UTC)
if ex, ok := rval["foo"].(time.Time); !ok || ex != tm {
t.Fatalf("Expected %v. Got %v", tm, ex)
}
}

func TestConvertersBoolConverter(t *testing.T) {
f := &Form{
Fields: []Field{
{Name: "foo", Converter: BoolConverter},
},
}
res := f.Load(create_req(url.Values{
"foo": {"on"},
}))
if ex, ok := res.Errors["foo"]; ok || ex != nil {
t.Fatalf("Expected %v. Got %v", nil, ex)
}
rval := res.Value.(map[string]interface{})
b := true
if ex, ok := rval["foo"].(bool); !ok || ex != b {
t.Fatalf("Expected %v. Got %v", b, ex)
}
}
27 changes: 27 additions & 0 deletions converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ package forms
import (
"errors"
"strconv"
"time"
)

const (
time_format = "2006-01-02"
)

var (
//Some built in converters that return the type they suggest with any errors.
IntConverter ConverterFunc = int_converter
Float64Converter ConverterFunc = float64_converter
Float32Converter ConverterFunc = float32_converter
TimeConverter ConverterFunc = time_converter
BoolConverter ConverterFunc = bool_converter
)

func make_human_readable(numerr *strconv.NumError) (err error) {
Expand Down Expand Up @@ -57,3 +64,23 @@ func float32_converter(in string) (out interface{}, err error) {
out = float32(f)
return
}

func time_converter(in string) (out interface{}, err error) {
//parse the input
t, err := time.Parse(time_format, in)

if err != nil {
return nil, errors.New("Invalid time: YYYY-MM-DD")
}

//set our output
out = t
return
}

func bool_converter(in string) (out interface{}, err error) {
if in == "on" {
return true, nil
}
return false, nil
}
52 changes: 52 additions & 0 deletions validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,55 @@ func TestValidatorsNonemptyValidator(t *testing.T) {
t.Fatalf("Expected %v. Got %v", "not nil", ex)
}
}

func TestValidatorsTimeValidator(t *testing.T) {
f := &Form{
Fields: []Field{
{Name: "foo", Validators: []Validator{DateValidator}},
},
}
res := f.Load(create_req(url.Values{
"foo": {"2010-10-26"},
}))
if ex, ok := res.Errors["foo"]; ok || ex != nil {
t.Fatalf("Expected %v. Got %v", nil, ex)
}

res = f.Load(create_req(url.Values{
"foo": {""},
}))
if ex, ok := res.Errors["foo"]; !ok || ex == nil {
t.Fatalf("Expected %v. Got %v", "not nil", ex)
}

res = f.Load(create_req(nil))
if ex, ok := res.Errors["foo"]; !ok || ex == nil {
t.Fatalf("Expected %v. Got %v", "not nil", ex)
}
}

func TestValidatorsEmailValidator(t *testing.T) {
f := &Form{
Fields: []Field{
{Name: "foo", Validators: []Validator{EmailValidator}},
},
}
res := f.Load(create_req(url.Values{
"foo": {"test@test.te"},
}))
if ex, ok := res.Errors["foo"]; ok || ex != nil {
t.Fatalf("Expected %v. Got %v", nil, ex)
}

res = f.Load(create_req(url.Values{
"foo": {""},
}))
if ex, ok := res.Errors["foo"]; !ok || ex == nil {
t.Fatalf("Expected %v. Got %v", "not nil", ex)
}

res = f.Load(create_req(nil))
if ex, ok := res.Errors["foo"]; !ok || ex == nil {
t.Fatalf("Expected %v. Got %v", "not nil", ex)
}
}
29 changes: 28 additions & 1 deletion validators.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package forms

import "errors"
import (
"errors"
"regexp"
"time"
)

var (
//Some built in validators that do what you would expect.
NonemptyValidator ValidatorFunc = nonempty_validator
DateValidator ValidatorFunc = date_validator
EmailValidator ValidatorFunc = email_validator
)

func nonempty_validator(in string) (out string, err error) {
Expand All @@ -14,3 +20,24 @@ func nonempty_validator(in string) (out string, err error) {
}
return
}

func date_validator(in string) (string, error) {
_, err := time.Parse(time_format, in)
if err != nil {
return in, errors.New("Invalid date")
}
return in, nil
}

// vague email validator (better then nothing)
func email_validator(in string) (string, error) {
ok, err := regexp.MatchString(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`, in)

if err != nil {
return in, err
}
if !ok {
return in, errors.New("Invalid e-mail address")
}
return in, nil
}