flexirest 1.3.32 → 1.3.33

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d818096fc6d03908005c35317dbc5e0ab2f03994
4
- data.tar.gz: a4ed3f0e1e7db9dd92d4c5fca478b0fc385edca3
3
+ metadata.gz: 1c372f15d7e88d296232e1e055cb598419ca3d2c
4
+ data.tar.gz: c61b9f302f68cde71be0d091288e10f5b994c342
5
5
  SHA512:
6
- metadata.gz: 35af9776e44f7b291ba41753f4cb7efef1f2441ccd0aed3fd41474b27dce714d7951cfc9a6ad5fe98996e9399b3ecece34d9b00dab65b77a93fd38eac88ea07f
7
- data.tar.gz: 45228f5f5c0fbcfde18bfa4bf5dac900024d9e45bd11d7957c80181da011e0ee253dbe59b55526b4fbf71199844b3bbc6b350874d851be3f6abf8527ae8dfd00
6
+ metadata.gz: '007239649da6105e553e6779858fc70951ff66825262300636254c34a749598839ad64c63bb145357b09bd07f85897b731d59b16c7a4354efd67d6f738fce30f'
7
+ data.tar.gz: fa9cbd5356984fc31ae87634833893cbc02b3586ad185324c85fe6458cedf6663e9303b93e437ffdecb09f09b5c88818e4570fd72508d402ba4da3807d74ac76
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.3.33
4
+
5
+ Feature:
6
+
7
+ - Allowed specifying custom message for validation failures
8
+
3
9
  ## 1.3.32
4
10
 
5
11
  Bugfix:
data/README.md CHANGED
@@ -959,7 +959,7 @@ You can create validations on your objects just like Rails' built in ActiveModel
959
959
  class Person < Flexirest::Base
960
960
  validates :first_name, presence: true #ensures that the value is present and not blank
961
961
  validates :last_name, existence: true #ensures that the value is non-nil only
962
- validates :password, length: {within:6..12}
962
+ validates :password, length: {within:6..12}, message: "Invalid password length, must be 6-12 characters"
963
963
  validates :post_code, length: {minimum:6, maximum:8}
964
964
  validates :salary, numericality: true, minimum: 20_000, maximum: 50_000
965
965
  validates :age, numericality: { minumum: 18, maximum: 65 }
@@ -977,7 +977,7 @@ Note: the block based validation is responsible for adding errors to `object._er
977
977
 
978
978
  Validations are run when calling `valid?` or when calling any API on an instance (and then only if it is `valid?` will the API go on to be called).
979
979
 
980
- `full_error_messages` returns an array of attributes with their associated error messages, i.e. `["age must be at least 18"]`
980
+ `full_error_messages` returns an array of attributes with their associated error messages, i.e. `["age must be at least 18"]`. Custom messages can be specified by passing a `:message` option to `validates`. This differs slightly from ActiveRecord in that it's an option to `validates` itself, not a part of a final hash.
981
981
 
982
982
  #### Permitting nil values
983
983
  The default behavior for `:length`, `:numericality` and `:inclusion` validators is to fail when a `nil` value is encountered. You can prevent `nil` attribute values from triggering validation errors for attributes that may permit `nil` by adding the `:allow_nil => true` option. Adding this option with a `true` value to `:length`, `:numericality` and `:inclusion` validators will permit `nil` values and not trigger errors. Some examples are:
@@ -15,12 +15,12 @@ gem 'flexirest'
15
15
 
16
16
  ## Configuration
17
17
 
18
- It's possible to explicit specify the `base_url` in the Model Class. If you have an common API Endpoint it makes sense to setup an initializer in `config/initializer` or use the `Rails.application.configure` namespace.
18
+ It's possible to explicit specify the `base_url` in the Model Class. If you have an common API Endpoint it makes sense to setup an initializer in `config/initializers` or use the `Rails.application.configure` namespace.
19
19
 
20
- This example use a custom file in `config/initializer` to setup the API endpoint. Either set a fixed URL or use environment variables if you would like to follow the [12factor](http://12factor.net/config) rules for preparing your application running on cloud infrastructure like heroku.
20
+ This example use a custom file in `config/initializers` to setup the API endpoint. Either set a fixed URL or use environment variables if you would like to follow the [12factor](http://12factor.net/config) rules for preparing your application running on cloud infrastructure like heroku.
21
21
 
22
22
  ```ruby
23
- # config/initializer/flexirest.rb
23
+ # config/initializers/flexirest.rb
24
24
  Flexirest::Base.base_url = ENV.fetch("API_ENDPOINT_URL")
