Skip to content

shape.loose does not work #6

@EyMaddis

Description

@EyMaddis

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 #5 is the problem, because this binds the context of the validator function.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions