roziscoding / ts-transformer-ajv

TypeScript transformer to validate types in runtime using JSON Schema and AJV
12 stars 2 forks source link

Transformation fails for simple case #2

Open arikmaor opened 3 years ago

arikmaor commented 3 years ago

I'm getting an error while trying to use this package (which seems very promising)

The project only contains one file src/index.ts

import { getValidatorFromType } from 'ts-transformer-ajv';

interface Foo {
  message: string
}

const x = {
    message: 'hello'
} as any;

function foo(input: Foo) {
    console.log(input.message);
}

const validator = getValidatorFromType<Foo>();
if (validator(x).isValid) {
    foo(x);
} else {
    console.log('nope');
}

this is my tsconfig.json

{
  "compilerOptions": {
    "outDir": "./dist",
    "allowJs": true,
    "target": "es2018",
    "module": "commonjs",
    "lib": ["es2018", "esnext"],
    "esModuleInterop": true,
    "sourceMap": true,
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "experimentalDecorators": true,
    "plugins": [
      { "transform": "ts-transformer-ajv" }
    ]
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

This is the error from ttsc:

> transformers-test@1.0.0 build /Users/arik/projects/transformers-test
> ttsc

/Users/arik/projects/transformers-test/node_modules/typescript/lib/typescript.js:106221
                throw e;
                ^

Error: type Foo not found
    at JsonSchemaGenerator.getSchemaForSymbol (/Users/arik/projects/transformers-test/node_modules/typescript-json-schema/dist/typescript-json-schema.js:877:19)
    at Object.generateSchema (/Users/arik/projects/transformers-test/node_modules/typescript-json-schema/dist/typescript-json-schema.js:1058:26)
    at Object.generateSchemaForType (/Users/arik/projects/transformers-test/node_modules/ts-transformer-ajv/dist/lib/generate-schema-for-type.js:15:24)
    at Object.getReplacement (/Users/arik/projects/transformers-test/node_modules/ts-transformer-ajv/dist/replacers/get-validator-from-type.js:33:47)
    at getCallExpressionReplacement (/Users/arik/projects/transformers-test/node_modules/ts-transformer-ajv/dist/transformer.js:20:21)
    at visitor (/Users/arik/projects/transformers-test/node_modules/ts-transformer-ajv/dist/transformer.js:29:55)
    at visitNode (/Users/arik/projects/transformers-test/node_modules/typescript/lib/typescript.js:80329:23)
    at Object.visitEachChild (/Users/arik/projects/transformers-test/node_modules/typescript/lib/typescript.js:80723:225)
    at visitor (/Users/arik/projects/transformers-test/node_modules/ts-transformer-ajv/dist/transformer.js:33:27)
    at visitNodes (/Users/arik/projects/transformers-test/node_modules/typescript/lib/typescript.js:80382:48)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! transformers-test@1.0.0 build: `ttsc`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the transformers-test@1.0.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/arik/.npm/_logs/2020-12-18T14_22_38_913Z-debug.log

Any clue?

roziscoding commented 3 years ago

Sorry for only seeing this now. I'll take a look at it and get back to you ASAP

gen4sp commented 2 years ago

the same issue. is there any news?

jeffersonmourak commented 2 years ago

Made it work, using "typescript": "^3.8.3" & "ajv": "^6.12.2", the most recent versions throw the error. I know it's a work around but at least can be used in while the author work on the fix.

roziscoding commented 2 years ago

I'll look into this, but I highly recommend moving to a more modern solution, like Zod, which also allows you to write things only once, but is way less hacky and has more advanced features

arikmaor commented 2 years ago

But in Zod you can't define your types with typescript, which is why I'd like you're project more

roziscoding commented 2 years ago

No, but you can define them with Zod and have Zod generate the types for you

roziscoding commented 2 years ago

Yeah, I started looking into this but, as I suspected, this will not work for newer typescript versions. That is actually why I recommend avoid using transformers: TypeScript API changes a lot between versions, so transformers are very fragile. I'm sorry guys, but I don't have the time necessary to update or mantain this library. I'll deprecate it, but feel free to fork and publish your own.