Bug/FR: vague error when using vine.object()
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.objecta 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/withoutallowUnknownProperties)
Reproduction repo
No response
Is there any use case for creating an object without any properties?
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.
I see, that makes sense. Will improve the error message and allow an object without any arguments