Skip to content

Commit

Permalink
fix in docs + refinement on whereUniqueInput
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishoermann committed Feb 3, 2023
1 parent a2f23d7 commit 6c58b32
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/generator/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zod-prisma-types",
"version": "2.0.6",
"version": "2.0.7",
"description": "Generates zod schemas from Prisma models with advanced validation",
"author": "Chris Hörmann",
"license": "MIT",
Expand Down
6 changes: 6 additions & 0 deletions packages/generator/src/classes/extendedDMMFInputType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class ExtendedDMMFInputType
readonly isDecimalField: boolean;
readonly omitFields: string[] = [];
readonly imports: Set<string>;
readonly isWhereUniqueInput?: boolean;

constructor(
readonly generatorConfig: GeneratorConfig,
Expand All @@ -48,6 +49,7 @@ export class ExtendedDMMFInputType
this.isDecimalField = this._setIsDecimalField();
this.omitFields = this._setOmitFields();
this.imports = this._setImports();
this.isWhereUniqueInput = this._setIsWhereUniqueInput();
}

/**
Expand Down Expand Up @@ -90,6 +92,10 @@ export class ExtendedDMMFInputType
});
}

private _setIsWhereUniqueInput() {
return /WhereUniqueInput$/.test(this.name);
}

private _fieldIsPrismaFunctionType() {
return this.name.match(PRISMA_FUNCTION_TYPES_WITH_VALIDATORS);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,23 @@ export const writeInputObjectType = (
writer.newLine();
});
})
.write(`).strict();`);
.write(`).strict()`);

if (inputType.isWhereUniqueInput && inputType.fields.length > 1) {
writer.write(`.refine((data) => `);
inputType.fields.forEach((field, idx) => {
const writeComma = idx !== inputType.fields.length - 1;
writer.write(`!!data.${field?.name}`);
if (writeComma) {
writer.write(` || `);
}
});
writer.write(
`, { message: 'At least one field must be provided @ ${inputType.name}' })`,
);
}

writer.write(`;`);

if (useMultipleFiles && !getSingleFileContent) {
writer.blankLine().writeLine(`export default ${inputType.name}Schema;`);
Expand Down
22 changes: 22 additions & 0 deletions packages/usage/tests/wereUniqueInput.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ZodError } from 'zod';
import { UserWhereUniqueInputSchema } from '../prisma/generated/zod';

it('should accept the first of the two input values', () => {
const paresd = UserWhereUniqueInputSchema.parse({ id: 'idstring' });
expect(paresd).toEqual({ id: 'idstring' });
});

it('should accept the second of the two input values', () => {
const paresd = UserWhereUniqueInputSchema.parse({ email: 'email@mail.com' });
expect(paresd).toEqual({ email: 'email@mail.com' });
});

it('should throw an error when no input value is provided', () => {
try {
UserWhereUniqueInputSchema.parse({
email: undefined,
});
} catch (error) {
expect(error).toBeInstanceOf(ZodError);
}
});

0 comments on commit 6c58b32

Please sign in to comment.