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

Custom Proxy Type #506

Open
jewell-lgtm opened this issue Nov 9, 2024 · 1 comment
Open

Custom Proxy Type #506

jewell-lgtm opened this issue Nov 9, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@jewell-lgtm
Copy link

Is your feature request related to a problem? Please describe.
The current allowed string proxy types, boolean, number, date, etc. do not adequately describe every conceivable use case. We should be able to specify any type that can be serialized to a string.

Describe the solution you'd like
We should allow stringProxy to receive the predefined types of 'number' | 'boolean' | 'date' etc, or pass an object with toString, toValue functions: e.g.

// given the schema
z.object({
    wrapped: z.object({
        _value: z.string()
    })
})

// the proxy could be used like this
// type inference could make this more concise
type WrapperType  = { _value: T}  
let proxyValue = stringProxy<WrapperType>(superform, 'wrapped', {
    toValue: (value: string) => ({ _value: value }),
    toString: (value: WrapperType) => value._value,
})

Describe alternatives you've considered
The transformation can also take place in the schema, but given the concept of these proxies, it makes sense to also handle some non-primitive types here

Additional context
Due to limitations of Date object in JS, we use the CalendarDate object from @internationalized/date (this also provides interoperability with components built with the melt-ui library)

I took a quick look at the code, and it doesn't seem too complex (I may, of course, be missing some cases I hadn't considered), I'd be happy to submit a PR, but wanted to sanity check the idea, and ask if this had already been considered and rejected. (I didn't find any related issues though)

@jewell-lgtm jewell-lgtm added the enhancement New feature or request label Nov 9, 2024
@ciscoheat
Copy link
Owner

Sounds good, please make a PR! Two extra options like that will be a nice non-breaking feature. My only concern is the type parameter, but I hope it will work fine.

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

No branches or pull requests

2 participants