Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/feliux/validate
Golang FOP implementation for validating params
https://github.com/feliux/validate
Last synced: 1 day ago
JSON representation
Golang FOP implementation for validating params
- Host: GitHub
- URL: https://github.com/feliux/validate
- Owner: feliux
- Created: 2024-04-29T16:33:36.000Z (9 months ago)
- Default Branch: master
- Last Pushed: 2024-04-29T16:57:31.000Z (9 months ago)
- Last Synced: 2024-06-19T11:28:44.826Z (7 months ago)
- Language: Go
- Homepage:
- Size: 3.91 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Functional Go implementation for validating params. Useful for http handlers!
**Usage**
```go
type AccountSetupParams struct {
Username string
}type AccountSetupErrors struct {
Username string
}func HandleAccountSetup(w http.ResponseWriter, r *http.Request) error {
var errors auth.AccountSetupErrors
params := auth.AccountSetupParams{
Username: r.FormValue("username"),
}
ok := validations.New(¶ms, validations.Fields{
"Username": validations.Rules(validations.Min(2), validations.Max(50)),
}).Validate(&errors)
if !ok {
// handle !ok
}
// handle ok ...
}
``````go
type SignupParams struct {
Email string
Password string
ConfirmPassword string
}type SignupErrors struct {
Email string
Password string
ConfirmPassword string
}func HandleSignupForm(w http.ResponseWriter, r *http.Request) error {
signupParams := auth.SignupParams{
Email: r.FormValue("email"),
Password: r.FormValue("password"),
ConfirmPassword: r.FormValue("confirmPassword"),
}
errors := auth.SignupErrors{}
if ok := validations.New(&signupParams, validations.Fields{
"Email": validations.Rules(validations.Email),
"Password": validations.Rules(validations.Password),
"ConfirmPassword": validations.Rules(
validations.Equal(signupParams.Password),
validations.Message("Passwords do not match"),
),
}).Validate(&errors); !ok {
// handle !ok
}
// handle ok ...
}
```