vue-types: shape.loose does not work
Hi again,
I noticed that shape({}).loose does not work. I did some digging and noticed that the unit test is actually incorrect.
I adapted the test to be breaking:
it('should validate an object with keys NOT present in the shape on `loose` mode', () => {
const customType = VueTypes.shape(shape).loose
expect(customType.validator({
id: 10,
name: 'John',
newProperty: true
})).toBe(true)
})
Test result:
WARN: '[VueTypes warn]: object is missing "newProperty" property'
[...]
SUMMARY:
✔ 76 tests completed
✖ 1 test failed
FAILED TESTS:
VuePropTypes
`.shape`
✖ should validate an object with keys NOT present in the shape on `loose` mode
PhantomJS 2.1.1 (Mac OS X 0.0.0)
Expected false to be true
assert@test/index.spec.js:734:27
toBe@test/index.spec.js:2615:28
test/index.spec.js:5672:15
The problem is that you use objectAssign({ isLoose: true }, this) (see here).
You create a new object, instead of appending data to this, you create a new object. This object is however not the one which is bound to the .shape() function.
If loose would set this.isLoose=true, it is fixed, but I think the new object is actually intended behavior (that’s why this is not a PR).
Actually the fix from https://github.com/dwightjack/vue-types/issues/5 is the problem, because this binds the context of the validator function.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 15 (15 by maintainers)
Commits related to this issue
- fix #5 and #6 by forcing the correct context for the shape validation function — committed to EyMaddis/vue-types by EyMaddis 7 years ago
Btw, I’m really appreciating the time you’re spending helping on the library. Right now I’m not working on vue.js projects so feedback like yours are very welcome!