diff --git a/doc.go b/doc.go index 0bc27a1..4e1a15f 100644 --- a/doc.go +++ b/doc.go @@ -998,5 +998,30 @@ that should not make it to production. } validate.Struct(t) // this will panic + +Non standard validators + +A collection of validation rules that are frequently needed but are more +complex than the ones found in the baked in validators. +A non standard validator must be registered manually using any tag you like. +See below examples of registration and use. + + type Test struct { + TestField string `validate:"yourtag"` + } + + t := &Test{ + TestField: "Test" + } + + validate := validator.New() + validate.RegisterValidation("yourtag", validations.ValidatorName) + + NotBlank + This validates that the value is not blank or with length zero. + For strings ensures they do not contain only spaces. For channels, maps, slices and arrays + ensures they don't have zero length. For others, a non empty value is required. + + Usage: notblank */ package validator diff --git a/non-standard/validators/notblank.go b/non-standard/validators/notblank.go new file mode 100644 index 0000000..5c2d806 --- /dev/null +++ b/non-standard/validators/notblank.go @@ -0,0 +1,25 @@ +package validators + +import ( + "reflect" + "strings" + + "github.com/andreiavrammsd/validator" +) + +// NotBlank is the validation function for validating if the current field +// has a value or length greater than zero, or is not a space only string. +func NotBlank(fl validator.FieldLevel) bool { + field := fl.Field() + + switch field.Kind() { + case reflect.String: + return len(strings.TrimSpace(field.String())) > 0 + case reflect.Chan, reflect.Map, reflect.Slice, reflect.Array: + return field.Len() > 0 + case reflect.Ptr, reflect.Interface, reflect.Func: + return !field.IsNil() + default: + return field.IsValid() && field.Interface() != reflect.Zero(field.Type()).Interface() + } +} diff --git a/non-standard/validators/notblank_test.go b/non-standard/validators/notblank_test.go new file mode 100644 index 0000000..1eecae6 --- /dev/null +++ b/non-standard/validators/notblank_test.go @@ -0,0 +1,65 @@ +package validators + +import ( + "testing" + + "github.com/andreiavrammsd/validator" + "gopkg.in/go-playground/assert.v1" +) + +type test struct { + String string `validate:"notblank"` + Array []int `validate:"notblank"` + Pointer *int `validate:"notblank"` + Number int `validate:"notblank"` + Interface interface{} `validate:"notblank"` + Func func() `validate:"notblank"` +} + +func TestNotBlank(t *testing.T) { + v := validator.New() + err := v.RegisterValidation("notblank", NotBlank) + assert.Equal(t, nil, err) + + // Errors + var x *int + invalid := test{ + String: " ", + Array: []int{}, + Pointer: x, + Number: 0, + Interface: nil, + Func: nil, + } + fieldsWithError := []string{ + "String", + "Array", + "Pointer", + "Number", + "Interface", + "Func", + } + + errors := v.Struct(invalid).(validator.ValidationErrors) + var fields []string + for _, err := range errors { + fields = append(fields, err.Field()) + } + + assert.Equal(t, fieldsWithError, fields) + + // No errors + y := 1 + x = &y + valid := test{ + String: "str", + Array: []int{1}, + Pointer: x, + Number: 1, + Interface: "value", + Func: func() {}, + } + + err = v.Struct(valid) + assert.Equal(t, nil, err) +}