Skip to content

feat: add validationSchemaExportType to config #382

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

Closed

Conversation

elijaholmos
Copy link
Contributor

Resolves #164

Adds a new config option, validationSchemaExportType, to specify the export type of the generated validation schema (either function or const), Feature supports yup, zod, and myzod.

@elijaholmos elijaholmos changed the title feat: add validationSchemaExportType to config feat: add validationSchemaExportType to config Jun 2, 2023
@Code-Hex
Copy link
Owner

Code-Hex commented Jun 6, 2023

@elijaholmos wow!! Amazing your work! I checked your code but still fails on the build.

$ yarn type-check:yup
yarn run v1.22.19
$ tsc --strict --noEmit example/yup/schemas.ts
example/yup/schemas.ts:91:10 - error TS1005: ':' expected.

91   return union<UserKind>(AdminSchema, GuestSchema)
            ~~~~~


Found 1 error in example/yup/schemas.ts:91

error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
$ yarn type-check:zod
yarn run v1.22.19
$ tsc --strict --noEmit example/zod/schemas.ts
example/zod/schemas.ts:95:10 - error TS1005: ':' expected.

95   return z.union([AdminSchema, GuestSchema])
            ~


Found 1 error in example/zod/schemas.ts:95

error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
$ yarn type-check:myzod
yarn run v1.22.19
$ tsc --strict --noEmit example/myzod/schemas.ts
example/myzod/schemas.ts:87:10 - error TS1005: ':' expected.

87   return myzod.union([AdminSchema, GuestSchema])
            ~~~~~


Found 1 error in example/myzod/schemas.ts:87

error Command failed with exit code 2.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

There may be something wrong with the generation of union type.

export const UserKindSchema = {
  return z.union([AdminSchema, GuestSchema])
};

I think we are expected:

export const UserKindSchema = z.union([AdminSchema, GuestSchema])

@elijaholmos
Copy link
Contributor Author

good catch, @Code-Hex! I updated the union generation code to fix that.

however, I am now running into a new problem... depending on the order of the code generation, I can end up with a "variable being used before its declaration" error:

$ yarn type-check
yarn run v1.22.19
$ tsc --noEmit
example/myzod/schemas.ts:80:11 - error TS2448: Block-scoped variable 'UserKindSchema' used before its declaration.

80     kind: UserKindSchema.optional().nullable(),
             ~~~~~~~~~~~~~~

  example/myzod/schemas.ts:86:14
    86 export const UserKindSchema = myzod.union([AdminSchema, GuestSchema]);
                    ~~~~~~~~~~~~~~
    'UserKindSchema' is declared here.

example/myzod/schemas.ts:80:11 - error TS2454: Variable 'UserKindSchema' is used before being assigned.

80     kind: UserKindSchema.optional().nullable(),
             ~~~~~~~~~~~~~~

example/yup/schemas.ts:84:11 - error TS2448: Block-scoped variable 'UserKindSchema' used before its declaration.

84     kind: UserKindSchema.nullable().optional(),
             ~~~~~~~~~~~~~~

  example/yup/schemas.ts:90:14
    90 export const UserKindSchema: yup.MixedSchema<UserKind> = union<UserKind>(AdminSchema, GuestSchema);
                    ~~~~~~~~~~~~~~
    'UserKindSchema' is declared here.

example/yup/schemas.ts:84:11 - error TS2454: Variable 'UserKindSchema' is used before being assigned.

84     kind: UserKindSchema.nullable().optional(),
             ~~~~~~~~~~~~~~

example/zod/schemas.ts:88:11 - error TS2448: Block-scoped variable 'UserKindSchema' used before its declaration.

88     kind: UserKindSchema.nullish(),
             ~~~~~~~~~~~~~~

  example/zod/schemas.ts:94:14
    94 export const UserKindSchema = z.union([AdminSchema, GuestSchema]);
                    ~~~~~~~~~~~~~~
    'UserKindSchema' is declared here.

example/zod/schemas.ts:88:11 - error TS2454: Variable 'UserKindSchema' is used before being assigned.

88     kind: UserKindSchema.nullish(),
             ~~~~~~~~~~~~~~


Found 6 errors in 3 files.

I was able to fix this with the enum declarations by hoisting them to the top of the file since they do not reference other schemas in their declarations, but this is more tricky with unions which directly reference other generated variables. In the above examples, I cannot hoist UserKindSchema to the top, because then it will be referencing AdminSchema and GuestSchema before their initialization. The UserKindSchema declaration needs to come before UserSchema but after AdminSchema and GuestSchema to pass the TS compiler check. What are your thoughts on this predicament? Do you have any advice on how to properly resolve the errors?

@Code-Hex
Copy link
Owner

Code-Hex commented Jun 8, 2023

@elijaholmos Thanks!
I fixed your problem in #389

@Code-Hex Code-Hex closed this Jun 8, 2023
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 this pull request may close these issues.

[Feature Request] configurable export type
2 participants