-
Notifications
You must be signed in to change notification settings - Fork 663
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from gin-gonic/example
chore: update go-playground/validator example
- Loading branch information
Showing
4 changed files
with
26 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,17 @@ | ||
## Struct level validations | ||
# Struct level validations | ||
|
||
Validations can also be registered at the `struct` level when field level validations | ||
don't make much sense. This can also be used to solve cross-field validation elegantly. | ||
Additionally, it can be combined with tag validations. Struct Level validations run after | ||
the structs tag validations. | ||
|
||
### Example requests | ||
## Example requests | ||
|
||
```shell | ||
# Validation errors are generated for struct tags as well as at the struct level | ||
$ curl -s -X POST http://localhost:8085/user \ | ||
-H 'content-type: application/json' \ | ||
-d '{}' | jq | ||
-H 'content-type: application/json' \ | ||
-d '{}' | jq | ||
{ | ||
"error": "Key: 'User.Email' Error:Field validation for 'Email' failed on the 'required' tag\nKey: 'User.FirstName' Error:Field validation for 'FirstName' failed on the 'fnameorlname' tag\nKey: 'User.LastName' Error:Field validation for 'LastName' failed on the 'fnameorlname' tag", | ||
"message": "User validation failed!" | ||
|
@@ -20,7 +20,7 @@ $ curl -s -X POST http://localhost:8085/user \ | |
# Validation fails at the struct level because neither first name nor last name are present | ||
$ curl -s -X POST http://localhost:8085/user \ | ||
-H 'content-type: application/json' \ | ||
-d '{"email": "[email protected]"}' | jq | ||
-d '{"email": "[email protected]"}' | jq | ||
{ | ||
"error": "Key: 'User.FirstName' Error:Field validation for 'FirstName' failed on the 'fnameorlname' tag\nKey: 'User.LastName' Error:Field validation for 'LastName' failed on the 'fnameorlname' tag", | ||
"message": "User validation failed!" | ||
|
@@ -29,22 +29,22 @@ $ curl -s -X POST http://localhost:8085/user \ | |
# No validation errors when either first name or last name is present | ||
$ curl -X POST http://localhost:8085/user \ | ||
-H 'content-type: application/json' \ | ||
-d '{"fname": "George", "email": "[email protected]"}' | ||
-d '{"fname": "George", "email": "[email protected]"}' | ||
{"message":"User validation successful."} | ||
|
||
$ curl -X POST http://localhost:8085/user \ | ||
-H 'content-type: application/json' \ | ||
-d '{"lname": "Contanza", "email": "[email protected]"}' | ||
-d '{"lname": "Contanza", "email": "[email protected]"}' | ||
{"message":"User validation successful."} | ||
|
||
$ curl -X POST http://localhost:8085/user \ | ||
-H 'content-type: application/json' \ | ||
-d '{"fname": "George", "lname": "Costanza", "email": "[email protected]"}' | ||
-d '{"fname": "George", "lname": "Costanza", "email": "[email protected]"}' | ||
{"message":"User validation successful."} | ||
``` | ||
|
||
### Useful links | ||
|
||
- Validator docs - https://godoc.org/gopkg.in/go-playground/validator.v8#Validate.RegisterStructValidation | ||
- Struct level example - https://github.com/go-playground/validator/blob/v8.18.2/examples/struct-level/struct_level.go | ||
- Validator release notes - https://github.com/go-playground/validator/releases/tag/v8.7 | ||
- [Validator docs](https://godoc.org/gopkg.in/go-playground/validator.v8#Validate.RegisterStructValidation) | ||
- [Struct level example](https://github.com/go-playground/validator/blob/v8.18.2/examples/struct-level/struct_level.go) | ||
- [Validator release notes](https://github.com/go-playground/validator/releases/tag/v8.7) |