Skip to content
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

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

serkodev
Copy link
Contributor

@serkodev serkodev commented Jan 18, 2025

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

const ParentSchema = v.object({
  foo: v.string(),
});

const Schema = v.object({
  ...ParentSchema.entries,
  bar: v.string(),
});

With new entriesFromObjects

const ParentSchema = v.object({
  foo: v.string(),
});

const Schema = v.object(
  v.entriesFromObjects(
    ParentSchema,
    v.object({
      bar: v.string(),
    })
  )
);

Copy link

vercel bot commented Jan 18, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
valibot ✅ Ready (Inspect) Visit Preview 💬 Add feedback Feb 1, 2025 8:00pm

Copy link

pkg-pr-new bot commented Jan 18, 2025

Open in Stackblitz

npm i https://pkg.pr.new/valibot@1023

commit: d9d0967

@fabian-hiller
Copy link
Owner

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(),
    })
  )
);

@fabian-hiller fabian-hiller self-assigned this Jan 18, 2025
@fabian-hiller fabian-hiller added the enhancement New feature or request label Jan 18, 2025
@fabian-hiller
Copy link
Owner

fabian-hiller commented Jan 18, 2025

Should we add a TypeScript overload signature that forces the user to pass at least two object schemas to entriesFromObjects?

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]>;

@serkodev
Copy link
Contributor Author

serkodev commented Jan 18, 2025

Thanks for pointing out the mistake of the example 😆 just fixed.

Should we add a TypeScript overload signature that forces the user to pass at least two object schemas to entriesFromObjects?

A great suggestion about forcing the user to pass a minimum of object schemas.

How about only enforcing at least 1 object schema?
There might be some cases where user uses this utility on a single object schema to avoid accessing the .entries property directly.

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 gBarSchema by barSchema.entries like below, it may cannot tree-shake properly.

const gBarSchema = genericSchema(barSchema.entries) // maybe cannot tree-shake

@fabian-hiller
Copy link
Owner

I plan to finalize and merge this PR in the next one or two weeks.

@fabian-hiller
Copy link
Owner

Sorry for my delay. I am busy with personal stuff. Hope to merge this after v1 RC is out.

@serkodev
Copy link
Contributor Author

serkodev commented Feb 3, 2025

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request priority This has priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Side effects when using the spread operator to merge objects
2 participants