-
-
Notifications
You must be signed in to change notification settings - Fork 247
feat: create recordWithPatterns schema #1165
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
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
3cd627f
to
5ad06de
Compare
Thank you for creating this PR! Do you know if Zod offers such a schema? How about calling it |
not to my knowledge, no - JSON schema has patternProperties which is similar
sure, makes sense
what would be the desired behaviour if a key doesn't match any of the patterns? |
Ah, the rest argument is necessary, because these two types behave differently: type Unmerged = {
[key: `foo(${string})`]: string;
[key: `bar(${string})`]: number;
} & {
[key: string]: boolean;
}
type Merged = {
[key: `foo(${string})`]: string;
[key: `bar(${string})`]: number;
[key: string]: boolean;
} in edit: hmm, the below works, so we could just avoid calling Prettify to merge the intersection: type Unmerged = {
[key: `foo(${string})`]: string;
} & {
[key: `bar(${string})`]: number;
} & {
[key: string]: boolean;
} it's kinda ugly, but 🤷🏻 |
that does still leave the question of what to do when a key doesn't match any of the patterns: const parsed = v.parse(
v.recordWithPatterns([
[v.pipe(v.string(), v.startsWith("foo-")), v.string()],
]),
{ "foo-allowed": "hi", notAllowed: true },
);
// loose - { 'foo-allowed': 'hi', notAllowed: true }
// strip - { 'foo-allowed': 'hi' }
// strict - error
// rest - validate against rest, error if not match |
fixes #1159
Also includes proper index signatures for strongly typed keys, e.g.