feat: create recordWithPatterns schema#1165
feat: create recordWithPatterns schema#1165EskiMojo14 wants to merge 5 commits intoopen-circle:mainfrom
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 |
762dfc3 to
4f7bee8
Compare
|
@EskiMojo14 is attempting to deploy a commit to the Open Circle Team on Vercel. A member of the Team first needs to authorize it. |
commit: |
fixes #1159
Also includes proper index signatures for strongly typed keys, e.g.