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

Option to use z.instanceof(Decimal) for Decimal fields #94

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ model User {
| `output` | Output directory for the generated zod schemas | `string` | `./generated` |
| `isGenerateSelect` | Enables the generation of Select related schemas and the select property | `boolean` | `false` |
| `isGenerateInclude` | Enables the generation of Include related schemas and the include property | `boolean` | `false` |
| `useDecimalLib` | Treat Decimal values as instances of Decimal library instead of numbers | `boolean` | `false` |

|
Use additional options in the `schema.prisma`

```prisma
Expand All @@ -136,5 +138,6 @@ generator zod {
output = "./generated-zod-schemas"
isGenerateSelect = true
isGenerateInclude = true
useDecimalLib = true
}
```
13 changes: 13 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"devDependencies": {
"@types/node": "^18.11.18",
"@types/prettier": "^2.7.2",
"decimal.js": "^10.4.3",
"prisma": "^4.8.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
Expand Down
12 changes: 7 additions & 5 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ generator zod {
output = "./generated"
isGenerateSelect = true
isGenerateInclude = true
useDecimalLib = true
}

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
books Book[]
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
books Book[]
rating Decimal
}

model Post {
Expand Down
3 changes: 3 additions & 0 deletions src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { changeOptionalToRequiredFields } from './whereUniqueInput-helpers';
interface AddMissingInputObjectTypeOptions {
isGenerateSelect: boolean;
isGenerateInclude: boolean;
useDecimalLib: boolean;
}

export function addMissingInputObjectTypes(
Expand Down Expand Up @@ -53,6 +54,7 @@ export function addMissingInputObjectTypes(
);
Transformer.setIsGenerateInclude(true);
}
Transformer.setUseDecimalLib(options.useDecimalLib);

changeOptionalToRequiredFields(inputObjectTypes);
}
Expand All @@ -63,5 +65,6 @@ export function resolveAddMissingInputObjectTypeOptions(
return {
isGenerateSelect: generatorConfigOptions.isGenerateSelect === 'true',
isGenerateInclude: generatorConfigOptions.isGenerateInclude === 'true',
useDecimalLib: generatorConfigOptions.useDecimalLib === 'true',
};
}
12 changes: 12 additions & 0 deletions src/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export default class Transformer {
private static isCustomPrismaClientOutputPath: boolean = false;
private static isGenerateSelect: boolean = false;
private static isGenerateInclude: boolean = false;
private static useDecimalLib: boolean = false;

constructor(params: TransformerParams) {
this.name = params.name ?? '';
Expand All @@ -54,6 +55,10 @@ export default class Transformer {
this.isGenerateInclude = isGenerateInclude;
}

static setUseDecimalLib(useDecimalLib: boolean) {
this.useDecimalLib = useDecimalLib;
}

static getOutputPath() {
return this.outputPath;
}
Expand Down Expand Up @@ -133,6 +138,11 @@ export default class Transformer {
let alternatives = lines.reduce<string[]>((result, inputType) => {
if (inputType.type === 'String') {
result.push(this.wrapWithZodValidators('z.string()', field, inputType));
} else if (Transformer.useDecimalLib && inputType.type === 'Decimal') {
result.push(
this.wrapWithZodValidators('z.instanceof(Decimal)', field, inputType),
);
this.addSchemaImport('Decimal');
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be a questionable decision, but I couldn't find any better way to add an import { Decimal } from 'decimal.js' for all schemas with Decimal fields

} else if (
inputType.type === 'Int' ||
inputType.type === 'Float' ||
Expand Down Expand Up @@ -372,6 +382,8 @@ export default class Transformer {
)} } from '../${queryName}${modelName}.schema'`;
} else if (Transformer.enumNames.includes(name)) {
return `import { ${name}Schema } from '../enums/${name}.schema'`;
} else if (name === 'Decimal') {
return "import { Decimal } from 'decimal.js'";
} else {
return `import { ${name}ObjectSchema } from './${name}.schema'`;
}
Expand Down