Skip to content

Commit

Permalink
feat: use gt, lt, gte and lte in validations
Browse files Browse the repository at this point in the history
BREAKING CHANGE

Closes #26
  • Loading branch information
vladfaust committed Aug 16, 2018
1 parent 6668a13 commit 17ed763
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion spec/action/params_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Prism::Action::Params::Spec
include Params

params do
param :id, Int32, validate: {min: 42}
param :id, Int32, validate: {gte: 42}
param :value, Int32?
param :time, Time?
end
Expand Down
8 changes: 4 additions & 4 deletions spec/params/validation_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ module Prism::Params::ValidationSpec
error!("has reserved value") if %w(foo bar baz).includes?(name)
},
}
param :age, Int32, nilable: true, validate: {min!: 17}
param :age, Int32, nilable: true, validate: {gt: 17}
param :height, Float64?, validate: {in: (0.5..2.5)}
param :iq, Int32?, validate: {min: 100, max!: 200}
param :iq, Int32?, validate: {gte: 100, lt: 200}
param :array, Array(Int32)?, validate: {
size: (1..2),
custom: ->(array : Array(Int32)) {
Expand Down Expand Up @@ -70,7 +70,7 @@ module Prism::Params::ValidationSpec
end

describe "#age" do
it "validates min!" do
it "validates gt" do
assert_invalid_param("?id=42&name=kek&age=17", "age", "must be greater than 17")
end
end
Expand All @@ -82,7 +82,7 @@ module Prism::Params::ValidationSpec
end

describe "#iq" do
it "validates min" do
it "validates gte" do
assert_invalid_param("?id=42&name=kek&iq=10", "iq", "must be greater or equal to 100")
end

Expand Down
10 changes: 8 additions & 2 deletions spec/params_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ module Prism::Params::Specs
include Prism::Params

params do
param :id, Int32, validate: {min: 42}
param :id, Int32, validate: {gte: 42}
param :value, Int32?
param :time, Time?
param :float_value, Float64?
param :"kebab-param", String?, proc: ->(p : String) { p.upcase }

param :nest1, nilable: true do
param :nest2 do
param :bar, Int32, validate: {max: 42}
param :bar, Int32, validate: {lt: 42}
end

param :foo, String?, proc: ->(p : String) { p.downcase }
Expand Down Expand Up @@ -122,6 +122,12 @@ module Prism::Params::Specs
response = handle_request(SimpleAction, Req.new(method: "GET", resource: "/?id=41&important[]=foo"))
end
end

it "raises" do
expect_raises(InvalidParamError) do
response = handle_request(SimpleAction, Req.new(method: "GET", resource: "/?id=42&important[]=foo&nest1[nest2][bar]=42"))
end
end
end

describe "testing certain content types" do
Expand Down
32 changes: 16 additions & 16 deletions src/prism/params/validation.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ module Prism::Params
# Implemented inline validations (defined as `:validate` option on param):
#
# - *size* (`Range | Int32`) - Validate size;
# - *min* (`Comparable`) - Check if param value `>=` than min;
# - *max* (`Comparable`) - Check if param value `<=` than max;
# - *min!* (`Comparable`) - Check if param value `>` than min;
# - *max!* (`Comparable`) - Check if param value `<` than max;
# - *gte* (`Comparable`) - Check if param value is *g*reater *t*han or *e*qual (`>=`);
# - *lte* (`Comparable`) - Check if param value is *l*ess *t*han or *e*qual (`<=`);
# - *gt* (`Comparable`) - Check if param value is *g*reater *t*han (`>`);
# - *lt* (`Comparable`) - Check if param value is *l*ess *t*han (`<`);
# - *in* (`Enumerable`) - Validate if param value is included in range or array etc.;
# - *regex* (`Regex`) - Validate if param value matches regex;
# - *custom* (`Proc`) - Custom validation, see example below.
Expand Down Expand Up @@ -56,27 +56,27 @@ module Prism::Params
end
{% end %}

{% if validations[:min] %}
unless {{value}} >= {{validations[:min]}}
error!("must be greater or equal to {{validations[:min].id}}")
{% if validations[:gte] %}
unless {{value}} >= {{validations[:gte]}}
error!("must be greater or equal to {{validations[:gte].id}}")
end
{% end %}

{% if validations[:max] %}
unless {{value}} <= {{validations[:max]}}
error!("must be less or equal to {{validations[:max].id}}")
{% if validations[:lte] %}
unless {{value}} <= {{validations[:lte]}}
error!("must be less or equal to {{validations[:lte].id}}")
end
{% end %}

{% if validations[:min!] %}
unless {{value}} > {{validations[:min!]}}
error!("must be greater than {{validations[:min!].id}}")
{% if validations[:gt] %}
unless {{value}} > {{validations[:gt]}}
error!("must be greater than {{validations[:gt].id}}")
end
{% end %}

{% if validations[:max!] %}
unless {{value}} < {{validations[:max!]}}
error!("must be less than {{validations[:max!].id}}")
{% if validations[:lt] %}
unless {{value}} < {{validations[:lt]}}
error!("must be less than {{validations[:lt].id}}")
end
{% end %}

Expand Down

0 comments on commit 17ed763

Please sign in to comment.