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

map attributes should allow arbitrary properties #450

Open
solaris007 opened this issue Nov 26, 2024 · 3 comments
Open

map attributes should allow arbitrary properties #450

solaris007 opened this issue Nov 26, 2024 · 3 comments

Comments

@solaris007
Copy link

Describe the bug
When wanting to specify an attribute of type map, a full map-schema via the properties setting is required, effectively locking down what kind of maps can be stored for that attribute. There are use-cases however, where an attribute is desired to be a map, yet the contents of that map may vary. Similar to OpenAPI specs, we could envision to allow setting additionalProperties: true, in which case the value would be validated as a valid object but not enforce a schema within the object.

Currently this needs to be worked around like this with custom validation:

    data: {
      type: 'any',
      required: false,
      validate: (value) => !value || isNonEmptyObject(value),
    },

ElectroDB Version
3.0.1

@tywalch
Copy link
Owner

tywalch commented Nov 26, 2024

Hi @solaris007 👋

This sounds like a good job for the CustomAttributeType function. It allows you to have fine-grain typing for an attribute; here is a playground example.

A lot of people then pair this with a validation library like zod or jsonschema to accomplish the validation side of things 👍

@solaris007
Copy link
Author

Hi @tywalch :) Thank you for your answer!

Certainly, the CustomAttributeType would be a great choice for more complex validations / types, but seems counter-intuitive / over-engineered for the simple case where we want to accept just any map?

@tywalch
Copy link
Owner

tywalch commented Nov 27, 2024

I suppose this was mainly to demonstrate that any type could be used, even something simple like Record<string, string> or { [key: string]: unknown }, which sounds like what you're trying to accomplish. Does this fall short of what you might be expecting?

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