Skip to content

Commit

Permalink
feat(checkable): rules inline
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolab committed Apr 18, 2021
1 parent 99bda27 commit 93ff867
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 29 deletions.
30 changes: 12 additions & 18 deletions spec/check/checkable/checkable_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ module H

property email : String
property age : Int32?, {
age: {
check: {
min: {"Age should be more than 18", 18},
between: {"Age should be between 25 and 35", 25, 35},
},
clean: {type: Int32, to: :to_i32, nilable: true},
check: {
min: {"Age should be more than 18", 18},
between: {"Age should be between 25 and 35", 25, 35},
},
clean: {type: Int32, to: :to_i32, nilable: true},
}

def initialize(@email, @age)
Expand Down Expand Up @@ -72,22 +70,18 @@ module H

property email : String
property username : String?, {
username: {
required: "Username is required",
check: {
min: {"Length must be more than 2", 2},
},
clean: {type: String},
required: "Username is required",
check: {
min: {"Length must be more than 2", 2},
},
clean: {type: String},
}
property age : Int32?, {
age: {
check: {
min: {"Age should be more than 18", 18},
between: {"Age should be between 25 and 35", 25, 35},
},
clean: {type: Int32, to: :to_i32, message: "Wrong type", nilable: true},
check: {
min: {"Age should be more than 18", 18},
between: {"Age should be between 25 and 35", 25, 35},
},
clean: {type: Int32, to: :to_i32, message: "Wrong type", nilable: true},
}

# ---------------------------------------------------------------------------
Expand Down
18 changes: 8 additions & 10 deletions spec/check/checkable/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ module H
)

property email : String, {
email: {
required: true,
check: {
not_empty: {"Email should not be empty"},
email: {"It is not a valid email"},
},
clean: {
type: String,
to: :to_s,
},
required: true,
check: {
not_empty: {"Email should not be empty"},
email: {"It is not a valid email"},
},
clean: {
type: String,
to: :to_s,
},
}

Expand Down
2 changes: 1 addition & 1 deletion src/checkable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ module Check
{% verbatim do %}
{% for k, prop in PROPS %}
{% if prop[:args] %}
Check.rules({{prop[:args].double_splat}})
Check.rules({{prop[:name]}}: { {{prop[:args].double_splat}} })
{% end %}
{% end %}
{% end %}
Expand Down

0 comments on commit 93ff867

Please sign in to comment.