formeze 3.0.0 → 3.1.0
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 +5 -5
- data/CHANGES.md +156 -0
- data/LICENSE.txt +1 -6
- data/README.md +12 -19
- data/formeze.gemspec +10 -8
- data/lib/formeze.rb +38 -13
- metadata +12 -66
- data/Rakefile.rb +0 -8
- data/spec/formeze_spec.rb +0 -1018
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: b67d9270f16eb5af01a76ed46059e8c61b14c67753f7ad05303a687a2741d2f6
|
4
|
+
data.tar.gz: b1ea76abe102e3a554f66af48253de9497b8786742f01c7f124dd8187ccde593
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: edc08ae4719627987bff853db4e82f021e1fb340aff32cf0cd1583d4943a6efb598addcf13223b8fcbb2d5d1c00728dd634f0be55b49daf40e2b63a6cc5f6214
|
7
|
+
data.tar.gz: 442b1da170d85a21b3c5c14b675802605275f6c0cd327cb007ba0f3009dcf6b010f7bc941051747f31aab9c0d7262f1e8ea307e287297e584b85eedbb6a94e28
|
data/CHANGES.md
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
# 3.1.0
|
2
|
+
|
3
|
+
* Added `'commit'` to the list of Rails form keys to ignore (#4)
|
4
|
+
|
5
|
+
* Added frozen string literal comment
|
6
|
+
|
7
|
+
* Extracted private constants to reduce memory allocations
|
8
|
+
|
9
|
+
* Removed spec file from gem
|
10
|
+
|
11
|
+
# 3.0.0
|
12
|
+
|
13
|
+
* Added functionality for handling multipart form data. For example:
|
14
|
+
|
15
|
+
class ExampleForm < Formeze::Form
|
16
|
+
field :image, accept: 'image/jpg,image/png', maxsize: 1000
|
17
|
+
end
|
18
|
+
|
19
|
+
For this to work the request needs to be passed to the parse method:
|
20
|
+
|
21
|
+
ExampleForm.new.parse(request)
|
22
|
+
|
23
|
+
* Removed the deprecated parse class method
|
24
|
+
|
25
|
+
* Removed Ruby 1.8.7 compatibility
|
26
|
+
|
27
|
+
# 2.2.0
|
28
|
+
|
29
|
+
* The #fill and #parse instance methods now return self. So instead of this:
|
30
|
+
|
31
|
+
form = ExampleForm.new
|
32
|
+
form.parse(request.raw_post)
|
33
|
+
|
34
|
+
You can now do this:
|
35
|
+
|
36
|
+
form = ExampleForm.new.parse(request.raw_post)
|
37
|
+
|
38
|
+
* Deprecated the parse class method
|
39
|
+
|
40
|
+
# 2.1.1
|
41
|
+
|
42
|
+
* Fixed that custom validation should not execute for optional fields with blank values
|
43
|
+
|
44
|
+
# 2.1.0
|
45
|
+
|
46
|
+
* Fixed that custom validation should only execute when there are no existing errors on the associated field
|
47
|
+
|
48
|
+
* Removed `:word_limit` field option
|
49
|
+
|
50
|
+
# 2.0.0
|
51
|
+
|
52
|
+
* Added new custom validation functionality
|
53
|
+
|
54
|
+
* Removed existing (undocumented) custom validation functionality
|
55
|
+
|
56
|
+
* KeyError now includes an error message when raised for unexpected keys
|
57
|
+
|
58
|
+
* Added #to_h form instance method
|
59
|
+
|
60
|
+
* Removed `:char_limit` field option
|
61
|
+
|
62
|
+
* Deprecated `:word_limit` field option (use custom validation instead)
|
63
|
+
|
64
|
+
# 1.9.1
|
65
|
+
|
66
|
+
* Added `:minlength` field option
|
67
|
+
|
68
|
+
* Added `:maxlength` field option
|
69
|
+
|
70
|
+
* Deprecated `:char_limit` field option (use `:maxlength` instead)
|
71
|
+
|
72
|
+
# 1.9.0
|
73
|
+
|
74
|
+
* Added `:blank` field option for specifying a null object to be used in place of blank input
|
75
|
+
|
76
|
+
# 1.8.0
|
77
|
+
|
78
|
+
* Added #fill instance method
|
79
|
+
|
80
|
+
* Improved handling of Rails utf8/authenticity_token parameters
|
81
|
+
|
82
|
+
# 1.7.0
|
83
|
+
|
84
|
+
* Ruby 1.8.7 compatibility
|
85
|
+
|
86
|
+
* Renamed `Formeze::UserError` to `Formeze::ValidationError`
|
87
|
+
|
88
|
+
* Added #to_hash instance method
|
89
|
+
|
90
|
+
# 1.6.0
|
91
|
+
|
92
|
+
* Added #errors_on? instance method for checking if there are errors on a specific field
|
93
|
+
|
94
|
+
* Added #errors_on instance method for accessing the errors on a specific field
|
95
|
+
|
96
|
+
* Added parse class method, so instead of this:
|
97
|
+
|
98
|
+
form = ExampleForm.new
|
99
|
+
form.parse(request.raw_post)
|
100
|
+
|
101
|
+
You can now do this:
|
102
|
+
|
103
|
+
form = ExampleForm.parse(request.raw_post)
|
104
|
+
|
105
|
+
# 1.5.1
|
106
|
+
|
107
|
+
* Added `Formeze::Form` class, so forms can now be defined like this:
|
108
|
+
|
109
|
+
class ExampleForm < Formeze::Form
|
110
|
+
end
|
111
|
+
|
112
|
+
The previous style of setup is still supported:
|
113
|
+
|
114
|
+
class ExampleForm < SomeAncestorClass
|
115
|
+
Formeze.setup(self)
|
116
|
+
end
|
117
|
+
|
118
|
+
# 1.5.0
|
119
|
+
|
120
|
+
* Added #errors? instance method
|
121
|
+
|
122
|
+
* Added `Formeze.scrub` method so that the scrub methods can be re-used outside field validation
|
123
|
+
|
124
|
+
# 1.4.0
|
125
|
+
|
126
|
+
* Added `:scrub` field option for cleaning up input data before validation
|
127
|
+
|
128
|
+
# 1.3.0
|
129
|
+
|
130
|
+
* Added functionality for overriding error messages via i18n
|
131
|
+
|
132
|
+
* Added functionality for setting field labels globally via i18n
|
133
|
+
|
134
|
+
# 1.2.0
|
135
|
+
|
136
|
+
* Replaced experimental guard/halting functionality with `:defined_if` and `:defined_unless` field options
|
137
|
+
|
138
|
+
# 1.1.3
|
139
|
+
|
140
|
+
* Fixed early return from guard/halting conditions
|
141
|
+
|
142
|
+
# 1.1.2
|
143
|
+
|
144
|
+
* Fixed validation so that additional checks are skipped if the input is blank
|
145
|
+
|
146
|
+
# 1.1.1
|
147
|
+
|
148
|
+
* Added an error message for `Formeze::KeyError` exceptions
|
149
|
+
|
150
|
+
# 1.1.0
|
151
|
+
|
152
|
+
* Changed behaviour of experimental guard conditions and added halting conditions with opposite behaviour
|
153
|
+
|
154
|
+
# 1.0.0
|
155
|
+
|
156
|
+
* First version!
|
data/LICENSE.txt
CHANGED
@@ -1,9 +1,4 @@
|
|
1
|
-
Copyright (c) TIMCRAFT
|
1
|
+
Copyright (c) 2012-2020 TIMCRAFT
|
2
2
|
|
3
3
|
This is an Open Source project licensed under the terms of the LGPLv3 license.
|
4
4
|
Please see <http://www.gnu.org/licenses/lgpl-3.0.html> for license text.
|
5
|
-
|
6
|
-
This code is distributed in the hope that it will be useful,
|
7
|
-
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
8
|
-
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
9
|
-
GNU Lesser General Public License for more details.
|
data/README.md
CHANGED
@@ -1,12 +1,12 @@
|
|
1
|
-
formeze
|
2
|
-
=======
|
1
|
+
# formeze
|
3
2
|
|
3
|
+

|
4
|
+

|
4
5
|
|
5
|
-
A
|
6
|
+
A lightweight Ruby library for processing form data.
|
6
7
|
|
7
8
|
|
8
|
-
Motivation
|
9
|
-
----------
|
9
|
+
## Motivation
|
10
10
|
|
11
11
|
Most web apps built for end users will need to process url-encoded form data.
|
12
12
|
Registration forms, profile forms, checkout forms, contact forms, and forms
|
@@ -18,16 +18,14 @@ Formeze adopts the approach of being "strict by default", forcing the applicatio
|
|
18
18
|
code to be explicit in what it accepts as input.
|
19
19
|
|
20
20
|
|
21
|
-
Installation
|
22
|
-
------------
|
21
|
+
## Installation
|
23
22
|
|
24
23
|
```
|
25
24
|
$ gem install formeze
|
26
25
|
```
|
27
26
|
|
28
27
|
|
29
|
-
Example usage
|
30
|
-
-------------
|
28
|
+
## Example usage
|
31
29
|
|
32
30
|
Here is a minimal example, which defines a form with a single field:
|
33
31
|
|
@@ -69,8 +67,7 @@ methods but will otherwise leave the object untouched (i.e. you can define
|
|
69
67
|
your own initialization logic).
|
70
68
|
|
71
69
|
|
72
|
-
Validation errors
|
73
|
-
-----------------
|
70
|
+
## Validation errors
|
74
71
|
|
75
72
|
Formeze distinguishes between validation errors (which are expected in the
|
76
73
|
normal running of your application), and key/value errors (which most likely
|
@@ -86,8 +83,7 @@ use the `errors_on?` and `errors_on` methods to check for and select error
|
|
86
83
|
messages specific to a single field.
|
87
84
|
|
88
85
|
|
89
|
-
Field options
|
90
|
-
-------------
|
86
|
+
## Field options
|
91
87
|
|
92
88
|
By default fields cannot be blank, they are limited to 64 characters,
|
93
89
|
and they cannot contain newlines. These restrictions can be overridden
|
@@ -211,8 +207,7 @@ Custom scrub methods can be defined by adding a symbol/proc entry to the
|
|
211
207
|
`Formeze.scrub_methods` hash.
|
212
208
|
|
213
209
|
|
214
|
-
Multipart form data
|
215
|
-
-------------------
|
210
|
+
## Multipart form data
|
216
211
|
|
217
212
|
For file fields you can specify the `accept` and `maxsize` options, for example:
|
218
213
|
|
@@ -227,8 +222,7 @@ For this to work you need to make sure your application includes the
|
|
227
222
|
form is submitted with the multipart/form-data mime type.
|
228
223
|
|
229
224
|
|
230
|
-
Custom validation
|
231
|
-
-----------------
|
225
|
+
## Custom validation
|
232
226
|
|
233
227
|
You may need additional validation logic beyond what the field options
|
234
228
|
described above provide, such as validating the format of a field without
|
@@ -308,8 +302,7 @@ key does not exist. The error for the password_confirmation field validation
|
|
308
302
|
would include the value of the `formeze.errors.does_not_match` I18n key.
|
309
303
|
|
310
304
|
|
311
|
-
I18n integration
|
312
|
-
----------------
|
305
|
+
## I18n integration
|
313
306
|
|
314
307
|
Formeze integrates with [I18n](http://edgeguides.rubyonrails.org/i18n.html)
|
315
308
|
so that you can define custom error messages and field labels within your
|
data/formeze.gemspec
CHANGED
@@ -1,18 +1,20 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'formeze'
|
3
|
-
s.version = '3.
|
3
|
+
s.version = '3.1.0'
|
4
4
|
s.license = 'LGPL-3.0'
|
5
5
|
s.platform = Gem::Platform::RUBY
|
6
6
|
s.authors = ['Tim Craft']
|
7
7
|
s.email = ['mail@timcraft.com']
|
8
|
-
s.homepage = '
|
9
|
-
s.description = 'A
|
8
|
+
s.homepage = 'https://github.com/readysteady/formeze'
|
9
|
+
s.description = 'A lightweight Ruby library for processing form data'
|
10
10
|
s.summary = 'See description'
|
11
|
-
s.files = Dir.glob('
|
11
|
+
s.files = Dir.glob('lib/**/*.rb') + %w(CHANGES.md LICENSE.txt README.md formeze.gemspec)
|
12
12
|
s.required_ruby_version = '>= 1.9.3'
|
13
|
-
s.add_development_dependency('rake', '~> 10')
|
14
|
-
s.add_development_dependency('i18n', '~> 0.6')
|
15
|
-
s.add_development_dependency('minitest', '~> 5')
|
16
|
-
s.add_development_dependency('mime-types', '~> 2')
|
17
13
|
s.require_path = 'lib'
|
14
|
+
s.metadata = {
|
15
|
+
'homepage' => 'https://github.com/readysteady/formeze',
|
16
|
+
'source_code_uri' => 'https://github.com/readysteady/formeze',
|
17
|
+
'bug_tracker_uri' => 'https://github.com/readysteady/formeze/issues',
|
18
|
+
'changelog_uri' => 'https://github.com/readysteady/formeze/blob/master/CHANGES.md'
|
19
|
+
}
|
18
20
|
end
|
data/lib/formeze.rb
CHANGED
@@ -1,7 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
require 'cgi'
|
2
3
|
|
3
4
|
module Formeze
|
5
|
+
module Presence
|
6
|
+
REGEXP = /\S/
|
7
|
+
|
8
|
+
def present?(string)
|
9
|
+
string =~ REGEXP
|
10
|
+
end
|
11
|
+
|
12
|
+
def blank?(string)
|
13
|
+
string !~ REGEXP
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
private_constant :Presence
|
18
|
+
|
4
19
|
class Field
|
20
|
+
include Presence
|
21
|
+
|
5
22
|
attr_reader :name
|
6
23
|
|
7
24
|
def initialize(name, options = {})
|
@@ -27,7 +44,7 @@ module Formeze
|
|
27
44
|
def validate(value, form)
|
28
45
|
value = Formeze.scrub(value, @options[:scrub])
|
29
46
|
|
30
|
-
if value
|
47
|
+
if blank?(value)
|
31
48
|
form.add_error(self, error(:required, 'is required')) if required?
|
32
49
|
|
33
50
|
value = blank_value if blank_value?
|
@@ -91,7 +108,7 @@ module Formeze
|
|
91
108
|
end
|
92
109
|
|
93
110
|
def maxsize?
|
94
|
-
@options.
|
111
|
+
@options.key?(:maxsize)
|
95
112
|
end
|
96
113
|
|
97
114
|
def maxsize
|
@@ -107,15 +124,15 @@ module Formeze
|
|
107
124
|
end
|
108
125
|
|
109
126
|
def too_short?(value)
|
110
|
-
@options.
|
127
|
+
@options.key?(:minlength) && value.chars.count < @options.fetch(:minlength)
|
111
128
|
end
|
112
129
|
|
113
130
|
def no_match?(value)
|
114
|
-
@options.
|
131
|
+
@options.key?(:pattern) && value !~ @options[:pattern]
|
115
132
|
end
|
116
133
|
|
117
134
|
def blank_value?
|
118
|
-
@options.
|
135
|
+
@options.key?(:blank)
|
119
136
|
end
|
120
137
|
|
121
138
|
def blank_value
|
@@ -123,7 +140,7 @@ module Formeze
|
|
123
140
|
end
|
124
141
|
|
125
142
|
def values?
|
126
|
-
@options.
|
143
|
+
@options.key?(:values)
|
127
144
|
end
|
128
145
|
|
129
146
|
def values
|
@@ -131,7 +148,7 @@ module Formeze
|
|
131
148
|
end
|
132
149
|
|
133
150
|
def defined_if?
|
134
|
-
@options.
|
151
|
+
@options.key?(:defined_if)
|
135
152
|
end
|
136
153
|
|
137
154
|
def defined_if
|
@@ -139,7 +156,7 @@ module Formeze
|
|
139
156
|
end
|
140
157
|
|
141
158
|
def defined_unless?
|
142
|
-
@options.
|
159
|
+
@options.key?(:defined_unless)
|
143
160
|
end
|
144
161
|
|
145
162
|
def defined_unless
|
@@ -148,6 +165,8 @@ module Formeze
|
|
148
165
|
end
|
149
166
|
|
150
167
|
class Validation
|
168
|
+
include Presence
|
169
|
+
|
151
170
|
def initialize(field, options, &block)
|
152
171
|
@field, @options, @block = field, options, block
|
153
172
|
end
|
@@ -161,11 +180,11 @@ module Formeze
|
|
161
180
|
end
|
162
181
|
|
163
182
|
def validates?(form)
|
164
|
-
@options.
|
183
|
+
@options.key?(:when) ? form.instance_eval(&@options[:when]) : true
|
165
184
|
end
|
166
185
|
|
167
186
|
def field_value?(form)
|
168
|
-
form.send(@field.name)
|
187
|
+
present?(form.send(@field.name))
|
169
188
|
end
|
170
189
|
|
171
190
|
def field_errors?(form)
|
@@ -223,10 +242,16 @@ module Formeze
|
|
223
242
|
end
|
224
243
|
end
|
225
244
|
|
245
|
+
private_constant :RequestCGI
|
246
|
+
|
247
|
+
RAILS_FORM_KEYS = %w[utf8 authenticity_token commit]
|
248
|
+
|
249
|
+
private_constant :RAILS_FORM_KEYS
|
250
|
+
|
226
251
|
module InstanceMethods
|
227
252
|
def fill(object)
|
228
253
|
self.class.fields.each_value do |field|
|
229
|
-
if Hash === object && object.
|
254
|
+
if Hash === object && object.key?(field.name)
|
230
255
|
send(:"#{field.name}=", object[field.name])
|
231
256
|
elsif object.respond_to?(field.name)
|
232
257
|
send(:"#{field.name}=", object.send(field.name))
|
@@ -246,7 +271,7 @@ module Formeze
|
|
246
271
|
self.class.fields.each_value do |field|
|
247
272
|
next unless field_defined?(field)
|
248
273
|
|
249
|
-
unless form_data.
|
274
|
+
unless form_data.key?(field.key)
|
250
275
|
next if field.multiple? || !field.key_required?
|
251
276
|
|
252
277
|
raise KeyError, "missing form key: #{field.key}"
|
@@ -262,7 +287,7 @@ module Formeze
|
|
262
287
|
end
|
263
288
|
|
264
289
|
if defined?(Rails)
|
265
|
-
|
290
|
+
RAILS_FORM_KEYS.each do |key|
|
266
291
|
form_data.delete(key)
|
267
292
|
end
|
268
293
|
end
|
metadata
CHANGED
@@ -1,88 +1,35 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: formeze
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tim Craft
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
12
|
-
dependencies:
|
13
|
-
|
14
|
-
name: rake
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - "~>"
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '10'
|
20
|
-
type: :development
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - "~>"
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '10'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: i18n
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
30
|
-
requirements:
|
31
|
-
- - "~>"
|
32
|
-
- !ruby/object:Gem::Version
|
33
|
-
version: '0.6'
|
34
|
-
type: :development
|
35
|
-
prerelease: false
|
36
|
-
version_requirements: !ruby/object:Gem::Requirement
|
37
|
-
requirements:
|
38
|
-
- - "~>"
|
39
|
-
- !ruby/object:Gem::Version
|
40
|
-
version: '0.6'
|
41
|
-
- !ruby/object:Gem::Dependency
|
42
|
-
name: minitest
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: '5'
|
48
|
-
type: :development
|
49
|
-
prerelease: false
|
50
|
-
version_requirements: !ruby/object:Gem::Requirement
|
51
|
-
requirements:
|
52
|
-
- - "~>"
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
version: '5'
|
55
|
-
- !ruby/object:Gem::Dependency
|
56
|
-
name: mime-types
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
58
|
-
requirements:
|
59
|
-
- - "~>"
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '2'
|
62
|
-
type: :development
|
63
|
-
prerelease: false
|
64
|
-
version_requirements: !ruby/object:Gem::Requirement
|
65
|
-
requirements:
|
66
|
-
- - "~>"
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
version: '2'
|
69
|
-
description: A little Ruby library for handling form data/input
|
11
|
+
date: 2020-04-09 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A lightweight Ruby library for processing form data
|
70
14
|
email:
|
71
15
|
- mail@timcraft.com
|
72
16
|
executables: []
|
73
17
|
extensions: []
|
74
18
|
extra_rdoc_files: []
|
75
19
|
files:
|
20
|
+
- CHANGES.md
|
76
21
|
- LICENSE.txt
|
77
22
|
- README.md
|
78
|
-
- Rakefile.rb
|
79
23
|
- formeze.gemspec
|
80
24
|
- lib/formeze.rb
|
81
|
-
|
82
|
-
homepage: http://github.com/timcraft/formeze
|
25
|
+
homepage: https://github.com/readysteady/formeze
|
83
26
|
licenses:
|
84
27
|
- LGPL-3.0
|
85
|
-
metadata:
|
28
|
+
metadata:
|
29
|
+
homepage: https://github.com/readysteady/formeze
|
30
|
+
source_code_uri: https://github.com/readysteady/formeze
|
31
|
+
bug_tracker_uri: https://github.com/readysteady/formeze/issues
|
32
|
+
changelog_uri: https://github.com/readysteady/formeze/blob/master/CHANGES.md
|
86
33
|
post_install_message:
|
87
34
|
rdoc_options: []
|
88
35
|
require_paths:
|
@@ -98,8 +45,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
98
45
|
- !ruby/object:Gem::Version
|
99
46
|
version: '0'
|
100
47
|
requirements: []
|
101
|
-
|
102
|
-
rubygems_version: 2.2.2
|
48
|
+
rubygems_version: 3.1.2
|
103
49
|
signing_key:
|
104
50
|
specification_version: 4
|
105
51
|
summary: See description
|