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

Option to replace shield() with another runtime validator (e.g. zod) #97

Open
pencilcheck opened this issue Jan 2, 2024 · 8 comments
Open

Comments

@pencilcheck
Copy link

pencilcheck commented Jan 2, 2024

I have been using drizzle orm for database typing generation. I have a enum I want to expose as a function parameter. I have created the type but I'm not sure if I can pass it onto the field, as it seems like shield is expecting a certain type of typing input in order for it to work.

I know how to specify enum from shield, but is it possible to allow custom typing such as drizzle to be used instead?

Example

import { recordType } from '../../../server/drizzle/schema';

type RecordType = typeof recordType

const t = shield.type;

const hello = shield(
  [{
    recordType: t.or(
      t.const('HIDDEN'),
      t.const('PENDING'),
      t.const('CONFIRMED')
    ), <<<<---- replace it with RecordType?
    year: t.number,
    timezone: t.string
  }],
@brillout
Copy link
Owner

brillout commented Jan 2, 2024

I agree it would be nice, but it's far from trivial, and not even possible if drizzle doesn't support type inference.

As for adding support for Zod, it's a consideration but so far shield() seems to be working for the (vast?) majority of use cases. Especially when following the recommendation depicted at https://telefunc.com/event-based.

Closing as I don't think it should be a priority right now: so far, I don't see the added value to be worth the effort. Feel free to object and elaborate.

Contribution or sponsoring welcome.

@pencilcheck
Copy link
Author

pencilcheck commented Jan 7, 2024

I agree it would be nice, but it's far from trivial, and not even possible if drizzle doesn't support type inference.

As for adding support for Zod, it's a consideration but so far shield() seems to be working for the (vast?) majority of use cases. Especially when following the recommendation depicted at https://telefunc.com/event-based.

Closing as I don't think it should be a priority right now: so far, I don't see the added value to be worth the effort. Feel free to object and elaborate.

Contribution or sponsoring welcome.

I don't know what is the right solution, I am just thinking if there is a better integration of type validation with other libraries such as drizzle or zod.

Right now all types has to be constructed using shield's buildin type

@brillout brillout added the enhancement ✨ New feature or request label Jan 7, 2024
@brillout
Copy link
Owner

brillout commented Jan 7, 2024

better integration [...] with other libraries such as drizzle

I agree that'd be nice. But, again, I don't know if it's possible: does Drizzle types support runtime validation like Zod does?

As for Zod, I still don't see much of an added value compared to shield(). But feel free to elaborate if you think otherwise. (I understand Zod has semantic validation such as email validation, but such semantic validation doesn't add any value regarding TypeScript runtime validation.)

@brillout
Copy link
Owner

I'm not against this at all. I'm just waiting for a good reason for implementing this. Labeling this as low-prio until then.

@brillout
Copy link
Owner

brillout commented Apr 5, 2024

Actually, form data is a valid use case for this as explained at #105.

Removing low-prio 🐌, but there are still other higher priorities: https://github.com/brillout/telefunc/issues?q=is%3Aissue+is%3Aopen+label%3A%22high-prio+%F0%9F%92%AB%22.

That said, contribution or sponsoring welcome.

If someone wants to contribute, we can start by having a look at how tRPC does it.

@brillout brillout mentioned this issue Apr 5, 2024
@brillout brillout changed the title Uses zod or other type composition API for a replacement for typing in shield? (Typescript manual use case) Option to replace shield() with another runtime validator (e.g. zod) Apr 10, 2024
@brillout
Copy link
Owner

I'm thinking, maybe an alternative could be to allow users to use Telefunc's shield() independently of Telefunc. There are zillions of runtime validators, can't we just implement one that works for the vast majority of use cases? Or are there really that many use cases that justify being able to pick a validator that fits best? (As for semantic validation, the user could use a semantic validator in addition to using shield().)

@pencilcheck
Copy link
Author

shield handles basic use cases well, but it doesn't seem to allow defining more complex use cases such as array structure, nested object structure, etc. If shield can be extended with other custom validator and it works well then it would be very useful.

But the use case this post is about isn't about extensibility. I'm asking if it is possible if I can have my typescript types to be used with shield.

@brillout
Copy link
Owner

brillout commented Jul 5, 2024

@pencilcheck Can you elaborate your use case for needing complex Telefunction arguments? Are you following https://telefunc.com/event-based? The more I know, the better can I take informed decisions.

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

No branches or pull requests

2 participants