-
Notifications
You must be signed in to change notification settings - Fork 2
Description
Feature description:
When trying to validate a value, validation should not fail if the value is not yet a message, but instead create the message and then validate it.
Problem it solves or use case:
Some libraries such as Tanstack Form (and hopefully, superform too) offer native support for Standard Schema validation, but this does not integrate well with the current logic of protovalidate-es because validation first requires creating a message with create().
Proposed implementation or solution:
In the call to validateUnsafe, do not reject validation outright if the value is not yet a message, but rather, if it isn't one, create a new message with the given descriptor
function validateUnsafe(registry, celMan, planner, schema, message, failFast) {
const messageTypeName = message.$typeName;
// Replace this part here...
if (!isMessage(message, schema)) {
throw new RuntimeError(`Cannot validate message ${messageTypeName} with schema ${schema.typeName}`);
}Contribution:
I am willing to implement it myself, if there are no reasons for which it cannot be implemented. I am ok with others implementing it too.
Examples or references:
Here is a stackblitz repo with the example I was working on.
As you can see, the validators such as onSubmit, onChange and so on must be a pure implementation of the Standard Schema, so if you swap completeValidator with greetValidator it won't work. Whereas if you include the message creation logic in the validator as I did for completeValidator, you can just seamlessly use that validator to validate form data on the client side, which is a major benefit of using this library.
The simple wrapper solution I implemented is also an alternative, but unless there are reasons to not do so, I think it would be a good idea to include that logic in the validator itself so that it can be used easily with form libraries.