-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(zod): clean up optionality of generated schema fields (#1139)
- Loading branch information
Showing
3 changed files
with
171 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,6 +45,9 @@ describe('Zod plugin tests', () => { | |
password String @omit | ||
role Role @default(USER) | ||
posts Post[] | ||
age Int? | ||
@@validate(length(password, 6, 20)) | ||
} | ||
model Post { | ||
|
@@ -61,8 +64,14 @@ describe('Zod plugin tests', () => { | |
{ addPrelude: false, pushDb: false } | ||
); | ||
const schemas = zodSchemas.models; | ||
expect(schemas.UserScalarSchema).toBeTruthy(); | ||
expect(schemas.UserWithoutRefineSchema).toBeTruthy(); | ||
expect(schemas.UserSchema).toBeTruthy(); | ||
expect(schemas.UserCreateScalarSchema).toBeTruthy(); | ||
expect(schemas.UserCreateWithoutRefineSchema).toBeTruthy(); | ||
expect(schemas.UserCreateSchema).toBeTruthy(); | ||
expect(schemas.UserUpdateScalarSchema).toBeTruthy(); | ||
expect(schemas.UserUpdateWithoutRefineSchema).toBeTruthy(); | ||
expect(schemas.UserUpdateSchema).toBeTruthy(); | ||
expect(schemas.UserPrismaCreateSchema).toBeTruthy(); | ||
expect(schemas.UserPrismaUpdateSchema).toBeTruthy(); | ||
|
@@ -75,6 +84,16 @@ describe('Zod plugin tests', () => { | |
expect( | ||
schemas.UserCreateSchema.safeParse({ email: '[email protected]', password: 'abc123' }).success | ||
).toBeTruthy(); | ||
expect( | ||
schemas.UserCreateSchema.safeParse({ email: '[email protected]', role: 'ADMIN', password: 'abc' }).success | ||
).toBeFalsy(); | ||
expect( | ||
schemas.UserCreateWithoutRefineSchema.safeParse({ | ||
email: '[email protected]', | ||
role: 'ADMIN', | ||
password: 'abc', | ||
}).success | ||
).toBeTruthy(); | ||
expect( | ||
schemas.UserCreateSchema.safeParse({ email: '[email protected]', role: 'ADMIN', password: 'abc123' }).success | ||
).toBeTruthy(); | ||
|
@@ -90,6 +109,8 @@ describe('Zod plugin tests', () => { | |
expect(schemas.UserUpdateSchema.safeParse({}).success).toBeTruthy(); | ||
expect(schemas.UserUpdateSchema.safeParse({ email: '[email protected]' }).success).toBeFalsy(); | ||
expect(schemas.UserUpdateSchema.safeParse({ email: '[email protected]' }).success).toBeTruthy(); | ||
expect(schemas.UserUpdateSchema.safeParse({ password: 'pas' }).success).toBeFalsy(); | ||
expect(schemas.UserUpdateWithoutRefineSchema.safeParse({ password: 'pas' }).success).toBeTruthy(); | ||
expect(schemas.UserUpdateSchema.safeParse({ password: 'password456' }).success).toBeTruthy(); | ||
|
||
// update unchecked | ||
|
@@ -98,7 +119,25 @@ describe('Zod plugin tests', () => { | |
).toBeTruthy(); | ||
|
||
// model schema | ||
expect(schemas.UserSchema.safeParse({ email: '[email protected]', role: 'ADMIN' }).success).toBeTruthy(); | ||
|
||
// missing fields | ||
expect( | ||
schemas.UserSchema.safeParse({ | ||
id: 1, | ||
email: '[email protected]', | ||
}).success | ||
).toBeFalsy(); | ||
|
||
expect( | ||
schemas.UserSchema.safeParse({ | ||
id: 1, | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
email: '[email protected]', | ||
role: 'ADMIN', | ||
}).success | ||
).toBeTruthy(); | ||
|
||
// without omitted field | ||
expect( | ||
schemas.UserSchema.safeParse({ | ||
|
@@ -109,6 +148,19 @@ describe('Zod plugin tests', () => { | |
updatedAt: new Date(), | ||
}).success | ||
).toBeTruthy(); | ||
|
||
// with optional field | ||
expect( | ||
schemas.UserSchema.safeParse({ | ||
id: 1, | ||
email: '[email protected]', | ||
role: 'ADMIN', | ||
createdAt: new Date(), | ||
updatedAt: new Date(), | ||
age: 18, | ||
}).success | ||
).toBeTruthy(); | ||
|
||
// with omitted field | ||
const withPwd = schemas.UserSchema.safeParse({ | ||
id: 1, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters