ts-to-zod icon indicating copy to clipboard operation
ts-to-zod copied to clipboard

BUG: Files are not created and does not respect the folder path

Open Manuelbaun opened this issue 2 years ago • 1 comments

Bug description

As the title states, when trying to generate the zod schemas, the *.zod.ts file as specified, is not created. Also it tries to access the project root instead of the path, where the file is located.

Input

// cli commands
pnpm ts-to-zod ./src/source.ts ./src/source.zod.ts  OR
pnpm ts-to-zod --config models 

// ts-to-zod.config.js
module.exports = [
    {
        name: 'models',
        input: 'src/source.ts',
        output: 'src/source.zod.ts',
    },
];

Expected output

// Expected Zod schemas

Actual output

⠇ Validating generated types    Error: error TS6053: File
    '../projectroot/source.integration.ts' not found.
      The file is in the program because:
        Root file specified for compilation
    error TS6053: File '../projectroot/source.ts' not found.    
      The file is in the program because:
        Root file specified for compilation
    error TS6053: File '../projectroot/source.zod.ts' not found.
      The file is in the program because:
        Root file specified for compilation

Somehow it assumes the files to be located at the root of the projects, although I specify the ./src in the file names.

However, If I move the source.ts to the root level, and try, I will get the same error, But if I create source.zod.ts and source.integration.ts then it generates fine.

Versions

  • Typescript: v4.8.4
  • Zod: v3.19.1
  • ts-to-zod: v1.13.1

Manuelbaun avatar Oct 05 '22 10:10 Manuelbaun

@Manuelbaun same behaviour here at CLI commands, but code approach is working, you can create an script like that:

import { generate } from "ts-to-zod";
import fs from 'fs'

const schemaGenerator = generate({
  sourceText: fs.readFileSync('./src/database/typeorm/schema/interface.ts', { encoding: 'utf-8' })
    .split(/\r?\n/)
    .slice(1)
    .join("\n"),
  keepComments: true,
  skipParseJSDoc: true
});

fs.writeFileSync('./src/database/typeorm/schema/zod.ts', schemaGenerator.getZodSchemasFile('./src/database/typeorm/schema/interface.ts'))

cjfswd avatar Oct 31 '22 18:10 cjfswd

I've been using CLI and relative paths for a while, it seems to work fine. Feel free to comment with more details (OS, Zod version) if this still happens 🙏

tvillaren avatar Aug 26 '24 21:08 tvillaren