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

Check for string length and number range #207

Open
slevy85 opened this issue Oct 10, 2022 · 5 comments
Open

Check for string length and number range #207

slevy85 opened this issue Oct 10, 2022 · 5 comments
Labels
enhancement New feature or request

Comments

@slevy85
Copy link

slevy85 commented Oct 10, 2022

Really appreciate the possibility to use typescript interface to generate runtime code.
And also it is very nice to be able to use the typescript interfaces as it is, not having to rewrite them into library-dependant code.

This being said, one other feature that could be very useful is to have validators also generated.

Here is a code sample to give ideas:

type MinLength<T extends number> = string
type MaxLength<T extends number> = string
type Min<T extends number> = number
type Max<T extends number> = number

interface User {
  name: MinLength<4> & MaxLength<100>
  age: Min<13> & Max<99>
}

The interfaces MinLength, MaxLength, Min, Max (and others) would be exported by ts-auto-guard, the generator will use these types to generate validateUser.

@rhys-vdw
Copy link
Owner

How would the generator know what to generate for MinLength? Would this be an exported suite of validator types?

@slevy85
Copy link
Author

slevy85 commented Oct 10, 2022

Yes the validator types would be exported by ts-auto-guard itself.

@rhys-vdw
Copy link
Owner

It's an interesting idea, but I think out of scope for ts-auto-guard, unless it's possible to express those constraints via the type system itself, which apparently it is:

https://stackoverflow.com/a/39495173/317135

https://stackoverflow.com/a/54832231/317135

However both of these look pretty hectic, it may not be worth the maintenance cost.

@slevy85
Copy link
Author

slevy85 commented Oct 11, 2022

Yes you are right that it is not in the scope of the "guard" system but a completely new feature, and I wanted to share it with you because you already made the logic of using static types and generating some code from them. This feature would reuse the same logic, the only difference will the generated code I think.

@rhys-vdw
Copy link
Owner

It looks as though something like this is possible via static types, and it would be cool to support it with type guards, but given the amount of active support this project receives I don't want to expand its feature set.

You're of course welcome to take whatever you like from the code base to make a similar project though, and I see that it could be useful.

I'll leave this issue open in case someone wants to have a shot at handling these type constraints that are possible in TS.

@rhys-vdw rhys-vdw added the enhancement New feature or request label Oct 12, 2022
@rhys-vdw rhys-vdw changed the title Implement an "auto-validate" feature Check for string length and number range Oct 12, 2022
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