Skip to content

Commit dd848b8

Browse files
author
Marco Solazzi
committed
Merge branch 'release/0.6.5'
2 parents 5e46798 + c959d07 commit dd848b8

File tree

9 files changed

+114
-84
lines changed

9 files changed

+114
-84
lines changed

dist/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ var VuePropTypes = {
229229

230230
return valueKeys.every(function (key) {
231231
if (keys.indexOf(key) === -1) {
232-
if (_this.isLoose === true) return true;
232+
if (_this._vueTypes_isLoose === true) return true;
233233
(0, _utils.warn)('object is missing "' + key + '" property');
234234
return false;
235235
}
@@ -238,11 +238,17 @@ var VuePropTypes = {
238238
});
239239
}
240240
});
241+
type.validator = type.validator.bind(type);
242+
243+
Object.defineProperty(type, '_vueTypes_isLoose', {
244+
enumerable: false,
245+
writable: true,
246+
value: false
247+
});
241248

242249
Object.defineProperty(type, 'loose', {
243250
get: function get() {
244-
this.isLoose = true;
245-
this.validator = this.validator.bind(this);
251+
this._vueTypes_isLoose = true;
246252
return this;
247253
},
248254

dist/utils.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,12 @@ var validateType = exports.validateType = function validateType(type, value) {
192192
var warn = noop;
193193

194194
if (process.env.NODE_ENV !== 'production') {
195-
(function () {
196-
var hasConsole = typeof console !== 'undefined';
197-
exports.warn = warn = function warn(msg) {
198-
if (hasConsole) {
199-
console.warn('[VueTypes warn]: ' + msg);
200-
}
201-
};
202-
})();
195+
var hasConsole = typeof console !== 'undefined';
196+
exports.warn = warn = function warn(msg) {
197+
if (hasConsole) {
198+
console.warn('[VueTypes warn]: ' + msg);
199+
}
200+
};
203201
}
204202

205203
exports.warn = warn;

es/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ var VuePropTypes = {
220220

221221
return valueKeys.every(function (key) {
222222
if (keys.indexOf(key) === -1) {
223-
if (_this.isLoose === true) return true;
223+
if (_this._vueTypes_isLoose === true) return true;
224224
warn('object is missing "' + key + '" property');
225225
return false;
226226
}
@@ -229,11 +229,17 @@ var VuePropTypes = {
229229
});
230230
}
231231
});
232+
type.validator = type.validator.bind(type);
233+
234+
Object.defineProperty(type, '_vueTypes_isLoose', {
235+
enumerable: false,
236+
writable: true,
237+
value: false
238+
});
232239

233240
Object.defineProperty(type, 'loose', {
234241
get: function get() {
235-
this.isLoose = true;
236-
this.validator = this.validator.bind(this);
242+
this._vueTypes_isLoose = true;
237243
return this;
238244
},
239245

es/utils.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,14 +180,12 @@ export var validateType = function validateType(type, value) {
180180
var warn = noop;
181181

182182
if (process.env.NODE_ENV !== 'production') {
183-
(function () {
184-
var hasConsole = typeof console !== 'undefined';
185-
warn = function warn(msg) {
186-
if (hasConsole) {
187-
console.warn('[VueTypes warn]: ' + msg);
188-
}
189-
};
190-
})();
183+
var hasConsole = typeof console !== 'undefined';
184+
warn = function warn(msg) {
185+
if (hasConsole) {
186+
console.warn('[VueTypes warn]: ' + msg);
187+
}
188+
};
191189
}
192190

193191
export { warn };

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vue-types",
3-
"version": "0.6.4",
3+
"version": "0.6.5",
44
"description": "Prop types utility for Vue",
55
"author": "Marco Solazzi",
66
"license": "MIT",

src/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ const VuePropTypes = {
211211

212212
return valueKeys.every((key) => {
213213
if (keys.indexOf(key) === -1) {
214-
if (this.isLoose === true) return true
214+
if (this._vueTypes_isLoose === true) return true
215215
warn(`object is missing "${key}" property`)
216216
return false
217217
}
@@ -220,11 +220,17 @@ const VuePropTypes = {
220220
})
221221
}
222222
})
223+
type.validator = type.validator.bind(type)
224+
225+
Object.defineProperty(type, '_vueTypes_isLoose', {
226+
enumerable: false,
227+
writable: true,
228+
value: false
229+
})
223230

224231
Object.defineProperty(type, 'loose', {
225232
get() {
226-
this.isLoose = true
227-
this.validator = this.validator.bind(this)
233+
this._vueTypes_isLoose = true
228234
return this
229235
},
230236
enumerable: false

test/index.spec.js

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ const checkRequired = (type) => {
1212
})
1313
}
1414

15+
// Vue.js does keep the context for validators, so there is no `this`
16+
const forceNoContext = (validator) => validator.bind(undefined)
17+
1518
describe('VuePropTypes', () => {
1619

1720
describe('`.any`', () => {
@@ -194,9 +197,10 @@ describe('VuePropTypes', () => {
194197
})
195198

196199
it('should provide a validator function that returns true on integer values', () => {
197-
expect(VueTypes.integer.validator(100)).toBe(true)
198-
expect(VueTypes.integer.validator(Infinity)).toBe(false)
199-
expect(VueTypes.integer.validator(0.1)).toBe(false)
200+
const validator = forceNoContext(VueTypes.integer.validator)
201+
expect(validator(100)).toBe(true)
202+
expect(validator(Infinity)).toBe(false)
203+
expect(validator(0.1)).toBe(false)
200204
})
201205

202206
})
@@ -226,8 +230,9 @@ describe('VuePropTypes', () => {
226230
})
227231

228232
it('should provide a custom validator function', () => {
229-
expect(customType.validator('mytest')).toBe(true)
230-
expect(customType.validator(0)).toBe(false)
233+
const validator = forceNoContext(customType.validator)
234+
expect(validator('mytest')).toBe(true)
235+
expect(validator(0)).toBe(false)
231236
})
232237

233238
})
@@ -266,8 +271,9 @@ describe('VuePropTypes', () => {
266271
})
267272

268273
it('should provide a custom validator function', () => {
269-
expect(customType.validator(0)).toBe(true)
270-
expect(customType.validator(5)).toBe(false)
274+
const validator = forceNoContext(customType.validator)
275+
expect(validator(0)).toBe(true)
276+
expect(validator(5)).toBe(false)
271277
})
272278

273279
it('should filter `null` values type checking', () => {
@@ -343,24 +349,25 @@ describe('VuePropTypes', () => {
343349

344350
it('should validate an array of same-type values', () => {
345351
const customType = VueTypes.arrayOf(Number)
346-
expect(customType.validator([0, 1, 2])).toBe(true)
352+
expect(forceNoContext(customType.validator)([0, 1, 2])).toBe(true)
347353

348354
})
349355

350356
it('should NOT validate an array of mixed-type values', () => {
351357
const customType = VueTypes.arrayOf(Number)
352-
expect(customType.validator([0, 1, 'string'])).toBe(false)
358+
expect(forceNoContext(customType.validator)([0, 1, 'string'])).toBe(false)
353359
})
354360

355361
it('should allow validation of VuePropTypes native types', () => {
356362
const customType = VueTypes.arrayOf(VueTypes.number)
357-
expect(customType.validator([0, 1, 2])).toBe(true)
363+
expect(forceNoContext(customType.validator)([0, 1, 2])).toBe(true)
358364
})
359365

360366
it('should allow validation of VuePropTypes custom types', () => {
361367
const customType = VueTypes.arrayOf(VueTypes.integer)
362-
expect(customType.validator([0, 1, 2])).toBe(true)
363-
expect(customType.validator([0, 1.2, 2])).toBe(false)
368+
const validator = forceNoContext(customType.validator)
369+
expect(validator([0, 1, 2])).toBe(true)
370+
expect(validator([0, 1.2, 2])).toBe(false)
364371
})
365372

366373
})
@@ -391,24 +398,25 @@ describe('VuePropTypes', () => {
391398

392399
it('should validate an object of same-type values', () => {
393400
const customType = VueTypes.objectOf(Number)
394-
expect(customType.validator({ id: 10, age: 30 })).toBe(true)
401+
expect(forceNoContext(customType.validator)({ id: 10, age: 30 })).toBe(true)
395402

396403
})
397404

398405
it('should NOT validate an array of mixed-type values', () => {
399406
const customType = VueTypes.objectOf(Number)
400-
expect(customType.validator({ id: '10', age: 30 })).toBe(false)
407+
expect(forceNoContext(customType.validator)({ id: '10', age: 30 })).toBe(false)
401408
})
402409

403410
it('should allow validation of VuePropTypes native types', () => {
404411
const customType = VueTypes.objectOf(VueTypes.number)
405-
expect(customType.validator({ id: 10, age: 30 })).toBe(true)
412+
expect(forceNoContext(customType.validator)({ id: 10, age: 30 })).toBe(true)
406413
})
407414

408415
it('should allow validation of VuePropTypes custom types', () => {
409416
const customType = VueTypes.objectOf(VueTypes.integer)
410-
expect(customType.validator({ id: 10, age: 30 })).toBe(true)
411-
expect(customType.validator({ id: 10.2, age: 30 })).toBe(false)
417+
const validator = forceNoContext(customType.validator)
418+
expect(validator({ id: 10, age: 30 })).toBe(true)
419+
expect(validator({ id: 10.2, age: 30 })).toBe(false)
412420
})
413421

414422
})
@@ -433,7 +441,7 @@ describe('VuePropTypes', () => {
433441
it('should validate an object with a given shape', () => {
434442

435443
const customType = VueTypes.shape(shape)
436-
expect(customType.validator({
444+
expect(forceNoContext(customType.validator)({
437445
id: 10,
438446
name: 'John',
439447
age: 30
@@ -443,7 +451,7 @@ describe('VuePropTypes', () => {
443451
it('should NOT validate an object without a given shape', () => {
444452

445453
const customType = VueTypes.shape(shape)
446-
expect(customType.validator({
454+
expect(forceNoContext(customType.validator)({
447455
id: '10',
448456
name: 'John',
449457
age: 30
@@ -453,7 +461,7 @@ describe('VuePropTypes', () => {
453461
it('should NOT validate an object with keys NOT present in the shape', () => {
454462

455463
const customType = VueTypes.shape(shape)
456-
expect(customType.validator({
464+
expect(forceNoContext(customType.validator)({
457465
id: 10,
458466
name: 'John',
459467
age: 30,
@@ -464,7 +472,7 @@ describe('VuePropTypes', () => {
464472
it('should validate an object with keys NOT present in the shape on `loose` mode', () => {
465473

466474
const customType = VueTypes.shape(shape).loose
467-
expect(customType.validator({
475+
expect(forceNoContext(customType.validator)({
468476
id: 10,
469477
name: 'John',
470478
age: 30,
@@ -474,7 +482,8 @@ describe('VuePropTypes', () => {
474482

475483
it('should NOT validate a value which is NOT an object', () => {
476484
const customType = VueTypes.shape(shape)
477-
expect(customType.validator('a string')).toBe(false)
485+
const validator = forceNoContext(customType.validator)
486+
expect(validator('a string')).toBe(false)
478487

479488
class MyClass {
480489
constructor() {
@@ -484,7 +493,7 @@ describe('VuePropTypes', () => {
484493
}
485494
}
486495

487-
expect(customType.validator(new MyClass())).toBe(false)
496+
expect(validator(new MyClass())).toBe(false)
488497
})
489498

490499
it('should provide a method to set a custom default', () => {
@@ -515,12 +524,13 @@ describe('VuePropTypes', () => {
515524
id: VueTypes.integer.isRequired,
516525
name: String
517526
})
527+
const validator = forceNoContext(customType.validator)
518528

519-
expect(customType.validator({
529+
expect(validator({
520530
name: 'John'
521531
})).toBe(false)
522532

523-
expect(customType.validator({
533+
expect(validator({
524534
id: 10
525535
})).toBe(true)
526536
})
@@ -531,12 +541,13 @@ describe('VuePropTypes', () => {
531541
myKey: VueTypes.any.isRequired,
532542
name: null
533543
})
544+
const validator = forceNoContext(customType.validator)
534545

535-
expect(customType.validator({
546+
expect(validator({
536547
name: 'John'
537548
})).toBe(false)
538549

539-
expect(customType.validator({
550+
expect(validator({
540551
myKey: null
541552
})).toBe(true)
542553

@@ -602,14 +613,15 @@ describe('VuePropTypes', () => {
602613

603614
it('should validate custom types with complex shapes', () => {
604615
const customType = VueTypes.oneOfType(complexTypes)
616+
const validator = forceNoContext(customType.validator)
605617

606-
expect(customType.validator(1)).toBe(true)
618+
expect(validator(1)).toBe(true)
607619

608620
// validates types not values!
609-
expect(customType.validator(5)).toBe(true)
621+
expect(validator(5)).toBe(true)
610622

611-
expect(customType.validator({ id: 10 })).toBe(true)
612-
expect(customType.validator({ id: '10' })).toBe(false)
623+
expect(validator({ id: 10 })).toBe(true)
624+
expect(validator({ id: '10' })).toBe(false)
613625

614626
})
615627

0 commit comments

Comments
 (0)