25
25
  ```
26
26
 
@@ -24,63 +24,63 @@ module Flexirest
24
24
  validation[:options].each do |type, options|
25
25
  if type == :presence
26
26
  if value.nil?
27
- @errors[validation[:field_name]] << "must be present"
27
+ @errors[validation[:field_name]] << (validation[:options][:message] || "must be present")
28
28
  elsif value.blank?
29
- @errors[validation[:field_name]] << "must be present"
29
+ @errors[validation[:field_name]] << (validation[:options][:message] || "must be present")
30
30
  end
31
31
  elsif type == :existence
32
32
  if value.nil?
33
- @errors[validation[:field_name]] << "must be not be nil"
33
+ @errors[validation[:field_name]] << (validation[:options][:message] || "must be not be nil")
34
34
  end
35
35
  elsif type == :length
36
36
  if value.nil?
37
- @errors[validation[:field_name]] << "must be not be nil" unless allow_nil
37
+ @errors[validation[:field_name]] << (validation[:options][:message] || "must be not be nil") unless allow_nil
38
38
  else
39
39
  if options[:within]
40
- @errors[validation[:field_name]] << "must be within range #{options[:within]}" unless options[:within].include?(value.to_s.length )
40
+ @errors[validation[:field_name]] << (validation[:options][:message] || "must be within range #{options[:within]}") unless options[:within].include?(value.to_s.length )
41
41
  end
42
42
  if options[:minimum]
43
- @errors[validation[:field_name]] << "must be at least #{options[:minimum]} characters long" unless value.to_s.length >= options[:minimum]
43
+ @errors[validation[:field_name]] << (validation[:options][:message] || "must be at least #{options[:minimum]} characters long") unless value.to_s.length >= options[:minimum]
44
44
  end
45
45
  if options[:maximum]
46
- @errors[validation[:field_name]] << "must be no more than #{options[:maximum]} characters long" unless value.to_s.length <= options[:maximum]
46
+ @errors[validation[:field_name]] << (validation[:options][:message] || "must be no more than #{options[:maximum]} characters long") unless value.to_s.length <= options[:maximum]
47
47
  end
48
48
  end
49
49
  elsif type == :numericality
50
50
  if value.nil?
51
- @errors[validation[:field_name]] << "must be not be nil" unless allow_nil
51
+ @errors[validation[:field_name]] << (validation[:options][:message] || "must be not be nil") unless allow_nil
52
52
  else
53
53
  numeric = (true if Float(value) rescue false)
54
54
  if !numeric
55
- @errors[validation[:field_name]] << "must be numeric"
55
+ @errors[validation[:field_name]] << (validation[:options][:message] || "must be numeric")
56
56
  else
57
57
  if options.is_a?(Hash)
58
58
  if options[:minimum]
59
- @errors[validation[:field_name]] << "must be at least #{options[:minimum]}" unless value.to_f >= options[:minimum]
59
+ @errors[validation[:field_name]] << (validation[:options][:message] || "must be at least #{options[:minimum]}") unless value.to_f >= options[:minimum]
60
60
  end
61
61
  if options[:maximum]
62
- @errors[validation[:field_name]] << "must be no more than #{options[:maximum]}" unless value.to_f <= options[:maximum]
62
+ @errors[validation[:field_name]] << (validation[:options][:message] || "must be no more than #{options[:maximum]}") unless value.to_f <= options[:maximum]
63
63
  end
64
64
  end
65
65
  end
66
66
  end
67
67
  elsif type == :minimum
68
68
  if value.nil?
69
- @errors[validation[:field_name]] << "must be not be nil" unless allow_nil
69
+ @errors[validation[:field_name]] << (validation[:options][:message] || "must be not be nil") unless allow_nil
70
70
  else
71
- @errors[validation[:field_name]] << "must be at least #{options}" unless value.to_f >= options.to_f
71
+ @errors[validation[:field_name]] << (validation[:options][:message] || "must be at least #{options}") unless value.to_f >= options.to_f
72
72
  end
73
73
  elsif type == :maximum
74
74
  if value.nil?
75
- @errors[validation[:field_name]] << "must be not be nil" unless allow_nil
75
+ @errors[validation[:field_name]] << (validation[:options][:message] || "must be not be nil") unless allow_nil
76
76
  else
77
- @errors[validation[:field_name]] << "must be no more than #{options}" unless value.to_f <= options.to_f
77
+ @errors[validation[:field_name]] << (validation[:options][:message] || "must be no more than #{options}") unless value.to_f <= options.to_f
78
78
  end
79
79
  elsif type == :inclusion
80
80
  if value.nil?
81
- @errors[validation[:field_name]] << "must be not be nil" unless allow_nil
81
+ @errors[validation[:field_name]] << (validation[:options][:message] || "must be not be nil") unless allow_nil
82
82
  else
83
- @errors[validation[:field_name]] << "must be included in #{options[:in].join(", ")}" unless options[:in].include?(value)
83
+ @errors[validation[:field_name]] << (validation[:options][:message] || "must be included in #{options[:in].join(", ")}") unless options[:in].include?(value)
84
84
  end
85
85
  end
86
86
  end
@@ -1,3 +1,3 @@
1
1
  module Flexirest
2
- VERSION = "1.3.32"
2
+ VERSION = "1.3.33"
3
3
  end
@@ -3,7 +3,7 @@ require 'spec_helper'
3
3
  describe "Flexirest::Validation" do
4
4
  class SimpleValidationExample < OpenStruct
5
5
  include Flexirest::Validation
6
- validates :first_name, presence: true
6
+ validates :first_name, presence: true, message: "Sorry, something went wrong"
7
7
  validates :middle_name, length: { minimum: 2, maximum: 30 }, allow_nil: true
8
8
  validates :last_name, existence: true
9
9
  validates :nick_name, length: { minimum: 2, maximum: 30 }
@@ -34,6 +34,13 @@ describe "Flexirest::Validation" do
34
34
  expect(a._errors[:first_name].size).to eq(1)
35
35
  end
36
36
 
37
+ it "should return a custom error if specified and a validation fails" do
38
+ a = SimpleValidationExample.new
39
+ a.first_name = nil
40
+ a.valid?
41
+ expect(a._errors[:first_name][0]).to eq("Sorry, something went wrong")
42
+ end
43
+
37
44
  it "should be invalid if a required value is present but blank" do
38
45
  a = SimpleValidationExample.new
39
46
  a.first_name = ""
@@ -60,7 +67,7 @@ describe "Flexirest::Validation" do
60
67
  a.first_name = "John"
61
68
  a.valid?
62
69
  expect(a._errors[:first_name]).to be_empty
63
- end
70
+ end
64
71
  end
65
72
 
66
73
  context "when validating existence" do
@@ -83,7 +90,7 @@ describe "Flexirest::Validation" do
83
90
  a.last_name = "John"
84
91
  a.valid?
85
92
  expect(a._errors[:last_name]).to be_empty
86
- end
93
+ end
87
94
  end
88
95
 
89
96
  context "when validating length" do
@@ -116,7 +123,7 @@ describe "Flexirest::Validation" do
116
123
  a.valid?
117
124
  expect(a._errors[:post_code].size).to eq(1)
118
125
  end
119
-
126
+
120
127
  it "should be valid if a length is nil and allow_nil option is true" do
121
128
  a = SimpleValidationExample.new
122
129
  a.valid?
@@ -155,13 +162,13 @@ describe "Flexirest::Validation" do
155
162
  a = SimpleValidationExample.new(salary:100_000)
156
163
  a.valid?
157
164
  expect(a._errors[:salary].size).to be > 0
158
- end
165
+ end
159
166
 
160
167
  it "should be valid that a numeric field is above or equal to a minimum" do
161
168
  a = SimpleValidationExample.new(salary:30_000)
162
169
  a.valid?
163
170
  expect(a._errors[:salary].size).to eq(0)
164
- end
171
+ end
165
172
 
166
173
  it "should be valid if a value is nil and allow_nil option is true" do
167
174
  a = SimpleValidationExample.new
@@ -205,13 +212,13 @@ describe "Flexirest::Validation" do
205
212
  a = SimpleValidationExample.new(age: 70)
206
213
  a.valid?
207
214
  expect(a._errors[:age].size).to be > 0
208
- end
215
+ end
209
216
 
210
217
  it "should be valid that a numeric field is above or equal to a minimum" do
211
218
  a = SimpleValidationExample.new(age: 30)
212
219
  a.valid?
213
220
  expect(a._errors[:age].size).to eq(0)
214
- end
221
+ end
215
222
  end
216
223
  end
217
224
 
@@ -219,15 +226,15 @@ describe "Flexirest::Validation" do
219
226
  it "should be invalid if the value is not contained in the list" do
220
227
  a = SimpleValidationExample.new(suffix: "Baz")
221
228
  a.valid?
222
- expect(a._errors[:suffix].size).to be > 0
229
+ expect(a._errors[:suffix].size).to be > 0
223
230
  end
224
231
 
225
232
  it "should be valid if the value is contained in the list" do
226
233
  a = SimpleValidationExample.new(suffix: "Dr.")
227
234
  a.valid?
228
- expect(a._errors[:suffix].size).to eq(0)
235
+ expect(a._errors[:suffix].size).to eq(0)
229
236
  end
230
-
237
+
231
238
  it "should be valid if the value is nil and allow_nil option is true" do
232
239
  a = SimpleValidationExample.new
233
240
  a.valid?
@@ -270,7 +277,7 @@ describe "Flexirest::Validation" do
270
277
  a = ValidationExample2.new(first_name:"Johnny")
271
278
  a.valid?
272
279
  expect(a._errors[:first_name]).to be_empty
273
- end
280
+ end
274
281
  end
275
282
 
276
283
  describe "#full_error_messages" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flexirest
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.32
4
+ version: 1.3.33
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Jeffries
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-01-24 00:00:00.000000000 Z
11
+ date: 2017-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler