Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0

The validator work

Tests are passed
Cette révision appartient à :
akuma06 2017-07-01 02:59:39 +02:00
Parent 946f02d0fc
révision b142ee75a4
2 fichiers modifiés avec 37 ajouts et 7 suppressions

Voir le fichier

@ -9,6 +9,7 @@ import (
"github.com/NyaaPantsu/nyaa/util/log"
msg "github.com/NyaaPantsu/nyaa/util/messages"
"github.com/go-playground/validator"
"strconv"
)
var validate *validator.Validate
@ -16,6 +17,7 @@ var validate *validator.Validate
func init() {
validate = validator.New()
validate.RegisterValidation("isutf", IsUTFLetterNumericValidator)
validate.RegisterValidation("default", DefaultValidator)
}
// ValidateForm : Check if a form is valid according to its tags
@ -135,3 +137,31 @@ func IsUTFLetterNumericValidator(fl validator.FieldLevel) bool {
value := fl.Field().String() // value
return IsUTFLetterNumeric(value)
}
func DefaultValidator(fl validator.FieldLevel) bool {
switch fl.Field().Kind() {
case reflect.String:
if len(fl.Field().String()) == 0 {
fl.Field().SetString(fl.Param())
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if fl.Field().Int() == 0 {
number, err := strconv.Atoi(fl.Param())
if err != nil {
fmt.Printf("Couldn't convert default value for field %s", fl.FieldName())
return false
}
fl.Field().SetInt(int64(number))
}
case reflect.Float32, reflect.Float64:
if fl.Field().Float() == 0 {
number, err := strconv.ParseFloat(fl.Param(), 64)
if err != nil {
fmt.Printf("Couldn't convert default value for field %s", fl.FieldName())
return false
}
fl.Field().SetFloat(number)
}
}
return true
}

Voir le fichier

@ -19,9 +19,9 @@ var _ = func() (_ struct{}) {
}()
type TestForm struct {
DefaultVal int `form:"default" default:"3" notnull:"true"`
ConfirmVal string `form:"confirm" needed:"true" equalInput:"ConfirmeVal" len_min:"7" len_max:"8"`
ConfirmeVal string `form:"confirme" needed:"true"`
DefaultVal int `validate:"default=3,required"`
ConfirmVal string `validate:"eqfield=ConfirmeVal,min=7,max=8,required"`
ConfirmeVal string `validate:"required"`
}
func TestValidateForm(t *testing.T) {
@ -48,25 +48,25 @@ func TestValidateForm(t *testing.T) {
testform.ConfirmVal = "test"
testform.ConfirmeVal = "test"
ValidateForm(&testform, messages)
if len(messages.GetErrors("confirm")) == 0 {
if len(messages.GetErrors("ConfirmVal")) == 0 {
t.Errorf("No errors on minimal length test when parsing invalid form: %v", testform)
}
messages.ClearAllErrors()
testform.ConfirmVal, testform.ConfirmeVal = "testing", "testind"
ValidateForm(&testform, messages)
if len(messages.GetErrors("confirm")) == 0 {
if len(messages.GetErrors("ConfirmVal")) == 0 {
t.Errorf("No errors on equal test when parsing invalid form: %v", testform)
}
messages.ClearAllErrors()
testform.ConfirmVal, testform.ConfirmeVal = "", "testing"
ValidateForm(&testform, messages)
if len(messages.GetErrors("confirm")) == 0 {
if len(messages.GetErrors("ConfirmVal")) == 0 {
t.Errorf("No errors on needed test when parsing invalid form: %v", testform)
}
messages.ClearAllErrors()
testform.ConfirmVal, testform.ConfirmeVal = "azertyuid", "azertyuid"
ValidateForm(&testform, messages)
if len(messages.GetErrors("confirm")) == 0 {
if len(messages.GetErrors("ConfirmVal")) == 0 {
t.Errorf("No errors on maximal length test when parsing invalid form %v", testform)
}
messages.ClearAllErrors()