Skip to content

Commit

Permalink
feat(checkable): add check!, ckeck_{{field}}!, clean_{{field}}!
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolab committed Mar 12, 2021
1 parent d8cc2d2 commit c90a4fe
Show file tree
Hide file tree
Showing 3 changed files with 543 additions and 11 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`):
Expand Down Expand Up @@ -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`).
Expand Down Expand Up @@ -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]"
```

Expand Down
Loading

0 comments on commit c90a4fe

Please sign in to comment.