improve validate testing with examples
also simplified structure of test cases and failure logging
This commit is contained in:
@@ -4,31 +4,7 @@ A suite of straightforward validation functions. You put something in, you get b
|
||||
|
||||
## Error handling
|
||||
|
||||
You can use `errors.Is()` to ascertain the type of errors thrown by validation functions. This may be helpful to control side effects, particularly if using multiple validators and returning early (similar to a strongly-typed try-catch). For example:
|
||||
|
||||
## Example
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/annybs/go/validate"
|
||||
)
|
||||
|
||||
func main() {
|
||||
v := validate.Equal("a")
|
||||
if err := v("b"); err != nil {
|
||||
if errors.Is(err, validate.ErrNotEqual) {
|
||||
fmt.Println("failed successfully")
|
||||
} else {
|
||||
fmt.Println("failed unsuccessfully")
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
You can use `errors.Is()` to ascertain the type of errors returned by validation functions. This may be helpful to control side effects, particularly if you are using multiple validators or want to change the error message.
|
||||
|
||||
## License
|
||||
|
||||
|
||||
Reference in New Issue
Block a user