Closed smujaddid closed 1 month ago
You can use .input
instead of .received
for pipelines without transformations. I think the same only works for transformations when writing a custom schema: https://valibot.dev/api/custom/
import * as v from 'valibot';
const StringSchema = v.config(
v.pipe(v.string(), v.maxLength(1)),
{ message: (issue) => `${v._stringify(issue.input)} length is > 1` } // or just `${issue.input}...`
);
const IntegerSchema = v.custom(
(input) => Number.isInteger(Number(input)),
(issue) => `${issue.received} is not a valid integer`
);
@fabian-hiller thanks! That's what I'm looking for.
I suggest to include this information on config
and custom
api documentation.
BTW, why the need to call v._stringify
?
When I tried to do unit testing for above scenario, I have to wrap the input value with double quote in the toThrowWithMessage
call just to pass assertion. I was expecting an unquoted string in that case.
But in some scenarios, the string is not double quoted in default generated message.
@fabian-hiller While the .input
solution works well for stringSchema
in the example above, it doesn't work for integerSchema
case because of transform
action.
The transform(Number)
action changed both .input
and .received
value received by number()
to transformed value which is NaN
in this case, where I expected it to be same as original input 27i9
.
This is exactly what I have described here:
You can use
.input
instead of.received
for pipelines without transformations. I think the same only works for transformations when writing a custom schema: https://valibot.dev/api/custom/
This is implemented this way by design. I do not expect to change it at this time.
.expected
and .received
is a language-neutral string that describes the data property that was expected/received. It follows a specific syntax. For example, strings are enclosed in double quotes. You can use _stringify
to convert an input to this format.
I suggest to include this information on
config
andcustom
api documentation.
Feel free to create a PR.
I understand. As for now, I refactor my schema to suit my needs. Thanks for your help.
Suppose I have 2 schemas for example (both not related)
in my unit tests I have these:
which results:
as you can see, the error message generated with
config
method, did used the current input for that action. For example, the4 length is > 1
has theissue.received
value from last action (maxLength
) which is4
that is the length of original input. Same goes forNaN is not valid
, the result oftransform(Number)
get passed intonumber()
. Both cases actually correct. But the problem is: How can I make theconfig
use original input to generate same error message for the entire pipeline?I'm expecting I can use the
config
to generate a message like this:I already read through documentation but can't find anything to solve this.