vine icon indicating copy to clipboard operation
vine copied to clipboard

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

Open MoltenCoffee opened this issue 2 years 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

Is there any use case for creating an object without any properties?

thetutlage avatar Nov 06 '23 04:11 thetutlage

Oftentimes, there won't be. For me, the issue arose when I did not want to validate the contents of an object exported from a WYSIWYG editor (like tiptap or editorjs). I wanted vine to validate that an object was present, but skip checking the exact contents of it.

This is perfectly possible at the moment, but only with vine.object({}).allowUnknownProperties(). To me this feels cumbersome and inconsistent with the rest of the API. However, my main problem is the vagueness of the error.

MoltenCoffee avatar Nov 10 '23 13:11 MoltenCoffee

I see, that makes sense. Will improve the error message and allow an object without any arguments

thetutlage avatar Nov 15 '23 02:11 thetutlage