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

Generated type validation doesn't work for non-optional any property #140

Closed
tvillaren opened this issue Jun 2, 2023 · 3 comments · Fixed by #142
Closed

Generated type validation doesn't work for non-optional any property #140

tvillaren opened this issue Jun 2, 2023 · 3 comments · Fixed by #142

Comments

@tvillaren
Copy link
Collaborator

Bug description

When using the any keyword to accept any input for a property, like this:

export interface Citizen {
  name: any
};

ts-to-zod generates the right output

export const citizenSchema = z.object({
  name: z.any()
});

However, as mentioned in issue colinhacks/zod#1628, the type inference from zod makes the name property optional, failing the ts-to-zod generated type validation (see 52cf4f7 for a failing test case).

Indeed, when looking at the inferred type via z.infer<typeof citizenSchema> we get the following:

type Citizen = {
  name?: any
}

which doesn't match the initial type, thus fail validation.

There is no full consensus on the fix on the zod Github.
👉 An user proposed a workaround but that would mean using a custom function instead of z.object().
👉 We could ignore this specific generated type validation error, but it may come from various sources.
👉 We could live with it, but that would mean only supporting any for optional properties (and thus, warn ts-to-zod users about this upon generation).

Happy to try to fix this but not sure toward which direction we could go 🤷

@fabien0102
Copy link
Owner

Interesting! I guess we can relax the validator for this usecase. The validator is here to help discovering missing types, so I think it's fine to ignore this case, I can have a look on Monday if you want! 😊

Have a nice weekend

@tvillaren
Copy link
Collaborator Author

Hello @fabien0102,

Have you had a chance to look at this?
If not, I can give it a try but I'm not sure what to do to ignore this case. Any pointer would be nice! Thx.

@fabien0102
Copy link
Owner

As you probably guessed, I had no time whatsoever 🙈 Until now! 😁

This should be fixed, thanks again for the clear reporting and follow up

tvillaren added a commit to tvillaren/ts-to-zod that referenced this issue Jul 20, 2023
tvillaren added a commit to tvillaren/ts-to-zod that referenced this issue Jul 20, 2023
tvillaren added a commit to tvillaren/ts-to-zod that referenced this issue Sep 7, 2023
tvillaren added a commit to tvillaren/ts-to-zod that referenced this issue Sep 7, 2023
tvillaren added a commit to tvillaren/ts-to-zod that referenced this issue Sep 8, 2023
tvillaren added a commit to tvillaren/ts-to-zod that referenced this issue Sep 8, 2023
tvillaren added a commit to tvillaren/ts-to-zod that referenced this issue Nov 8, 2023
tvillaren added a commit to tvillaren/ts-to-zod that referenced this issue Nov 8, 2023
tvillaren added a commit to tvillaren/ts-to-zod that referenced this issue Nov 29, 2023
tvillaren added a commit to tvillaren/ts-to-zod that referenced this issue Nov 29, 2023
tvillaren added a commit to tvillaren/ts-to-zod that referenced this issue Nov 29, 2023
tvillaren added a commit to tvillaren/ts-to-zod that referenced this issue Nov 29, 2023
tvillaren added a commit to tvillaren/ts-to-zod that referenced this issue Feb 5, 2024
tvillaren added a commit to tvillaren/ts-to-zod that referenced this issue Feb 5, 2024
schiller-manuel pushed a commit that referenced this issue Feb 5, 2024
* test: failing test case

* test: additional test case

* feat: new util function to extract import identifiers

* feat: more import utils, in their own file

* feat: add new helper generating instanceof

* feat: adding non-relative import handling of classes

* doc: adding documentation

* test: adding extend test case

* clean: remove moved tests after rebase

* test: union case test

* fix: remove unused const

* feat: allow any module import (relative & non-relative)

* test: add test to cover issue #140

* fix: same behavior as #140 with 3rd party imports

* feat: make arg optional

* fix: update tests after Jest update

* fix: typo in tests

* feat: enabling refs to extra files in post-generation validation

* revert switch to generic fixOptional

* fix: remove instanceof handling

* clean: remove wrongly commited file

* clean: remove unused isRelativeModuleImport

* doc: rework

* fix: missing makePosixPath after rebase

* clean: removing commented out bits

---------

Co-authored-by: tvillaren <tvillaren@users.noreply.github.com>
tvillaren added a commit to tvillaren/ts-to-zod that referenced this issue Feb 6, 2024
schiller-manuel pushed a commit that referenced this issue Feb 6, 2024
* test: failing test case

* test: additional test case

* feat: new util function to extract import identifiers

* feat: more import utils, in their own file

* feat: add new helper generating instanceof

* feat: adding non-relative import handling of classes

* doc: adding documentation

* test: adding extend test case

* clean: remove moved tests after rebase

* test: union case test

* fix: remove unused const

* feat: allow any module import (relative & non-relative)

* test: add test to cover issue #140

* fix: same behavior as #140 with 3rd party imports

* feat: make arg optional

* fix: update tests after Jest update

* fix: typo in tests

* feat: enabling refs to extra files in post-generation validation

* revert switch to generic fixOptional

* fix: remove instanceof handling

* clean: remove wrongly commited file

* clean: remove unused isRelativeModuleImport

* doc: rework

* fix: missing makePosixPath after rebase

* test: adding failing tests

* fix: typo

* fix: complex array were not traversed

* fix: missing TS helpers in type extractor

* test: failing test case

* test: additional test case

* fix: same behavior as #140 with 3rd party imports

* feat: add createImport function

* feat: handle input / output mapping as source for generate

* feat: add zod import handling from config file

* doc: adding documentation for ZOD imports

* refacto test

* test: adding hybrid import

* test: adding new test

* fix: using relative to compare paths

* fix: multiple fixes after rebase

* fix: remove useless imports from output

* update documentation

* fix: build issues after rebase

---------

Co-authored-by: tvillaren <tvillaren@users.noreply.github.com>
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

Successfully merging a pull request may close this issue.

2 participants