This repository has been archived by the owner on Jun 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
feature: updating the validations API #8
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7dec952
feature: updating the validations API
paganotoni 2d4843a
task: updating docs a bit more
paganotoni 69de525
task: minor tweak to the docs
paganotoni 69a0ca3
task: exporting Validations and moving the tests to be in validation_…
paganotoni 7e6bf4f
feature: updating the API for validate and making it part of the form…
paganotoni d5d3290
changing the form instantiation
paganotoni fef5646
task: changing Form to Fields
paganotoni d808aa9
task: tweaking the docs a bit more
paganotoni File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
--- | ||
title: Form Validation | ||
index: 3 | ||
--- | ||
|
||
Leapkit provides the `form/validate` package that offers a flexible and reusable way to validate form data by defining a set of validation rules that can be applied to form fields. | ||
|
||
### How to Use | ||
|
||
Validations are a set of rules stablished for different fields passed in the request. You can define these Validations to be used in your http handlers by and call the `form.Validate` function passing the `req` (*http.Request) and handling the `validate.Errors` returned. Example: | ||
|
||
```go | ||
newUserValidation := validate.Form( | ||
validate.Field("email", validate.Required("email is required")), | ||
validate.Field("password", validate.Required("password is required")), | ||
) | ||
|
||
verrs := form.Validate(req, newUserValidation) | ||
if len(verrs) > 0 { | ||
// handle validation errors... | ||
} | ||
``` | ||
|
||
### Built-in Rules | ||
|
||
You can build your set of rules for each validation by using the package's built-in functions. | ||
|
||
```go | ||
// General Rules: | ||
func Required(message ...string) Rule | ||
|
||
// String Rules: | ||
func Matches(field string, message ...string) Rule | ||
func MatchRegex(re *regexp.Regexp, message ...string) Rule | ||
func MinLength(min int, message ...string) Rule | ||
func MaxLength(max int, message ...string) Rule | ||
func WithinOptions(options []string, message ...string) Rule | ||
|
||
// Number Rules: | ||
func EqualTo(value float64, message ...string) Rule | ||
func LessThan(value float64, message ...string) Rule | ||
func LessThanOrEqualTo(value float64, message ...string) Rule | ||
func GreaterThan(value float64, message ...string) Rule | ||
func GreaterThanOrEqualTo(value float64, message ...string) Rule | ||
|
||
// UUID Rule: | ||
func ValidUUID(message ...string) Rule | ||
|
||
// Time Rules: | ||
func TimeEqualTo(u time.Time, message ...string) Rule | ||
func TimeBefore(u time.Time, message ...string) Rule | ||
func TimeBeforeOrEqualTo(u time.Time, message ...string) Rule | ||
func TimeAfter(u time.Time, message ...string) Rule | ||
func TimeAfterOrEqualTo(u time.Time, message ...string) Rule | ||
``` | ||
|
||
### Custom validation Rules | ||
|
||
Alternatively, you can create your own validation rules. Example: | ||
|
||
```go | ||
func IsUnique(db *sqlx.DB ) func([]string) error { | ||
return func(emails []string) error { | ||
query := "SELECT EXISTS(SELECT 1 FROM users WHERE email = $1)" | ||
stmt, err := db.Prepare(query) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for _, email := range emails { | ||
var exists bool | ||
if err := stmt.QueryRow(email).Scan(&exists); err != nil { | ||
return err | ||
} | ||
|
||
if exists { | ||
return fmt.Errorf("email '%s' already exists.", email) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
// ... | ||
newUserValidation := validation.Form{ | ||
validation.New("email", validate.Required(), IsUnique(db)) | ||
} | ||
... | ||
``` |
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
Empty file.
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tatang26 👀