From cb043b81548e0580f3259af80cccefad24541d83 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Sun, 14 Oct 2012 20:07:49 +0300 Subject: [PATCH 1/5] added time converter and validator and email validator --- converters.go | 18 ++++++++++++++++++ validators.go | 23 +++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/converters.go b/converters.go index 9ac5466..825e238 100644 --- a/converters.go +++ b/converters.go @@ -5,11 +5,16 @@ import ( "strconv" ) +const ( + time_format = "1900-07-26" +) + 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 forms.ConverterFunc = time_converter ) func make_human_readable(numerr *strconv.NumError) (err error) { @@ -57,3 +62,16 @@ 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 +} diff --git a/validators.go b/validators.go index 3b4c917..e52e581 100644 --- a/validators.go +++ b/validators.go @@ -5,6 +5,8 @@ import "errors" var ( //Some built in validators that do what you would expect. NonemptyValidator ValidatorFunc = nonempty_validator + DateValidator forms.ValidatorFunc = date_validator + EmailValidator forms.ValidatorFunc = email_validator ) func nonempty_validator(in string) (out string, err error) { @@ -14,3 +16,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(`?`, in) + + if err != nil { + return in, err + } + if !ok { + return in, errors.New("Invalid e-mail address") + } + return in, nil +} From 27426724f426a0bf3dcd5221e8439211f703da12 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Sun, 14 Oct 2012 20:33:41 +0300 Subject: [PATCH 2/5] corrected import problems --- converters.go | 3 ++- validators.go | 12 ++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/converters.go b/converters.go index 825e238..33325f0 100644 --- a/converters.go +++ b/converters.go @@ -3,6 +3,7 @@ package forms import ( "errors" "strconv" + "time" ) const ( @@ -14,7 +15,7 @@ var ( IntConverter ConverterFunc = int_converter Float64Converter ConverterFunc = float64_converter Float32Converter ConverterFunc = float32_converter - TimeConverter forms.ConverterFunc = time_converter + TimeConverter ConverterFunc = time_converter ) func make_human_readable(numerr *strconv.NumError) (err error) { diff --git a/validators.go b/validators.go index e52e581..2fcf4ac 100644 --- a/validators.go +++ b/validators.go @@ -1,12 +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 forms.ValidatorFunc = date_validator - EmailValidator forms.ValidatorFunc = email_validator + DateValidator ValidatorFunc = date_validator + EmailValidator ValidatorFunc = email_validator ) func nonempty_validator(in string) (out string, err error) { @@ -18,7 +22,7 @@ func nonempty_validator(in string) (out string, err error) { } func date_validator(in string) (string, error) { - _, err := time.Parse(TIME_FORMAT, in) + _, err := time.Parse(time_format, in) if err != nil { return in, errors.New("Invalid date") } From d54b681cfa689b80f0027db4985fdacb8b4e89d1 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Mon, 15 Oct 2012 18:16:28 +0300 Subject: [PATCH 3/5] more corrections and tests (finally) --- converter_test.go | 20 ++++++++++++++++++ converters.go | 2 +- validator_test.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++ validators.go | 2 +- 4 files changed, 74 insertions(+), 2 deletions(-) diff --git a/converter_test.go b/converter_test.go index 1f6c065..3c7de7c 100644 --- a/converter_test.go +++ b/converter_test.go @@ -5,6 +5,7 @@ import ( "fmt" "net/url" "testing" + "time" ) var ( @@ -133,3 +134,22 @@ 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) + } +} diff --git a/converters.go b/converters.go index 33325f0..7b004e3 100644 --- a/converters.go +++ b/converters.go @@ -7,7 +7,7 @@ import ( ) const ( - time_format = "1900-07-26" + time_format = "2006-01-02" ) var ( diff --git a/validator_test.go b/validator_test.go index fe8126f..e11151e 100644 --- a/validator_test.go +++ b/validator_test.go @@ -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) + } +} diff --git a/validators.go b/validators.go index 2fcf4ac..df7430f 100644 --- a/validators.go +++ b/validators.go @@ -31,7 +31,7 @@ func date_validator(in string) (string, error) { // vague email validator (better then nothing) func email_validator(in string) (string, error) { - ok, err := regexp.MatchString(`?`, in) + ok, err := regexp.MatchString(`^[a-z0-9._%+\-]+@[a-z0-9.\-]+\.[a-z]{2,4}$`, in) if err != nil { return in, err From e40e4701cac646333c9d3f3daee8dded792083a9 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Mon, 15 Oct 2012 18:22:38 +0300 Subject: [PATCH 4/5] added cast --- converters.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/converters.go b/converters.go index 7b004e3..5233a61 100644 --- a/converters.go +++ b/converters.go @@ -73,6 +73,6 @@ func time_converter(in string) (out interface{}, err error) { } //set our output - out = t + out = time.Time(t) return } From dccf25ac37e2d12054502a892f5b11ad514a8837 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Sat, 20 Oct 2012 15:59:04 +0300 Subject: [PATCH 5/5] added bool converter --- converter_test.go | 19 +++++++++++++++++++ converters.go | 10 +++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/converter_test.go b/converter_test.go index 3c7de7c..c9d67ef 100644 --- a/converter_test.go +++ b/converter_test.go @@ -153,3 +153,22 @@ func TestConvertersTimeConverter(t *testing.T) { 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) + } +} diff --git a/converters.go b/converters.go index 5233a61..e61468f 100644 --- a/converters.go +++ b/converters.go @@ -16,6 +16,7 @@ var ( 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) { @@ -73,6 +74,13 @@ func time_converter(in string) (out interface{}, err error) { } //set our output - out = time.Time(t) + out = t return } + +func bool_converter(in string) (out interface{}, err error) { + if in == "on" { + return true, nil + } + return false, nil +}