Skip to content

Commit 7f5355d

Browse files
committed
The global options object is now passed to custom validators
The is to help the issue raised in ansman#63
1 parent 4dd3b6d commit 7f5355d

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

index.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,7 @@
401401
<li>
402402
<a href="#changelog">Changelog</a>
403403
<ul>
404+
<li><a href="#changelog-wip">WIP</a></li>
404405
<li><a href="#changelog-0-8-0">0.8.0</a></li>
405406
<li><a href="#changelog-0-7-1">0.7.1</a></li>
406407
<li><a href="#changelog-0-7-0">0.7.0</a></li>
@@ -972,6 +973,10 @@ <h2>Writing your own validator</h2>
972973
<li>
973974
<b>attributes</b> - The entire attributes object.
974975
</li>
976+
<li>
977+
<b>globalOptions</b> - The options passed when calling
978+
<code>validate</code> (will always be an object, non null).
979+
</li>
975980
</ol>
976981
<p class="description">
977982
If the validator passes simply return <code>null</code> or <code>undefined</code>.
@@ -2114,6 +2119,17 @@ <h2>Utilities</h2>
21142119
</div>
21152120
<div id="changelog">
21162121
<h2>Changelog</h2>
2122+
<div id="changelog-wip">
2123+
<h3>
2124+
<b class="version">WIP</b>
2125+
</h3>
2126+
<ul>
2127+
<li>
2128+
The global validation options are now passed to custom
2129+
validators.
2130+
</li>
2131+
</ul>
2132+
</div>
21172133
<div id="changelog-0-8-0">
21182134
<h3>
21192135
<b class="version">0.8.0</b>

specs/validate-spec.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,17 @@ describe("validate", function() {
100100
expect(pass).toHaveBeenCalledWithContext(pass);
101101
});
102102

103-
it("calls the validator with the val, opts, key and attributes", function() {
103+
it("calls the validator with the val, opts, key, attributes and global options", function() {
104104
var options = {someOption: true}
105105
, attributes = {someAttribute: 'some value'}
106-
, constraints = {someAttribute: {pass: options}};
107-
validate.runValidations(attributes, constraints, {});
106+
, constraints = {someAttribute: {pass: options}}
107+
, globalOptions = {someOption: 'some value'};
108+
validate.runValidations(attributes, constraints, globalOptions);
108109
expect(pass).toHaveBeenCalledWith('some value',
109110
options,
110111
'someAttribute',
111-
attributes);
112+
attributes,
113+
globalOptions);
112114
});
113115

114116
it("returns an array of results", function() {
@@ -179,7 +181,7 @@ describe("validate", function() {
179181
, globalOptions = {foo: "bar"};
180182
validate.runValidations(attrs, constraints, globalOptions);
181183
expect(spy).toHaveBeenCalledWith("Nicklas", attrs, "name", globalOptions, constraints);
182-
expect(pass).toHaveBeenCalledWith("Nicklas", options.pass, "name", attrs);
184+
expect(pass).toHaveBeenCalledWith("Nicklas", options.pass, "name", attrs, globalOptions);
183185
});
184186

185187
it("allows the options for a validator to be a function", function() {
@@ -190,7 +192,7 @@ describe("validate", function() {
190192
, globalOptions = {foo: "bar"};
191193
validate.runValidations(attrs, constraints, globalOptions);
192194
expect(spy).toHaveBeenCalledWith("Nicklas", attrs, "name", globalOptions, constraints);
193-
expect(pass).toHaveBeenCalledWith("Nicklas", options, "name", attrs);
195+
expect(pass).toHaveBeenCalledWith("Nicklas", options, "name", attrs, globalOptions);
194196
});
195197

196198
it("doesnt run the validations if the options are falsy", function() {
@@ -213,7 +215,8 @@ describe("validate", function() {
213215
"bar",
214216
true,
215217
"foo",
216-
{foo: "bar"}
218+
{foo: "bar"},
219+
{}
217220
);
218221

219222
validate($form, constraints);
@@ -222,7 +225,8 @@ describe("validate", function() {
222225
"bar",
223226
true,
224227
"foo",
225-
{foo: "bar"}
228+
{foo: "bar"},
229+
{}
226230
);
227231
});
228232
});

validate.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,12 @@
131131
value: value,
132132
validator: validatorName,
133133
options: validatorOptions,
134-
error: validator.call(validator, value, validatorOptions, attr,
135-
attributes)
134+
error: validator.call(validator,
135+
value,
136+
validatorOptions,
137+
attr,
138+
attributes,
139+
options)
136140
});
137141
}
138142
}

0 commit comments

Comments
 (0)