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

Adding rest props for object-type #1162

Closed
vtrushin opened this issue Dec 28, 2022 · 2 comments
Closed

Adding rest props for object-type #1162

vtrushin opened this issue Dec 28, 2022 · 2 comments

Comments

@vtrushin
Copy link

vtrushin commented Dec 28, 2022

First of all, thank you for this convenient tree-shakeable lib!

Sometimes we don't need to know, what the keys are in our object, but only their types
E.g. We keep addresses, added by user in structure like that:

addresses: {
  [addressName: string]: string
}

TS allows us to do it

What if to add similar behavior in object structure, using a Symbol for that. Symbol lets you distinguish this props from any others:

// Inside Superstruct lib
export const anyProp = Symbol()
...
const isAnyProp = key === anyProp
...

// On Superstruct's user side
import { object, anyProp } from 'superstruct'

const Addresses = object({
  requiredAddressField: string,
  [anyProp]: string
})
@arturmuller
Copy link
Collaborator

Hi @vtrushin!

It's been a while since you asked this question, but perhaps the answer might still be useful for you or others.

You should be able to achieve what you ask for with the existing features of superstruct:

// Just like in TS where you can describe your desired behaviour with an intersection of a
// type and a record:
type Addresses = { requiredAddressField: string } & Record<string, string>

// ...in Superstruct, you can do the same thing:
const addresses = intersection([
  type({ requiredAddressField: string() }),
  record(string(), string()),
]);

Note that it is important that you use type not object in this case, as object would not allow extraneous keys and validation would therefore not pass.


I will close this issue for now, but if there are some additional details you would like to discuss, feel free to comment!

@vtrushin
Copy link
Author

@arturmuller Thank you for your answer!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants