Closed IlyaSemenov closed 1 month ago
We could introduce an abortPipeEarly
method that overwrites this config for all nested schemas:
export function naturalNumber(message = "Natural value expected.") {
return v.abortPipeEarly(v.pipe(v.number(message), v.integer(message), v.minValue(1, message)));
}
An alternative could be a general config
method that merges the new config into the previously applied config for all nested schemas:
export function naturalNumber(message = "Natural value expected.") {
return v.config(v.pipe(v.number(message), v.integer(message), v.minValue(1, message)), { abortPipeEarly: true });
}
What do you think is best? Until this functionality is part of the library, you can write this method yourself:
export function abortPipeEarly<TSchema extends v.GenericSchema>(
schema: TSchema
): TSchema {
return {
...schema,
_run(dataset, config) {
return schema._run(dataset, { ...config, abortPipeEarly: true });
},
};
}
export function config<TSchema extends v.GenericSchema>(
schema: TSchema,
config: Omit<v.Config<v.InferIssue<TSchema>>, 'skipPipe'>
): TSchema {
return {
...schema,
_run(dataset, config_) {
return schema._run(dataset, { ...config_, ...config });
},
};
}
Thanks for confirming that the use case is valid.
I suppose between the two options you suggest, v.config
is better as being more flexible (and presumably abortPipeEarly
can be then built on top of it).
v0.31.0-rc.10 with config
method is available
As a developer, I would like to create reusable schemas, presumably with the new pipe function, that only emit a single issue to the parse result.
delivers:
I'd somehow like baz to have only 1 issue. Note that
parse(..., { abortPipeEarly: true })
is not really an option, as it's global to the whole parse and also it's controlled by the user (and not by the schema author).Of course I could always fallback to
custom()
but thenpipe()
kinda loses its value.Some ideas: