Skip to content

Commit 3de6e9c

Browse files
committed
Upgrading to can 3.0.
Adding equality operator fix from #36.
1 parent 6efc35b commit 3de6e9c

File tree

13 files changed

+112
-87
lines changed

13 files changed

+112
-87
lines changed

can-validate/can-validate.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@
3434
*
3535
*/
3636

37-
import can from 'can';
37+
import ns from 'can-util/namespace';
38+
import Construct from 'can-construct';
39+
import canDev from 'can-util/js/dev/';
3840

3941
// add methods to can
40-
var Validate = can.Construct.extend({
42+
var Validate = Construct.extend({
4143

4244
/*
4345
* @description The current validator ID to use when can.validate methods are called.
@@ -96,7 +98,7 @@ var Validate = can.Construct.extend({
9698
isValid: function () {
9799
//!steal-remove-start
98100
if (!this._validatorId) {
99-
can.dev.warn('A validator library is required for can.validate to work properly.');
101+
canDev.warn('A validator library is required for can.validate to work properly.');
100102
}
101103
//!steal-remove-end
102104
return this.validator().isValid.apply(this, arguments);
@@ -118,7 +120,7 @@ var Validate = can.Construct.extend({
118120
once: function () {
119121
//!steal-remove-start
120122
if (!this._validatorId) {
121-
can.dev.warn('A validator library is required for can.validate to work properly.');
123+
canDev.warn('A validator library is required for can.validate to work properly.');
122124
}
123125
//!steal-remove-end
124126
return this.validator().once.apply(this, arguments);
@@ -139,16 +141,16 @@ var Validate = can.Construct.extend({
139141
var validateArgs = arguments;
140142
//!steal-remove-start
141143
if (!this._validatorId) {
142-
can.dev.warn('A validator library is required for can.validate to work properly.');
144+
canDev.warn('A validator library is required for can.validate to work properly.');
143145
}
144146
if (typeof arguments[0] !== 'object') {
145-
can.dev.warn('Attempting to pass single value to validate, use can.validator.once instead.');
147+
canDev.warn('Attempting to pass single value to validate, use can.validator.once instead.');
146148
}
147149
//!steal-remove-end
148150
return this.validator().validate.apply(this, validateArgs);
149151
}
150152
}, {});
151153

152-
can.validate = Validate;
154+
ns.validate = Validate;
153155

154-
export default can;
156+
export default Validate;

can-validate/demo.html

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</head>
1414
<body>
1515
<div id="app" class="container">
16-
<script src="../../node_modules/steal/steal.js" main="can/view/autorender/"></script>
16+
<script src="../../node_modules/steal/steal.js" main="can-view-autorender"></script>
1717
<script type='text/stache' id="demo" can-autorender>
1818
<h1>can.validate Demo</h1>
1919

@@ -31,16 +31,16 @@ <h3 class="panel-title">Validation Test</h3>
3131
<form>
3232
<label for="test1">MyVal <span class="text-danger"></span></label>
3333
<div class="input-group" {{data 'field' 'myVal'}}>
34-
<input class="form-control" id="test1" type="text" can-value="myVal">
34+
<input class="form-control" id="test1" type="text" {($value)}="myVal">
3535
<span class="input-group-btn">
36-
<button type="button" class="btn btn-default" can-click="validateField">Validate</button>
36+
<button type="button" class="btn btn-default" ($click)="validateField">Validate</button>
3737
</span>
3838
</div>
3939
<label for="test2">MyNum <span class="text-danger"></span></label>
4040
<div class="input-group" {{data 'field' 'myNum'}}>
41-
<input class="form-control" id="test2" type="text" can-value="myNum">
41+
<input class="form-control" id="test2" type="text" {($value)}="myNum">
4242
<span class="input-group-btn">
43-
<button type="button" class="btn btn-default" can-click="validateField">Validate</button>
43+
<button type="button" class="btn btn-default" ($click)="validateField">Validate</button>
4444
</span>
4545
</div>
4646
</form>
@@ -50,7 +50,9 @@ <h3 class="panel-title">Validation Test</h3>
5050
</script>
5151
</div>
5252
<script type='text/javascript'>
53-
steal('can','can-validate/', 'can-validate/shims/validatejs.shim', function(can){
53+
steal('jquery/dist/jquery.js', 'can-validate/', 'can-view-model', 'can-util/dom/data/', 'can-validate/shims/validatejs.shim', 'can-stache-bindings', function($, canValidate, setViewModel, domData){
54+
window.getViewModel = setViewModel;
55+
canValidate = canValidate.default;
5456
var validations = {
5557
myVal: {
5658
required: true,
@@ -67,16 +69,17 @@ <h3 class="panel-title">Validation Test</h3>
6769
}
6870
};
6971

70-
can.$('#demo').viewModel({
72+
setViewModel(document.getElementById('demo'), {
7173
myVal: 'testing testing',
7274
myNum: 100,
7375
errors: [],
74-
validateField: function (ctx, $el) {
76+
validateField: function (ctx, el) {
77+
var $el = $(el);
7578
var $parent = $el.parents('.input-group'),
76-
$label = can.$('label[for=' + $parent.find('.form-control').attr('id') + ']'),
77-
val = $parent.data('field');
79+
$label = $('label[for=' + $parent.find('.form-control').attr('id') + ']'),
80+
val = domData.get.call($parent[0], 'field');
7881

79-
var errors = can.validate.once(this.attr(val), validations[val], 'myObjIsAwesome');
82+
var errors = canValidate.once(this.attr(val), validations[val], 'myObjIsAwesome');
8083
if (errors) {
8184
$parent.addClass('has-error');
8285
$label.find('.text-danger').text(errors[0]);

can-validate/map/validate/demo.html

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
</head>
1414
<body>
1515
<div id="app" class="container">
16-
<script src="../../../node_modules/steal/steal.js" main="can/view/autorender/"></script>
16+
<script src="../../../node_modules/steal/steal.js" main="can-view-autorender/"></script>
1717
<script type='text/stache' id="demo" can-autorender>
1818
<h1>can.Map Validate Plugin Demo</h1>
1919

@@ -32,19 +32,19 @@ <h3 class="panel-title">Validation Test</h3>
3232
In the following examples, this happens when the field&rsquo;s value changes.</p>
3333
<div class="form-group">
3434
<label for="test1">MyVal required?</label>
35-
<input class="form-control" id="test" type="checkbox" can-value="myMap.isRequired">
35+
<input class="form-control" id="test" type="checkbox" {($value)}="myMap.isRequired">
3636
</div>
3737
<div class="form-group">
3838
<label for="test1">MyVal</label>
39-
<input class="form-control" id="test1" type="text" can-value="myMap.myVal">
39+
<input class="form-control" id="test1" type="text" {($value)}="myMap.myVal">
4040
</div>
4141
<div class="form-group">
4242
<label for="test2">MyNum</label>
43-
<input class="form-control" id="test2" type="text" can-value="myMap.myNum">
43+
<input class="form-control" id="test2" type="text" {($value)}="myMap.myNum">
4444
</div>
4545
<div class="input-group">
4646
<p>You can also validate all properties at once</p>
47-
<button type="button" class="btn btn-primary" can-click="doValidate">Validate All</button>
47+
<button type="button" class="btn btn-primary" ($click)="doValidate">Validate All</button>
4848
</div>
4949
</div>
5050
<ul class="list-group">
@@ -62,10 +62,9 @@ <h3 class="panel-title">Validation Test</h3>
6262
</script>
6363
</div>
6464
<script type='text/javascript'>
65-
steal.import('can', 'can-validate', 'can/map/define/', 'can-validate/map/validate/', 'can-validate/shims/validatejs.shim')
66-
.then(function(modules){
67-
var can = modules[0];
68-
var TestMap = can.Map.extend({
65+
steal('can-map', 'can-validate', 'can-view-model', 'can-map-define', 'can-validate/map/validate/', 'can-validate/shims/validatejs.shim', 'can-stache-bindings',
66+
function(canMap, canValidate, setViewModel){
67+
var TestMap = canMap.extend({
6968
define: {
7069
myNum: {
7170
value: 'heyo',
@@ -93,7 +92,7 @@ <h3 class="panel-title">Validation Test</h3>
9392
}
9493
});
9594

96-
can.$('#demo').viewModel({
95+
setViewModel(document.getElementById('demo'), {
9796
myMap: new TestMap({isRequired: true}),
9897
myFailMap: new TestMap({testString: 'hello'}),
9998
doValidate: function () {

can-validate/map/validate/validate.js

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,14 @@
5454
*
5555
*/
5656

57-
import can from 'can';
58-
import 'can-validate/can-validate';
59-
60-
var proto = can.Map.prototype;
57+
import canMap from 'can-map';
58+
import canCompute from 'can-compute';
59+
import canValidate from 'can-validate/can-validate';
60+
import canEach from 'can-util/js/each/';
61+
import isEmptyObject from 'can-util/js/is-empty-object/';
62+
import deepAssign from 'can-util/js/deep-assign/';
63+
64+
var proto = canMap.prototype;
6165
var oldSet = proto.__set;
6266
var ErrorsObj;
6367
var defaultValidationOpts;
@@ -70,7 +74,7 @@ var resolveComputes = function (itemObj, opts) {
7074
var processedObj = {};
7175

7276
// Loop through each validation option
73-
can.each(opts, function (item, key) {
77+
canEach(opts, function (item, key) {
7478
var actualOpts = item;
7579
if (typeof item === 'function') {
7680
// create compute and add it to computes array
@@ -102,9 +106,9 @@ var getPropDefineBehavior = function (behavior, attr, define) {
102106
};
103107

104108
// Default Map for errors object. Useful to add instance helpers
105-
ErrorsObj = can.Map.extend({}, {
109+
ErrorsObj = canMap.extend({}, {
106110
hasErrors: function () {
107-
return !can.isEmptyObject(this.attr());
111+
return !isEmptyObject(this.attr());
108112
}
109113
});
110114

@@ -134,12 +138,12 @@ var initProperty = function (key, value) {
134138
mapValidateCache = getValidateFromCache.call(this);
135139

136140
// If validate options don't exist in cache for current prop, create them
137-
if (mapValidateCache[key] && !can.isEmptyObject(mapValidateCache[key])) {
141+
if (mapValidateCache[key] && !isEmptyObject(mapValidateCache[key])) {
138142
validateOpts = mapValidateCache[key];
139143
propIniting = false;
140144
} else {
141145
// Copy current prop's validation properties to cache
142-
validateOpts = can.extend({}, getPropDefineBehavior('validate', key, this.define));
146+
validateOpts = deepAssign({}, getPropDefineBehavior('validate', key, this.define));
143147
// Need to build computes in the next step
144148
propIniting = true;
145149
}
@@ -148,7 +152,7 @@ var initProperty = function (key, value) {
148152
if (typeof validateOpts !== 'undefined') {
149153
//create validation computes only when initing the map
150154
if (propIniting) {
151-
validateOpts = can.extend({},
155+
validateOpts = deepAssign({},
152156
defaultValidationOpts,
153157
validateOpts,
154158
// Find any functions, converts them to computes and returns
@@ -177,11 +181,11 @@ proto.init = function () {
177181
oldInit.apply(this, arguments);
178182
}
179183
};
180-
can.extend(can.Map.prototype, {
184+
deepAssign(canMap.prototype, {
181185
_initValidation: function () {
182186
var self = this;
183187
var validateCache = getValidateFromCache.call(this);
184-
can.each(this.define, function (props, key) {
188+
canEach(this.define, function (props, key) {
185189
if (props.validate && !validateCache[key]) {
186190
initProperty.call(self, key, self[key]);
187191
}
@@ -211,18 +215,18 @@ can.extend(can.Map.prototype, {
211215
var self = this;
212216

213217
// Loop through validate options
214-
can.each(this.define, function (value, key) {
218+
canEach(this.define, function (value, key) {
215219
if (value.validate) {
216220
processedOpts[key] = resolveComputes({key: key, value: self.attr(key)}, validateOpts[key]);
217221
}
218222
});
219-
var errors = can.validate.validate(this.serialize(), processedOpts);
223+
var errors = canValidate.validate(this.serialize(), processedOpts);
220224

221225
// Process errors if we got them
222226
// TODO: This creates a new instance every time.
223227
this.attr('errors', new ErrorsObj(errors));
224228

225-
return can.isEmptyObject(errors);
229+
return isEmptyObject(errors);
226230
},
227231
/**
228232
* @function _validateOne Validate One
@@ -235,16 +239,17 @@ can.extend(can.Map.prototype, {
235239
*
236240
* @param {object} item A key/value object
237241
* @param {object} opts Object that contains validation config.
242+
+ @param {object} otherItems Object that contains other attributes in the map
238243
* @return {boolean} True if method found that the property can be saved; if
239244
* validation fails and the property must validate (`mustValidate` property),
240245
* this will be `false`.
241246
*/
242-
_validateOne: function (item, opts) {
247+
_validateOne: function (item, opts, otherItems) {
243248
var errors;
244249
var allowSet = true;
245250

246251
// run validation
247-
errors = can.validate.once(item.value, can.extend({}, opts), item.key);
252+
errors = canValidate.once(item.value, deepAssign({}, opts), item.key, otherItems);
248253

249254
// Process errors if we got them
250255
if (errors && errors.length > 0) {
@@ -287,11 +292,11 @@ can.extend(can.Map.prototype, {
287292
var self = this;
288293

289294
// Loop through each validation option
290-
can.each(opts, function (item, key) {
295+
canEach(opts, function (item, key) {
291296
processedObj[key] = item;
292297
if (typeof item === 'function') {
293298
// create compute and add it to computes array
294-
var compute = can.compute(can.proxy(item, self));
299+
var compute = canCompute(Function.prototype.bind.call(item, self));
295300
computes.push({key: key, compute: compute});
296301
processedObj[key] = compute;
297302
}
@@ -300,10 +305,10 @@ can.extend(can.Map.prototype, {
300305
// Using the computes array, create necessary listeners
301306
// We do this afterwards instead of inline so we can have access
302307
// to the final set of validation options.
303-
can.each(computes, function (item) {
308+
canEach(computes, function (item) {
304309
item.compute.bind('change', function () {
305310
itemObj.value = self.attr(itemObj.key);
306-
self._validateOne(itemObj, processedObj);
311+
self._validateOne(itemObj, processedObj, self.attr());
307312
});
308313
});
309314

@@ -325,7 +330,7 @@ proto.__set = function (prop, value, current, success, error) {
325330
// If validate opts are set and initing, validate properties only if validateOnInit is true
326331
if ((validateOpts && !mapIniting) || (validateOpts && mapIniting && validateOpts.validateOnInit)) {
327332
// Validate item
328-
allowSet = this._validateOne({key: prop, value: value}, validateOpts);
333+
allowSet = this._validateOne({key: prop, value: value}, validateOpts, this.attr());
329334
}
330335
}
331336

can-validate/map/validate/validate.test.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
/* jshint asi: false */
2-
import can from 'can';
3-
import 'can/map/define/';
2+
import canMap from 'can-map';
3+
import 'can-map-define';
44
import 'can-validate';
55
import 'can-validate/map/validate/';
66
import 'can-validate/shims/validatejs.shim';
7+
import isEmptyObject from 'can-util/js/is-empty-object/';
78
import 'chai';
89
import 'steal-mocha';
910
var expect = chai.expect;
1011
var validatedMap;
1112
var secondaryMap;
1213

13-
var ValidatedMap = can.Map.extend({
14+
var ValidatedMap = canMap.extend({
1415
define: {
1516
myNumber: {
1617
value: 100,
@@ -40,7 +41,7 @@ describe('Validate can.Map define plugin', function () {
4041
validatedMap = new ValidatedMap();
4142
});
4243
it('does not validate', function () {
43-
expect(can.isEmptyObject(validatedMap.errors)).to.equal(true);
44+
expect(isEmptyObject(validatedMap.errors)).to.equal(true);
4445
});
4546
});
4647
});
@@ -95,7 +96,7 @@ describe('Validate can.Map define plugin', function () {
9596
});
9697
it('control map validates successfully', function () {
9798
secondaryMap.attr('computedProp', '');
98-
expect(can.isEmptyObject(secondaryMap.attr('errors'))).to.equal(true);
99+
expect(isEmptyObject(secondaryMap.attr('errors'))).to.equal(true);
99100
});
100101
it('other map validates, sets error', function () {
101102
validatedMap.attr('computedProp', '');

0 commit comments

Comments
 (0)