-
-
Notifications
You must be signed in to change notification settings - Fork 218
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
feat: create entriesFromObjects
utils
#1023
base: main
Are you sure you want to change the base?
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
commit: |
Thanks for creating this PR! I think your second code example in was meant to be: const ParentSchema = v.object({
foo: v.string(),
});
const Schema = v.object(
v.entriesFromObjects(
ParentSchema,
v.object({
bar: v.string(),
})
)
); |
Should we add a TypeScript overload signature that forces the user to pass at least two object schemas to export function entriesFromObjects<
const TSchema1 extends Schema,
const TSchema2 extends Schema,
const TSchemas extends Schema[],
>(
schema1: TSchema1,
schema2: TSchema2,
...schemas: TSchemas
): MergedEntries<[TSchema1, TSchema2, ...TSchemas]>; |
Thanks for pointing out the mistake of the example 😆 just fixed.
A great suggestion about forcing the user to pass a minimum of object schemas. How about only enforcing at least 1 object schema? For my use case, I created a generic schema and I can just pass entries to it and it will output a modified schema. // @__NO_SIDE_EFFECTS__
export function genericSchema<T extends v.ObjectEntries>(obj: T) {
return v.object({
auth: v.optional(v.string()),
...obj,
})
}
// pass entries directly
const gfooSchema = genericSchema({ foo: v.string() })
// pass entries from object
const barSchema = v.object({ bar: v.string() })
const gBarSchema = genericSchema(v.entriesFromObjects(barSchema)) If I create const gBarSchema = genericSchema(barSchema.entries) // maybe cannot tree-shake |
I plan to finalize and merge this PR in the next one or two weeks. |
Sorry for my delay. I am busy with personal stuff. Hope to merge this after v1 RC is out. |
No problem, take your time. 😁 Thanks! |
Resolve #1017
This PR create an new util
entriesFromObjects
. You can use it extract entries from multiple objects for prevent using spread operator (...
) and.entries
(property access behavior) for better tree-shaking result.Original extends
v.object
With new
entriesFromObjects