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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add passthrough object schema #480

Closed
vladshcherbin opened this issue Mar 14, 2024 · 5 comments
Closed

Add passthrough object schema #480

vladshcherbin opened this issue Mar 14, 2024 · 5 comments
Assignees
Labels
enhancement New feature or request workaround Workaround fixes problem

Comments

@vladshcherbin
Copy link

vladshcherbin commented Mar 14, 2024

Hey 馃憢

I'm porting my projects to use valibot instead of zod and would love to have some kind of passthrough object schema.

My use case:

I'm validating API requests with lots of nested objects and want to keep unknown props while validating the ones in schema.

With rest object param I can do it this way:

object({ location: string() }, unknown())

It works great.

However with nested objects it becomes hard to track this objects, for example:

Nested object example
const schema = object({
  adverts: array(
    object({
      id: number(),
      locationName: string(),
      metadata: object({
        brandId: number(),
        modelId: number()
      }, unknown()),
      photos: array(
        object({
          big: object({
            height: number(),
            url: string([url()]),
            width: number()
          }, unknown()),
          main: boolean(),
          mimeType: picklist(['image/jpeg', 'image/png'])
        }, unknown())
      ),
      price: object({
        usd: object({
          amountFiat: number(),
          currency: literal('usd')
        }, unknown())
      }, unknown()),
      properties: array(
        object({
          name: string(),
          value: union([boolean(), number(), string()])
        }, unknown()),
        [minLength(1)]
      ),
      publicUrl: string([url()]),
      year: number()
    }, unknown())
  ),
  currentSorting: object({
    label: literal('薪芯胁褘械 芯斜褗褟胁谢械薪懈褟')
  }),
  pageCount: number()
}, unknown())

For me it's quite hard to track such objects, check any missing unknown() rest types, add new objects 馃槩

zod has passthrough() method with pending feature request to have global method.

Feature request

Imagine having a simple type for this in valibot. Just compare:

Proposed api example
const schema = passthroughObject({
  adverts: array(
    passthroughObject({
      id: number(),
      locationName: string(),
      metadata: passthroughObject({
        brandId: number(),
        modelId: number()
      }),
      photos: array(
        passthroughObject({
          big: passthroughObject({
            height: number(),
            url: string([url()]),
            width: number()
          }),
          main: boolean(),
          mimeType: picklist(['image/jpeg', 'image/png'])
        })
      ),
      price: passthroughObject({
        usd: passthroughObject({
          amountFiat: number(),
          currency: literal('usd')
        })
      }),
      properties: array(
        passthroughObject({
          name: string(),
          value: union([boolean(), number(), string()])
        }),
        [minLength(1)]
      ),
      publicUrl: string([url()]),
      year: number()
    })
  ),
  currentSorting: object({
    label: literal('薪芯胁褘械 芯斜褗褟胁谢械薪懈褟')
  }),
  pageCount: number()
})

It's so clean, easy to follow, add, change, etc. It can also have a different name.

Please help reduce the pain of validating such nested objects, I pray for this feature for years 馃檹

@fabian-hiller
Copy link
Owner

You could add passthroughObject on your own by just wrapping object:

export function passthroughObject<TEntries extends v.ObjectEntries>(
  entries: TEntries
): v.ObjectSchema<TEntries, v.UnknownSchema> {
  return v.object(entries, v.unknown());
}

@fabian-hiller fabian-hiller self-assigned this Mar 14, 2024
@fabian-hiller fabian-hiller added enhancement New feature or request workaround Workaround fixes problem labels Mar 14, 2024
@vladshcherbin
Copy link
Author

@fabian-hiller thank you a lot for the example, I'll use it as for now 鈾ワ笍

Hopefully it can be added to core and help other developers too. I actually need it in almost every project with external API requests.

@fabian-hiller
Copy link
Owner

I will think about it. At the moment I prefer strong primitives over specialized functions. Also, I don't want there to be too many ways to achieve the same things within a library, because that makes usage confusing in my eyes.

@fabian-hiller
Copy link
Owner

The latest version now comes with a looseObject schema 馃З

@vladshcherbin
Copy link
Author

@fabian-hiller perfect, thank you! 馃殌

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

No branches or pull requests

2 participants