How to validate an object with keys that follow specific pattern (regex)? #1094
Unanswered
akrynski-1
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Unfortunately, this harder then I wish because we do not support custom key schemas with import * as v from 'valibot';
const BaseSchema = v.object({
email: v.pipe(v.string(), v.email()),
password: v.pipe(v.string(), v.minLength(8)),
});
const ExtendedSchema = v.pipe(
v.objectWithRest(BaseSchema.entries, v.number()),
v.check((input) => {
for (const key in input) {
if (!(key in BaseSchema.entries) && !key.startsWith('field_')) {
return false;
}
}
return true;
}, 'Additional keys must be prefixed with "field_"'),
); |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have object with a shape similar to one below:
How can I validate it using valibot?
I have known and static fields:
foo
andbar
I have dynamic fields following specific key pattern:
field_*
, they all have the same type.There can be fields I don't care about at all, they won't be in the schema, I don't want them to result in failed validation, they should also work with
looseObject
because maybe they will be useful somewhere else.I tried playing with
record
andintersect
but this will fail because keys likeemail
andpassword
don't match the regex here:It feels like it should be possible without hacks.
Thank you in advance!
Beta Was this translation helpful? Give feedback.
All reactions