-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(checkable): add check!, ckeck_{{field}}!, clean_{{field}}!
- Loading branch information
Showing
3 changed files
with
543 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -240,23 +240,38 @@ pp v.errors | |
pp user_h # => Casted and cleaned Hash | ||
# Same but raise if there is a validation error | ||
user_h = User.check!(input_h) | ||
# Check a Hash (on instance) | ||
user = user.new("[email protected]", 38) | ||
v = user.check # => Validation instance | ||
pp v.valid? | ||
pp v.errors | ||
# Same but raise if there is a validation error | ||
user.check! # => Validation instance | ||
# Example with an active record model | ||
user.check!.save | ||
# Check field | ||
v, email = User.check_email(value: "[email protected]") | ||
v, age = User.check_age(value: 42) | ||
# Same but raise if there is a validation error | ||
email = User.check_email!(value: "[email protected]") | ||
v, email = User.check_email(value: "[email protected] ", format: true) | ||
v, email = User.check_email(value: "[email protected] ", format: false) | ||
# Using an existing Validation instance | ||
v = Check.new_validation | ||
v, email = User.check_email(v, value: "[email protected]") | ||
# Same but raise if there is a validation error | ||
email = User.check_email!(v, value: "[email protected]") | ||
``` | ||
|
||
__Clean__ with this example class (`User`): | ||
|
@@ -310,6 +325,9 @@ Example with multiple values returned: | |
|
||
```crystal | ||
ok, value1, value2 = User.clean_my_tuple({1, 2, 3}) | ||
# Same but raise if there is a validation error | ||
value1, value2 = User.clean_my_tuple!({1, 2, 3}) | ||
``` | ||
|
||
Considering the example class above (`User`). | ||
|
@@ -342,6 +360,12 @@ So `clean_email` cast to `String` and strip the value `" [email protected] "`: | |
# Email value with one space before and one space after | ||
ok, email = User.clean_email(value: " [email protected] ") | ||
puts email # => "[email protected]" | ||
# Same but raise if there is a validation error | ||
# Email value with one space before and one space after | ||
email = User.clean_email!(value: " [email protected] ") | ||
puts email # => "[email protected]" | ||
``` | ||
|
||
|
Oops, something went wrong.