judge 1.5.0 → 2.0.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.
- data/README.md +273 -14
- data/app/assets/javascripts/judge.js +391 -0
- data/app/controllers/judge/validations_controller.rb +9 -0
- data/app/models/judge/validation.rb +53 -0
- data/config/routes.rb +3 -0
- data/lib/generators/judge/install/install_generator.rb +42 -0
- data/lib/judge.rb +13 -8
- data/lib/judge/config.rb +39 -0
- data/lib/judge/controller.rb +35 -0
- data/lib/judge/each_validator.rb +5 -4
- data/lib/judge/engine.rb +5 -0
- data/lib/judge/form_builder.rb +19 -24
- data/lib/judge/html.rb +5 -7
- data/lib/judge/message_collection.rb +2 -1
- data/lib/judge/message_config.rb +2 -1
- data/lib/judge/validator.rb +3 -1
- data/lib/judge/validator_collection.rb +1 -1
- data/lib/judge/version.rb +2 -2
- data/lib/tasks/judge_tasks.rake +4 -0
- data/{lib/generators/judge/templates → vendor/assets/javascripts}/json2.js +4 -5
- data/{lib/generators/judge/templates → vendor/assets/javascripts}/underscore.js +451 -285
- metadata +94 -87
- data/.gitignore +0 -9
- data/.travis.yml +0 -21
- data/Gemfile +0 -3
- data/Rakefile +0 -11
- data/judge.gemspec +0 -23
- data/lib/generators/judge/judge_generator.rb +0 -21
- data/lib/generators/judge/templates/judge.js +0 -330
- data/spec/each_validator_spec.rb +0 -17
- data/spec/form_builder_spec.rb +0 -68
- data/spec/html_spec.rb +0 -14
- data/spec/javascripts/JudgeSpec.js +0 -509
- data/spec/javascripts/fixtures/form.html +0 -538
- data/spec/javascripts/helpers/customMatchers.js +0 -20
- data/spec/javascripts/helpers/jasmine-jquery.js +0 -204
- data/spec/javascripts/helpers/jquery-1.5.1.min.js +0 -18
- data/spec/javascripts/helpers/json2.js +0 -487
- data/spec/javascripts/helpers/underscore.js +0 -1060
- data/spec/javascripts/support/jasmine.yml +0 -79
- data/spec/javascripts/support/jasmine_config.rb +0 -6
- data/spec/javascripts/support/jasmine_runner.rb +0 -21
- data/spec/javascripts/support/runner.js +0 -51
- data/spec/message_collection_spec.rb +0 -73
- data/spec/setup.rb +0 -75
- data/spec/support/factories.rb +0 -23
- data/spec/support/locale/en.yml +0 -18
- data/spec/support/setup.rb +0 -72
- data/spec/support/spec_helper.rb +0 -13
- data/spec/support/validators/city_validator.rb +0 -9
- data/spec/validator_collection_spec.rb +0 -21
- data/spec/validator_spec.rb +0 -33
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: judge
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,27 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-02-27 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.1'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.1'
|
14
30
|
- !ruby/object:Gem::Dependency
|
15
31
|
name: jasmine
|
16
|
-
requirement:
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
17
33
|
none: false
|
18
34
|
requirements:
|
19
35
|
- - ~>
|
@@ -21,70 +37,107 @@ dependencies:
|
|
21
37
|
version: 1.1.2
|
22
38
|
type: :development
|
23
39
|
prerelease: false
|
24
|
-
version_requirements:
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.1.2
|
25
46
|
- !ruby/object:Gem::Dependency
|
26
|
-
name: rails
|
27
|
-
requirement:
|
47
|
+
name: rspec-rails
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
28
49
|
none: false
|
29
50
|
requirements:
|
30
51
|
- - ~>
|
31
52
|
- !ruby/object:Gem::Version
|
32
|
-
version:
|
53
|
+
version: 2.13.0
|
33
54
|
type: :development
|
34
55
|
prerelease: false
|
35
|
-
version_requirements:
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: rspec
|
38
|
-
requirement: &2168504460 !ruby/object:Gem::Requirement
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
57
|
none: false
|
40
58
|
requirements:
|
41
59
|
- - ~>
|
42
60
|
- !ruby/object:Gem::Version
|
43
|
-
version:
|
61
|
+
version: 2.13.0
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: jquery-rails
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
44
70
|
type: :development
|
45
71
|
prerelease: false
|
46
|
-
version_requirements:
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
47
78
|
- !ruby/object:Gem::Dependency
|
48
79
|
name: sqlite3
|
49
|
-
requirement:
|
80
|
+
requirement: !ruby/object:Gem::Requirement
|
50
81
|
none: false
|
51
82
|
requirements:
|
52
83
|
- - ~>
|
53
84
|
- !ruby/object:Gem::Version
|
54
|
-
version: 1.3.
|
85
|
+
version: 1.3.7
|
55
86
|
type: :development
|
56
87
|
prerelease: false
|
57
|
-
version_requirements:
|
88
|
+
version_requirements: !ruby/object:Gem::Requirement
|
89
|
+
none: false
|
90
|
+
requirements:
|
91
|
+
- - ~>
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: 1.3.7
|
58
94
|
- !ruby/object:Gem::Dependency
|
59
95
|
name: factory_girl
|
60
|
-
requirement:
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ~>
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '2.6'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
61
105
|
none: false
|
62
106
|
requirements:
|
63
107
|
- - ~>
|
64
108
|
- !ruby/object:Gem::Version
|
65
109
|
version: '2.6'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: appraisal
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ~>
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.5.1
|
66
118
|
type: :development
|
67
119
|
prerelease: false
|
68
|
-
version_requirements:
|
69
|
-
|
70
|
-
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
none: false
|
122
|
+
requirements:
|
123
|
+
- - ~>
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: 0.5.1
|
126
|
+
description: Validate Rails 3 forms on the client side, simply
|
127
|
+
email: joecorcoran@gmail.com
|
71
128
|
executables: []
|
72
129
|
extensions: []
|
73
130
|
extra_rdoc_files: []
|
74
131
|
files:
|
75
|
-
- .
|
76
|
-
- .
|
77
|
-
-
|
78
|
-
-
|
79
|
-
-
|
80
|
-
-
|
81
|
-
- judge.
|
82
|
-
- lib/generators/judge/judge_generator.rb
|
83
|
-
- lib/generators/judge/templates/json2.js
|
84
|
-
- lib/generators/judge/templates/judge.js
|
85
|
-
- lib/generators/judge/templates/underscore.js
|
86
|
-
- lib/judge.rb
|
132
|
+
- app/assets/javascripts/judge.js
|
133
|
+
- app/controllers/judge/validations_controller.rb
|
134
|
+
- app/models/judge/validation.rb
|
135
|
+
- config/routes.rb
|
136
|
+
- lib/generators/judge/install/install_generator.rb
|
137
|
+
- lib/judge/config.rb
|
138
|
+
- lib/judge/controller.rb
|
87
139
|
- lib/judge/each_validator.rb
|
140
|
+
- lib/judge/engine.rb
|
88
141
|
- lib/judge/form_builder.rb
|
89
142
|
- lib/judge/html.rb
|
90
143
|
- lib/judge/message_collection.rb
|
@@ -92,30 +145,13 @@ files:
|
|
92
145
|
- lib/judge/validator.rb
|
93
146
|
- lib/judge/validator_collection.rb
|
94
147
|
- lib/judge/version.rb
|
148
|
+
- lib/judge.rb
|
95
149
|
- lib/tasks/js.rake
|
96
|
-
-
|
97
|
-
-
|
98
|
-
-
|
99
|
-
-
|
100
|
-
-
|
101
|
-
- spec/javascripts/helpers/customMatchers.js
|
102
|
-
- spec/javascripts/helpers/jasmine-jquery.js
|
103
|
-
- spec/javascripts/helpers/jquery-1.5.1.min.js
|
104
|
-
- spec/javascripts/helpers/json2.js
|
105
|
-
- spec/javascripts/helpers/underscore.js
|
106
|
-
- spec/javascripts/support/jasmine.yml
|
107
|
-
- spec/javascripts/support/jasmine_config.rb
|
108
|
-
- spec/javascripts/support/jasmine_runner.rb
|
109
|
-
- spec/javascripts/support/runner.js
|
110
|
-
- spec/message_collection_spec.rb
|
111
|
-
- spec/setup.rb
|
112
|
-
- spec/support/factories.rb
|
113
|
-
- spec/support/locale/en.yml
|
114
|
-
- spec/support/setup.rb
|
115
|
-
- spec/support/spec_helper.rb
|
116
|
-
- spec/support/validators/city_validator.rb
|
117
|
-
- spec/validator_collection_spec.rb
|
118
|
-
- spec/validator_spec.rb
|
150
|
+
- lib/tasks/judge_tasks.rake
|
151
|
+
- vendor/assets/javascripts/json2.js
|
152
|
+
- vendor/assets/javascripts/underscore.js
|
153
|
+
- LICENSE.txt
|
154
|
+
- README.md
|
119
155
|
homepage: http://github.com/joecorcoran/judge
|
120
156
|
licenses: []
|
121
157
|
post_install_message:
|
@@ -128,45 +164,16 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
128
164
|
- - ! '>='
|
129
165
|
- !ruby/object:Gem::Version
|
130
166
|
version: '0'
|
131
|
-
segments:
|
132
|
-
- 0
|
133
|
-
hash: -172077303514846172
|
134
167
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
135
168
|
none: false
|
136
169
|
requirements:
|
137
170
|
- - ! '>='
|
138
171
|
- !ruby/object:Gem::Version
|
139
172
|
version: '0'
|
140
|
-
segments:
|
141
|
-
- 0
|
142
|
-
hash: -172077303514846172
|
143
173
|
requirements: []
|
144
174
|
rubyforge_project:
|
145
|
-
rubygems_version: 1.8.
|
175
|
+
rubygems_version: 1.8.24
|
146
176
|
signing_key:
|
147
177
|
specification_version: 3
|
148
|
-
summary:
|
149
|
-
test_files:
|
150
|
-
- spec/each_validator_spec.rb
|
151
|
-
- spec/form_builder_spec.rb
|
152
|
-
- spec/html_spec.rb
|
153
|
-
- spec/javascripts/JudgeSpec.js
|
154
|
-
- spec/javascripts/fixtures/form.html
|
155
|
-
- spec/javascripts/helpers/customMatchers.js
|
156
|
-
- spec/javascripts/helpers/jasmine-jquery.js
|
157
|
-
- spec/javascripts/helpers/jquery-1.5.1.min.js
|
158
|
-
- spec/javascripts/helpers/json2.js
|
159
|
-
- spec/javascripts/helpers/underscore.js
|
160
|
-
- spec/javascripts/support/jasmine.yml
|
161
|
-
- spec/javascripts/support/jasmine_config.rb
|
162
|
-
- spec/javascripts/support/jasmine_runner.rb
|
163
|
-
- spec/javascripts/support/runner.js
|
164
|
-
- spec/message_collection_spec.rb
|
165
|
-
- spec/setup.rb
|
166
|
-
- spec/support/factories.rb
|
167
|
-
- spec/support/locale/en.yml
|
168
|
-
- spec/support/setup.rb
|
169
|
-
- spec/support/spec_helper.rb
|
170
|
-
- spec/support/validators/city_validator.rb
|
171
|
-
- spec/validator_collection_spec.rb
|
172
|
-
- spec/validator_spec.rb
|
178
|
+
summary: Client side validation for Rails
|
179
|
+
test_files: []
|
data/.gitignore
DELETED
data/.travis.yml
DELETED
@@ -1,21 +0,0 @@
|
|
1
|
-
rvm:
|
2
|
-
- 1.9.2
|
3
|
-
- 1.9.3
|
4
|
-
|
5
|
-
before_script:
|
6
|
-
- "export DISPLAY=:99.0"
|
7
|
-
- "sh -e /etc/init.d/xvfb start"
|
8
|
-
- "git clone git://github.com/n1k0/casperjs.git"
|
9
|
-
- "cd casperjs"
|
10
|
-
- "git checkout tags/0.6.5"
|
11
|
-
- "sudo ln -sf `pwd`/bin/casperjs /usr/local/bin/casperjs"
|
12
|
-
|
13
|
-
script: "bundle exec rake"
|
14
|
-
|
15
|
-
notifications:
|
16
|
-
recipients:
|
17
|
-
- joe@tribesports.com
|
18
|
-
|
19
|
-
branches:
|
20
|
-
only:
|
21
|
-
- master
|
data/Gemfile
DELETED
data/Rakefile
DELETED
data/judge.gemspec
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
$:.push File.expand_path("../lib", __FILE__)
|
3
|
-
require "judge/version"
|
4
|
-
|
5
|
-
Gem::Specification.new do |s|
|
6
|
-
s.name = "judge"
|
7
|
-
s.version = Judge::VERSION
|
8
|
-
s.homepage = "http://github.com/joecorcoran/judge"
|
9
|
-
s.summary = "Simple client side ActiveModel::Validators"
|
10
|
-
s.description = "Validate Rails 3 forms on the client side, cleanly"
|
11
|
-
s.email = "joe@tribesports.com"
|
12
|
-
s.authors = ["Joe Corcoran"]
|
13
|
-
|
14
|
-
s.files = `git ls-files`.split("\n")
|
15
|
-
s.test_files = `git ls-files -- spec/*`.split("\n")
|
16
|
-
s.require_paths = ["lib"]
|
17
|
-
|
18
|
-
s.add_development_dependency "jasmine", "~> 1.1.2"
|
19
|
-
s.add_development_dependency "rails", "~> 3.2"
|
20
|
-
s.add_development_dependency "rspec", "~> 2.8"
|
21
|
-
s.add_development_dependency "sqlite3", "~> 1.3.5"
|
22
|
-
s.add_development_dependency "factory_girl", "~> 2.6"
|
23
|
-
end
|
@@ -1,21 +0,0 @@
|
|
1
|
-
class JudgeGenerator < Rails::Generators::Base
|
2
|
-
source_root File.expand_path("../templates", __FILE__)
|
3
|
-
argument :path, :type => :string, :default => "public/javascripts"
|
4
|
-
class_option :dep, :type => :boolean, :default => true, :desc => "Copy underscore.js and json2.js"
|
5
|
-
|
6
|
-
desc %Q{
|
7
|
-
This copies judge.js (and dependencies) to your application.
|
8
|
-
The default path is public/javascripts, but you can set your own as follows:
|
9
|
-
|
10
|
-
rails generate judge my/super/cool/path
|
11
|
-
|
12
|
-
Use the --no-dep option if you don't want to copy underscore.js and json2.js too.
|
13
|
-
}
|
14
|
-
def copy_files
|
15
|
-
copy_file "judge.js", "#{path}/judge.js"
|
16
|
-
if options.dep?
|
17
|
-
copy_file "underscore.js", "#{path}/underscore.js"
|
18
|
-
copy_file "json2.js", "#{path}/json2.js"
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
@@ -1,330 +0,0 @@
|
|
1
|
-
// Judge 1.5.0
|
2
|
-
// (c) 2011–2012 Joe Corcoran
|
3
|
-
// http://raw.github.com/joecorcoran/judge/master/LICENSE.txt
|
4
|
-
// This is the JavaScript part of Judge, a client-side validation gem for Rails 3.
|
5
|
-
// You can find a guide and some more traditional API documentation at <http://joecorcoran.github.com/judge/>.
|
6
|
-
// Hopefully the comments here will help you understand what's happening under the hood.
|
7
|
-
|
8
|
-
/*jshint curly: true, evil: true, newcap: true, noarg: true, strict: false */
|
9
|
-
/*global _: false, JSON: false */
|
10
|
-
|
11
|
-
(function(root) {
|
12
|
-
|
13
|
-
// The judge namespace.
|
14
|
-
var judge = {};
|
15
|
-
|
16
|
-
judge.VERSION = '1.5.0';
|
17
|
-
|
18
|
-
// A judge.Watcher is a DOM element wrapper that judge uses to store validation info and instance methods.
|
19
|
-
judge.Watcher = function (element) {
|
20
|
-
// Throw dependency errors.
|
21
|
-
if (typeof root._ === 'undefined') {
|
22
|
-
throw new DependencyError('Ensure underscore.js is loaded');
|
23
|
-
}
|
24
|
-
if (_(root.JSON).isUndefined()) {
|
25
|
-
throw new DependencyError('Ensure that your browser provides the JSON object or that json2.js is loaded');
|
26
|
-
}
|
27
|
-
|
28
|
-
// Throw some constructor usage errors.
|
29
|
-
if (_(element).isUndefined()) {
|
30
|
-
throw new ReferenceError('No DOM element passed to judge.Watcher constructor');
|
31
|
-
}
|
32
|
-
if (element.getAttribute('data-validate') === null) {
|
33
|
-
throw new ReferenceError('DOM element does not have data-validate attribute – please use Judge::FormBuilder');
|
34
|
-
}
|
35
|
-
|
36
|
-
// Watcher instance properties.
|
37
|
-
this.element = element;
|
38
|
-
this.validators = JSON.parse(this.element.getAttribute('data-validate'));
|
39
|
-
};
|
40
|
-
|
41
|
-
// The `validate` method returns an object which describes the current validity of the
|
42
|
-
// value of the watched element.
|
43
|
-
judge.Watcher.prototype.validate = function(callback) {
|
44
|
-
var allMessages = [], isValid;
|
45
|
-
_(this.validators).each(function(v) {
|
46
|
-
if (this.element.value.length || v.options.allow_blank !== true) {
|
47
|
-
var messages = this.validates()[v.kind](this.element.value, v.options, v.messages);
|
48
|
-
if (messages.length) {
|
49
|
-
allMessages.push(messages);
|
50
|
-
}
|
51
|
-
}
|
52
|
-
}, this);
|
53
|
-
allMessages = _(allMessages).flatten();
|
54
|
-
isValid = (allMessages.length < 1);
|
55
|
-
if (_.isFunction(callback)) {
|
56
|
-
callback(isValid, allMessages, this.element);
|
57
|
-
}
|
58
|
-
return {
|
59
|
-
valid: isValid,
|
60
|
-
messages: allMessages,
|
61
|
-
element: this.element
|
62
|
-
};
|
63
|
-
};
|
64
|
-
|
65
|
-
// Ported ActiveModel validators.
|
66
|
-
// See <http://api.rubyonrails.org/classes/ActiveModel/Validations.html> for the originals.
|
67
|
-
judge.eachValidators = (function() {
|
68
|
-
return {
|
69
|
-
// ActiveModel::Validations::PresenceValidator
|
70
|
-
presence: function(value, options, messages) {
|
71
|
-
return (value.length) ? [] : [messages.blank];
|
72
|
-
},
|
73
|
-
|
74
|
-
// ActiveModel::Validations::LengthValidator
|
75
|
-
length: function(value, options, messages) {
|
76
|
-
var msgs = [],
|
77
|
-
types = {
|
78
|
-
minimum: { operator: '<', message: 'too_short' },
|
79
|
-
maximum: { operator: '>', message: 'too_long' },
|
80
|
-
is: { operator: '!=', message: 'wrong_length' }
|
81
|
-
};
|
82
|
-
_(types).each(function(properties, type) {
|
83
|
-
var invalid = operate(value.length, properties.operator, options[type]);
|
84
|
-
if (_(options).has(type) && invalid) {
|
85
|
-
msgs.push(messages[properties.message]);
|
86
|
-
}
|
87
|
-
});
|
88
|
-
return msgs;
|
89
|
-
},
|
90
|
-
|
91
|
-
// ActiveModel::Validations::ExclusionValidator
|
92
|
-
exclusion: function(value, options, messages) {
|
93
|
-
var stringIn = _(options['in']).map(function(o) { return o.toString(); });
|
94
|
-
return (_(stringIn).include(value)) ? [messages.exclusion] : [];
|
95
|
-
},
|
96
|
-
|
97
|
-
// ActiveModel::Validations::InclusionValidator
|
98
|
-
inclusion: function(value, options, messages) {
|
99
|
-
var stringIn = _(options['in']).map(function(o) { return o.toString(); });
|
100
|
-
return (!_(stringIn).include(value)) ? [messages.inclusion] : [];
|
101
|
-
},
|
102
|
-
|
103
|
-
// ActiveModel::Validations::NumericalityValidator
|
104
|
-
numericality: function(value, options, messages) {
|
105
|
-
var operators = {
|
106
|
-
greater_than: '>',
|
107
|
-
greater_than_or_equal_to: '>=',
|
108
|
-
equal_to: '==',
|
109
|
-
less_than: '<',
|
110
|
-
less_than_or_equal_to: '<='
|
111
|
-
},
|
112
|
-
msgs = [],
|
113
|
-
parsedValue = parseFloat(value, 10);
|
114
|
-
|
115
|
-
if (isNaN(Number(value))) {
|
116
|
-
msgs.push(messages.not_a_number);
|
117
|
-
} else {
|
118
|
-
if (options.odd && isEven(parsedValue)) {
|
119
|
-
msgs.push(messages.odd);
|
120
|
-
}
|
121
|
-
if (options.even && isOdd(parsedValue)) {
|
122
|
-
msgs.push(messages.even);
|
123
|
-
}
|
124
|
-
if (options.only_integer && !isInt(parsedValue)) {
|
125
|
-
msgs.push(messages.not_an_integer);
|
126
|
-
}
|
127
|
-
_(operators).each(function(operator, key) {
|
128
|
-
var valid = operate(parsedValue, operators[key], parseFloat(options[key], 10));
|
129
|
-
if (_(options).has(key) && !valid) {
|
130
|
-
msgs.push(messages[key]);
|
131
|
-
}
|
132
|
-
});
|
133
|
-
}
|
134
|
-
return msgs;
|
135
|
-
},
|
136
|
-
|
137
|
-
// ActiveModel::Validations::FormatValidator
|
138
|
-
format: function(value, options, messages) {
|
139
|
-
var msgs = [];
|
140
|
-
if (_(options).has('with')) {
|
141
|
-
var withReg = convertRegExp(options['with']);
|
142
|
-
if (!withReg.test(value)) {
|
143
|
-
msgs.push(messages.invalid);
|
144
|
-
}
|
145
|
-
}
|
146
|
-
if (_(options).has('without')) {
|
147
|
-
var withoutReg = convertRegExp(options.without);
|
148
|
-
if (withoutReg.test(value)) {
|
149
|
-
msgs.push(messages.invalid);
|
150
|
-
}
|
151
|
-
}
|
152
|
-
return msgs;
|
153
|
-
},
|
154
|
-
|
155
|
-
// ActiveModel::Validations::AcceptanceValidator
|
156
|
-
acceptance: function(value, options, messages) {
|
157
|
-
return (this._element.checked === true) ? [] : [messages.accepted];
|
158
|
-
},
|
159
|
-
|
160
|
-
// ActiveModel::Validations::ConfirmationValidator
|
161
|
-
confirmation: function(value, options, messages) {
|
162
|
-
var id = this._element.getAttribute('id'),
|
163
|
-
confId = id + '_confirmation',
|
164
|
-
confElem = document.getElementById(confId);
|
165
|
-
return (value === confElem.value) ? [] : [messages.confirmation];
|
166
|
-
}
|
167
|
-
};
|
168
|
-
|
169
|
-
})();
|
170
|
-
|
171
|
-
// This object should contain any judge validation methods
|
172
|
-
// that correspond to custom validators used in the model.
|
173
|
-
judge.customValidators = {};
|
174
|
-
|
175
|
-
// Return all validation methods, including those found in judge.customValidators.
|
176
|
-
// If you name a custom validation method the same as a default one, for example
|
177
|
-
// `judge.customValidators.presence = function() {};`
|
178
|
-
// then the custom method _will overwrite_ the default one, so be careful!
|
179
|
-
judge.Watcher.prototype.validates = function() {
|
180
|
-
return _.extend({_element: this.element}, judge.eachValidators, judge.customValidators);
|
181
|
-
};
|
182
|
-
|
183
|
-
// Validate all given element(s) without storing. Watcher creation is handled for you,
|
184
|
-
// but the created Watchers will not be returned, so it's less flexible.
|
185
|
-
judge.validate = function(elements, callback) {
|
186
|
-
var results = [];
|
187
|
-
elements = isCollection(elements) ? elements : [elements];
|
188
|
-
_(elements).each(function(element) {
|
189
|
-
var j = new judge.Watcher(element);
|
190
|
-
results.push(j.validate(callback));
|
191
|
-
});
|
192
|
-
return results;
|
193
|
-
};
|
194
|
-
|
195
|
-
// The judge store is now open :)
|
196
|
-
judge.store = (function() {
|
197
|
-
var store = {};
|
198
|
-
|
199
|
-
return {
|
200
|
-
|
201
|
-
// Stores watcher(s) for element(s) against a user defined key.
|
202
|
-
save: function(key, element) {
|
203
|
-
var elements = isCollection(element) ? element : [element];
|
204
|
-
_(elements).each(function(element) {
|
205
|
-
if (!_(store).has(key)) { store[key] = []; }
|
206
|
-
var watchedInstance = new judge.Watcher(element),
|
207
|
-
currentStored = _(store[key]).pluck('element');
|
208
|
-
if (!_(currentStored).include(element)) {
|
209
|
-
store[key].push(watchedInstance);
|
210
|
-
}
|
211
|
-
});
|
212
|
-
return store;
|
213
|
-
},
|
214
|
-
|
215
|
-
// Removes stored watcher(s).
|
216
|
-
remove: function(key, element) {
|
217
|
-
if (!_(store).has(key)) { return null; }
|
218
|
-
var elements = isCollection(element) ? element : [element];
|
219
|
-
store[key] = _(store[key]).reject(function(j) { return _(elements).include(j.element); });
|
220
|
-
if (store[key].length === 0) {
|
221
|
-
delete store[key];
|
222
|
-
}
|
223
|
-
return store[key];
|
224
|
-
},
|
225
|
-
|
226
|
-
// Returns the entire store object, or an array of watchers stored against
|
227
|
-
// the given key.
|
228
|
-
get: function(key) {
|
229
|
-
if (_(key).isUndefined()) { return store; }
|
230
|
-
return _(store).has(key) ? store[key] : null;
|
231
|
-
},
|
232
|
-
|
233
|
-
// Returns the entire store object with watchers converted to elements,
|
234
|
-
// or all DOM elements stored within all watchers stored against the given key.
|
235
|
-
getDOM: function(key) {
|
236
|
-
if (_(key).isUndefined()) {
|
237
|
-
var convertedStore = {};
|
238
|
-
_(store).each(function(array, key) {
|
239
|
-
convertedStore[key] = _(array).pluck('element');
|
240
|
-
});
|
241
|
-
return convertedStore;
|
242
|
-
}
|
243
|
-
return _(store).has(key) ? _(store[key]).pluck('element') : null;
|
244
|
-
},
|
245
|
-
|
246
|
-
// Shortcut for `judge.validate(judge.store.getDOM(key));`.
|
247
|
-
// Returns null if no stored elements are found for the given key.
|
248
|
-
validate: function(key, callback) {
|
249
|
-
var elements = judge.store.getDOM(key);
|
250
|
-
return (key && !_(elements).isNull()) ? judge.validate(elements, callback) : null;
|
251
|
-
},
|
252
|
-
|
253
|
-
// Wipes the entire store object, or wipes all watchers stored against
|
254
|
-
// the given key.
|
255
|
-
clear: function(key) {
|
256
|
-
if (_(key).isUndefined()) {
|
257
|
-
store = {};
|
258
|
-
} else {
|
259
|
-
if (!_(store).has(key)) { return null; }
|
260
|
-
delete store[key];
|
261
|
-
}
|
262
|
-
return store;
|
263
|
-
}
|
264
|
-
|
265
|
-
};
|
266
|
-
})();
|
267
|
-
|
268
|
-
// Trying to be a bit more descriptive than the basic error types allow.
|
269
|
-
var DependencyError = function(message) {
|
270
|
-
this.name = 'DependencyError';
|
271
|
-
this.message = message;
|
272
|
-
};
|
273
|
-
DependencyError.prototype = new Error();
|
274
|
-
DependencyError.prototype.constructor = DependencyError;
|
275
|
-
|
276
|
-
// A way of checking isArray, but including weird object types that are returned from collection queries.
|
277
|
-
var isCollection = function(object) {
|
278
|
-
var type = objectString(object),
|
279
|
-
types = [
|
280
|
-
'Array',
|
281
|
-
'NodeList',
|
282
|
-
'StaticNodeList',
|
283
|
-
'HTMLCollection',
|
284
|
-
'HTMLFormElement',
|
285
|
-
'HTMLAllCollection'
|
286
|
-
];
|
287
|
-
return _(types).include(type);
|
288
|
-
};
|
289
|
-
|
290
|
-
// Returns the object type as represented in `Object.prototype.toString`.
|
291
|
-
var objectString = function(object) {
|
292
|
-
var string = Object.prototype.toString.call(object);
|
293
|
-
return string.replace(/\[|\]/g, '').split(' ')[1];
|
294
|
-
};
|
295
|
-
|
296
|
-
// OMG! How can you use eval, you monster! It's totally evil!
|
297
|
-
// (It's used here for stuff like `(3, '<', 4) => '3 < 4' => true`.)
|
298
|
-
var operate = function(input, operator, validInput) {
|
299
|
-
return eval(input+' '+operator+' '+validInput);
|
300
|
-
};
|
301
|
-
|
302
|
-
// Some nifty numerical helpers.
|
303
|
-
var
|
304
|
-
isInt = function(value) { return value === +value && value === (value|0); },
|
305
|
-
isFloat = function(value) { return value === +value && value !== (value|0); },
|
306
|
-
isEven = function(value) { return (value % 2 === 0) ? true : false; },
|
307
|
-
isOdd = function(value) { return !isEven(value); };
|
308
|
-
|
309
|
-
// Converts a Ruby regular expression, given as a string, into JavaScript.
|
310
|
-
// This is rudimentary at best, as there are many, many differences between Ruby
|
311
|
-
// and JavaScript when it comes to regexp-fu. Basically, if you validate your records
|
312
|
-
// using complex regular expressions AND you hope that this will "just work"
|
313
|
-
// on the client-side too… you are going to be pretty disappointed.
|
314
|
-
// If you know of a better way (or an existing library) to convert regular expressions
|
315
|
-
// from Ruby to JavaScript, I would really love to hear from you.
|
316
|
-
var convertRegExp = function(string) {
|
317
|
-
var parts = string.slice(1, -1).split(':'),
|
318
|
-
flags = parts.shift().replace('?', ''),
|
319
|
-
source = parts.join(':').replace(/\\\\/g, '\\');
|
320
|
-
return new RegExp(source, convertFlags(flags));
|
321
|
-
};
|
322
|
-
var convertFlags = function(string) {
|
323
|
-
var on = string.split('-')[0];
|
324
|
-
return (/m/.test(on)) ? 'm' : '';
|
325
|
-
};
|
326
|
-
|
327
|
-
// Make judge object available.
|
328
|
-
root.judge = judge;
|
329
|
-
|
330
|
-
})(window);
|