-
-
Notifications
You must be signed in to change notification settings - Fork 204
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validation for multiple possible values or notValues #716
Comments
On a related note, This is more of a JS issue though, as |
Thanks for sharing that idea. Yes, we can add Can you share your entire !NaN schema? |
This likely occurred for me because I'm also allowing v.pipe(
// Input
v.union([
// Expected (intended "input" type)
v.number(),
// Coerced (supported alternative "input" types)
v.pipe(v.string(), v.trim(), v.transform(i => Number(i || NaN))),
]),
// Validation
// v.notValue(NaN, () => 'Not a Number'), // <--- couldn't use this
v.check(i => !Number.isNaN(i), () => 'Not a Number'),
v.integer(() => 'Not an Integer'),
v.minValue(1, () => 'Too Small'),
v.maxValue(65535, () => 'Too Large'),
); The tricky thing with The bigger picture is that I was trying to recreate a Zod usage we had, where we needed "optional numbers", and I recalled it being a bit of a pain... Essentially, a form input (string) where the value should be coerced to a number, is within a range, but can also be optional (with no fallback). Here is an example: import * as v from 'valibot';
/** Schemas */
const AsNumberSchema = v.union([
// Expected (intended "input" type)
v.number(),
// Coerced (supported alternative "input" types)
v.pipe(v.string(), v.trim(), v.transform(i => Number(i || NaN))),
]);
const AsUndefinedSchema = v.union([
// Optional (coercing null as undefined)
v.pipe(v.null(), v.transform(() => undefined)),
v.undefined(),
]);
const PortSchema = v.pipe(
// Supported Types
AsNumberSchema,
// Validation
v.check(i => !Number.isNaN(i), () => 'Not a Number'),
v.integer(() => 'Not an Integer'),
v.minValue(1, () => 'Too Small'),
v.maxValue(65535, () => 'Too Large'),
);
const ConfigurationSchema = v.object({
port: v.union([ PortSchema, AsUndefinedSchema ]),
});
/** Types */
type ConfigurationSchema = v.InferOutput<typeof ConfigurationSchema>;
/** Debug */
const result = v.safeParse(ConfigurationSchema, { port: undefined }, { abortPipeEarly: true });
console.log(result); There might be a better (more repeatable) approach to this, as I would also need to do similar string/undefined support for booleans (who don't have an "invalid" state to work with). So maybe using Alternatively, perhaps the better option is to just create a separate dedicated Form/UserInput version of the schemas (that always assumes a string as input) rather than one that caters for everything. I was just trying to avoid repetition, but potentially separating out these concerns makes more sense.
With the above in mind, the scenario I was curious about, was if I needed to exclude a set of reserved ports from those available. If it was always a non-contiguous list then something like |
Yes, let's keep this open. Maybe someone in the community will implement it, or I will do it at a later date. |
@fabian-hiller I'll work on adding |
I was looking through the validators to see if there was an easy way to blacklist or whitelist a set of strings/numbers (all in one go). I see that
.value
and.notValue
are there, but didn't see any ones that would allow for an array of items to check against.I'm aware this could be done with a custom
.check
or.regex
, but wondered if it was a common enough usecase where we might consider adding.values
and.notValues
?The text was updated successfully, but these errors were encountered: