param_check 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d98a6fa8c72c459888f417ac75fe6a2f9ff5167a
4
- data.tar.gz: 341fa70017a064a1b5046f21d2d7b1dd3697fd2a
3
+ metadata.gz: a9590fa4308abf4f64a13849ab47ad1dc8b68925
4
+ data.tar.gz: e857e2a0ac155531ca16253dc2d741b971e3f982
5
5
  SHA512:
6
- metadata.gz: f9a92099a2944dbe2889ce787cbaacb1e65a46808967e738f8e03be5197e595ce3cb760d7ab3f79b7b2f29773be9b3b61a150903c57ef76e8c21f1186ea40671
7
- data.tar.gz: f66096a368a3e10b300d1da425f988dbe9753fb498e6abf3cba26977b0646945540fec8305975fa7a35e1ccc39c9f1e75beb46991406cf7cb7a6f1c1b052a635
6
+ metadata.gz: de94c96397f07a7e6c331b421a0597c970b1e427dcd600c8b9748077da32f96ce41ffa7fbe5ed1ea4c3541320378d16fd7dfb8b2c9367fe8367449a1a1b1ba9a
7
+ data.tar.gz: 50a8a7ecf47a2afddcf592abb91083ef05bf4278bd14402157853450c9ca5eeef862c2607ed152dc3615661b78e357f8cbf13c35ab1f79167b2a964b32f9fa29
data/.DS_Store ADDED
Binary file
data/README.md CHANGED
@@ -68,10 +68,15 @@ params[:foo] = {
68
68
  params[:foo] = 'bar'
69
69
  ```
70
70
 
71
- ### Validating range
71
+ ### Validating numbers
72
+ For number validation, you can use either the long hand keys `less_than` or the short hand `lt`
73
+
72
74
  ```ruby
73
75
  def index
74
- param! :foo, min: 1, max: 3
76
+ param! :foo, lt: 2
77
+ param! :bar, lte: 3
78
+ param! :baz, mt: 4
79
+ param! :bazzy, mte: 5
75
80
  end
76
81
  ```
77
82
 
@@ -79,6 +84,8 @@ end
79
84
  ```ruby
80
85
  def index
81
86
  param! :foo, in: ['foo', 'bar']
87
+ param! :bar, in: 1..4
88
+ param! :baz, in: 'a'..'z'
82
89
  end
83
90
  ```
84
91
 
@@ -93,11 +100,21 @@ end
93
100
  ```
94
101
 
95
102
  ## Options
96
- | Option | Type | Default | Values | Description |
97
- | -------- | ------- | ------- | ---------------------------------------------- | ------------------------------------------ |
98
- | required | Boolean | false | true/false | Is the parameter required? |
99
- | type | Class | nil | Fixnum/Integer/String/ActionController::Params | Specifies the type the parameter should be |
100
-
103
+ <<<<<<< HEAD
104
+ | Option | Type | Default | Values | Description |
105
+ | -------- | ------- | ------- | ---------------------------------------------------- | --------------------------------------------------- |
106
+ | required | Boolean | false | true/false | Is the parameter required? |
107
+ | type | Class | nil | Fixnum/Integer/Float/String/ActionController::Params | Specifies the type the parameter should be |
108
+ | lt | Integer | nil | Integer/Float | The parameter value should be less than |
109
+ | lte | Integer | nil | Integer/Float | The parameter value should be less than or equal to |
110
+ | mt | Integer | nil | Integer/Float | The parameter value should be more than |
111
+ | mte | Integer | nil | Integer/Float | The parameter value should be more than or equal to |
112
+ | in | Array | nil | Array | An array of values the param should match |
113
+
114
+ ## Production sites using this gem
115
+ Submit a PR to add your site to the list
116
+
117
+ [Givey](https://www.givey.com)
101
118
 
102
119
  ## Development
103
120
 
@@ -2,6 +2,8 @@ en:
2
2
  param_check:
3
3
  missing_required_parameter: "Missing required parameter: %{parameter}"
4
4
  invalid_parameter: "Parameter %{parameter} is invalid. Expected %{expected}, got %{got}"
5
- invalid_minimum: "Parameter %{parameter} must be at least %{min}, got %{got}"
6
- invalid_maximum: "Parameter %{parameter} must not be more than %{max}, got %{got}"
7
5
  invalid_inclusion: "Parameter %{parameter} is not in the list. Expected inclusion in %{expected}, got %{got}"
6
+ value_not_less_than: "Parameter %{parameter} should be less than %{expected}. Got %{got}"
7
+ value_not_less_than_equal_to: "Parameter %{parameter} should be less than or equal to %{expected}. Got %{got}"
8
+ value_not_more_than: "Parameter %{parameter} should be more than %{expected}. Got %{got}"
9
+ value_not_more_than_equal_to: "Parameter %{parameter} should be more than or equal to %{expected}. Got %{got}"
@@ -1,3 +1,3 @@
1
1
  module ParamCheck
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/param_check.rb CHANGED
@@ -78,27 +78,46 @@ module ParamCheck
78
78
  end
79
79
  end
80
80
 
81
- def validate_range name, value, min = nil, max = nil
82
- if min.present?
83
- if value.nil? || value.to_i < min.to_i
84
- raise ParameterError, I18n.t(
85
- 'param_check.invalid_minimum',
86
- parameter: name,
87
- min: min,
88
- got: value,
89
- )
90
- end
81
+ def validate_number name, value, options = {}
82
+ options[:lt] = options[:less_than] if options[:less_than].present?
83
+ options[:lte] = options[:less_than_equal_to] if options[:less_than_equal_to].present?
84
+ options[:mt] = options[:more_than] if options[:more_than].present?
85
+ options[:more_than_equal_to] = options[:more_than_equal_to] if options[:more_than_equal_to].present?
86
+
87
+ if options[:lt].present?
88
+ raise ParameterError, I18n.t(
89
+ 'param_check.value_not_less_than',
90
+ parameter: name,
91
+ expected: options[:lt],
92
+ got: value,
93
+ ) if value >= options[:lt]
91
94
  end
92
95
 
93
- if max.present?
94
- if value.nil? || value.to_i > max.to_i
95
- raise ParameterError, I18n.t(
96
- 'param_check.invalid_maximum',
97
- parameter: name,
98
- max: max,
99
- got: value,
100
- )
101
- end
96
+ if options[:lte].present?
97
+ raise ParameterError, I18n.t(
98
+ 'param_check.value_not_less_than_equal_to',
99
+ parameter: name,
100
+ expected: options[:lte],
101
+ got: value,
102
+ ) if value > options[:lte]
103
+ end
104
+
105
+ if options[:mt].present?
106
+ raise ParameterError, I18n.t(
107
+ 'param_check.value_not_more_than',
108
+ parameter: name,
109
+ expected: options[:mt],
110
+ got: value,
111
+ ) if value <= options[:mt]
112
+ end
113
+
114
+ if options[:mte].present?
115
+ raise ParameterError, I18n.t(
116
+ 'param_check.value_not_more_than_equal_to',
117
+ parameter: name,
118
+ expected: options[:mte],
119
+ got: value,
120
+ ) if value < options[:mte]
102
121
  end
103
122
  end
104
123
 
data/param_check.gemspec CHANGED
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
11
11
 
12
12
  spec.summary = %q{Validate parameters for Rails API methods}
13
13
  spec.description = %q{Validate parameters presence and type for Rails API methods}
14
- spec.homepage = "https://cjmarkham.co.uk"
14
+ spec.homepage = "https://github.com/cjmarkham/param_check"
15
15
  spec.license = "MIT"
16
16
 
17
17
  spec.files = `git ls-files -z`.split("\x0").reject do |f|
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: param_check
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - cjmarkham
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-15 00:00:00.000000000 Z
11
+ date: 2017-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -73,6 +73,7 @@ executables: []
73
73
  extensions: []
74
74
  extra_rdoc_files: []
75
75
  files:
76
+ - ".DS_Store"
76
77
  - ".gitignore"
77
78
  - ".rspec"
78
79
  - CODE_OF_CONDUCT.md
@@ -88,7 +89,7 @@ files:
88
89
  - lib/param_check/engine.rb
89
90
  - lib/param_check/version.rb
90
91
  - param_check.gemspec
91
- homepage: https://cjmarkham.co.uk
92
+ homepage: https://github.com/cjmarkham/param_check
92
93
  licenses:
93
94
  - MIT
94
95
  metadata: {}