vine icon indicating copy to clipboard operation
vine copied to clipboard

Bug/FR: vague error when using vine.object()

Open MoltenCoffee opened this issue 9 months ago • 3 comments

Package version

1.6.0

Describe the bug

This is submitted as a bug, but might as well be a documentation issue or feature request, depending how one looks at it.

My issue lies with vine.object, primarily the behaviour when one uses it as follows:

vine.object()
// instead of:
vine.object({})

This will generate the following error: TypeError: Cannot convert undefined or null to object, with a stacktrace that does not really help. The problem comes down to vine.object expecting an object as parameter. Developers using plain JS will have a hard time figuring this out, as the documentation isn't that clear on this either, note the word may:

Ensure the value of a field is a valid JavaScript object literal.
You __may__ define a collection of properties you want to validate within the object.

What should it do?

I believe there are several routes that could be picked, that are not mutually exclusive:

  • Implement a runtime check if an object is provided, and throw a clearer error if there is not
  • Give vine.object a default parameter ({})
  • Update the docs, stating that one must provide an object
  • Or even, allow vine.object() to work, just validating that something is an object (with/without allowUnknownProperties)

Reproduction repo

No response

MoltenCoffee avatar Sep 15 '23 14:09 MoltenCoffee