fix(binding): Expose validator engine used by the default Validator (#1277)

* fix(binding): Expose validator engine used by the default Validator

- Add func ValidatorEngine for returning the underlying validator engine used
  in the default StructValidator implementation.
- Remove the function RegisterValidation from the StructValidator interface
  which made it immpossible to use a StructValidator implementation without the
  validator.v8 library.
- Update and rename test for registering validation
  Test{RegisterValidation => ValidatorEngine}.
- Update readme and example for registering custom validation.
- Add example for registering struct level validation.
- Add documentation for the following binding funcs/types:
  - Binding interface
  - StructValidator interface
  - Validator instance
  - Binding implementations
  - Default func

* fix(binding): Move validator engine getter inside interface

* docs: rm date cmd from custom validation demo
This commit is contained in:
Suhas Karanth
2018-03-29 12:03:07 +05:30
committed by Bo-Yi Wu
parent 65a65c2edd
commit 6d913fc343
8 changed files with 161 additions and 15 deletions

View File

@ -214,11 +214,14 @@ func notOne(
return false
}
func TestRegisterValidation(t *testing.T) {
func TestValidatorEngine(t *testing.T) {
// This validates that the function `notOne` matches
// the expected function signature by `defaultValidator`
// and by extension the validator library.
err := Validator.RegisterValidation("notone", notOne)
engine, ok := Validator.Engine().(*validator.Validate)
assert.True(t, ok)
err := engine.RegisterValidation("notone", notOne)
// Check that we can register custom validation without error
assert.Nil(t, err)
@ -228,6 +231,6 @@ func TestRegisterValidation(t *testing.T) {
// Check that we got back non-nil errs
assert.NotNil(t, errs)
// Check that the error matches expactation
// Check that the error matches expectation
assert.Error(t, errs, "", "", "notone")
}