samchon / typia

Super-fast/easy runtime validators and serializers via transformation
https://typia.io/
MIT License
4.62k stars 159 forks source link

Cannot use prisma types after migration #892

Closed celloman1929 closed 5 months ago

celloman1929 commented 11 months ago

First, I failed to create a reproduction. This issue seems not reproducible unless I share our private repo.

I use typia for runtime type checking in place of manual typeguards, and often use with prisma types which are generated under @prisma/client. However, today I had some major updates in prisma schema, and made a new migration. After this migration, all typia validations using prisma generated types are emitting error saying "no transform has been configured".

import { Prisma } from "@prisma/client";
import { is } from "typia";
is<Prisma.UserCreateInput>(input);
/Users/celloman/Project/api/node_modules/.pnpm/typia@5.3.1_typescript@5.3.2/node_modules/typia/src/module.ts:893
  throw new Error(
        ^
Error: Error on typia.is(): no transform has been configured. Read and follow https://typia.io/docs/setup please.
    at halt (/Users/celloman/Project/api/node_modules/.pnpm/typia@5.3.1_typescript@5.3.2/node_modules/typia/src/module.ts:893:9)
    at is (/Users/celloman/Project/api/node_modules/.pnpm/typia@5.3.1_typescript@5.3.2/node_modules/typia/src/module.ts:201:3)
    at Object.<anonymous> (/Users/celloman/Project/api/src/t.ts:25:3)
    at Module._compile (node:internal/modules/cjs/loader:1241:14)
    at Module.m._compile (/Users/celloman/Library/pnpm/global/5/.pnpm/ts-node@10.9.1_@types+node@20.9.4_typescript@5.3.2/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1295:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/celloman/Library/pnpm/global/5/.pnpm/ts-node@10.9.1_@types+node@20.9.4_typescript@5.3.2/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Function.Module._load (node:internal/modules/cjs/loader:938:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)

For example, above code used to work, but after migration its not working. Other validations using other types are working fine at the same moment. So this is prisma specific problem with typia.

I upgraded typescript and typia version to latest today, but I confirmed this error is happening in previous versions – typescript@5.2.2 & typia@5.2.6. I've cloned and reset commits migrating multiple times, but prisma migration is the only case triggering this error. I've tried cleaning node_modules, or using npm or yarn for the package manager, but whenever I migrate the updated schema typia stops working with prisma generated types.

Is there any similar issue you are aware of? Or any guess for this happening?

Here's my tsconfig

// tsconfig.json
{
  "include": [
    "src/**/*.ts",
    "test/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "compilerOptions": {
    "outDir": "dist",
    "baseUrl": ".",
    "paths": {
      "@/*": ["src/*"],
      "@test/*": ["test/*"],
    },

    "target": "ES2022",
    "module": "CommonJS",
    "lib": ["ES2022"],

    "esModuleInterop": true,
    "moduleResolution": "Node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,

    "incremental": true,
    "declaration": true,
    "sourceMap": true,
    "skipLibCheck": true,
    "removeComments": true,

    "strict": true,
    "strictNullChecks": true,
    "noUnusedLocals": true,
    "plugins": [
      {
        "transform": "typia/lib/transform"
      }
    ]
  }
}
samchon commented 11 months ago

There had been break changes on TS 5.3.

Upgrade typia to the latest version, and try npx typia setup command.

celloman1929 commented 11 months ago

Upgraded typia to 5.3.2, still not working 😭

I separated schema changes and applied them literally line by line. And I realised adding unique constraint to a certain table is triggering this sudden typia error. Even there are other tables with @@unique set, this particular table is making troubles.

I guess I'll have to compare and look into generated source codes more thoroughly.

If I ever find the reason, I'll leave another comment here. But just letting you know there are some cases when typia cannot transform some kind of types.

samchon commented 11 months ago

The @@unique constraints does not affect to the validation.

Run tsc command and tell me which message comes.

celloman1929 commented 11 months ago

The @@unique constraints does not affect to the validation.

Yeah, I know it doesn't make sense at all.

Running tsc has no error, so I may be wrong saying that transform has some problem. I've compared generated codes – literally all generated codes by prisma migrade, but couldn't find any potential flaws.

This leaves the another case that it might be import problem under .prisma. Native types in @prisma/client like Prisma.JsonObject works fine, but not schema generated types which are imported from .prisma. Prisma's operation works – doing stuffs with new table – but, typia is not. So, I assume something inside typia related to runtime import has potential flaws. I wish I could create a reproduction so that you can look into it as this part is what I cannot do.

Don't be offended. I'm not blaming typia nor you for this issue. I know how it sounds ridiculous to see an issue which doesn't make sense and cannot be reproducible. I love typia and I'm detouring not using unique constraints for this table. So nobody has problem now. Like I said before, just reporting some unknown bug that might happen again in the future.😁

samchon commented 11 months ago

I think you'd better to provide reproducible repo with researching.

I'm also using prisma, but could not experienced such unique constraint related bug.

  1. At first, try to update every prisma, typia, nestia and typescript versions to be the latest one
  2. Check that you're using non-standard TypeScript compiler like Babel/SWC (jest also using Babel)
  3. Try to re-run the npx typia setup command

If nothing be fixed after such trials, then consider inviting me to the private repo.