-
-
Notifications
You must be signed in to change notification settings - Fork 144
fix(orm): coerce ISO strings on DateTime input, with strictDateInput opt-in (#2631) #2632
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
Open
erwan-joly
wants to merge
4
commits into
zenstackhq:dev
Choose a base branch
from
erwan-joly:fix/2631-coerce-iso-strings-on-datetime-input
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+125
−1
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
08d8a8d
fix(orm): coerce ISO strings (datetime, date, time-only) on DateTime …
erwan-joly e8cc785
review fixes for #2632
erwan-joly 6956c7e
docs(orm): clarify coercedDateTimeSchema accepts Date-parseable strings
erwan-joly 73fe8c8
fix(orm): keep DateTime union shape for OpenAPI baseline
erwan-joly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import { createTestClient } from '@zenstackhq/testtools'; | ||
| import { afterEach, beforeEach, describe, expect, it } from 'vitest'; | ||
|
|
||
| // Regression for #2631: ZenStack 3.5+ replaced Prisma's permissive | ||
| // datetime input coercion with a strict zod union, breaking every caller | ||
| // that passed ISO strings to `DateTime` fields. The default behaviour now | ||
| // coerces strings; `ClientOptions.strictDateInput: true` opts back in to | ||
| // the strict semantics. | ||
| describe('Issue 2631 — strictDateInput option', () => { | ||
| const schema = ` | ||
| model Event { | ||
| id Int @id @default(autoincrement()) | ||
| label String | ||
| when DateTime | ||
| } | ||
| `; | ||
|
|
||
| describe('default (strictDateInput unset / false)', () => { | ||
| let db: any; | ||
|
|
||
| beforeEach(async () => { | ||
| db = await createTestClient(schema, { usePrismaPush: true, provider: 'sqlite' }); | ||
| }); | ||
| afterEach(async () => db?.$disconnect()); | ||
|
|
||
| it('accepts a Date object', async () => { | ||
| const e = await db.event.create({ data: { label: 'date', when: new Date('2024-01-15T10:30:00Z') } }); | ||
| expect(e.when).toBeInstanceOf(Date); | ||
| }); | ||
|
|
||
| it('accepts an ISO datetime string and coerces to Date', async () => { | ||
| const e = await db.event.create({ data: { label: 'iso', when: '2024-01-15T10:30:00.000Z' } }); | ||
| expect(e.when).toBeInstanceOf(Date); | ||
| }); | ||
|
|
||
| it('accepts an ISO date string and coerces to Date', async () => { | ||
| const e = await db.event.create({ data: { label: 'date-only', when: '2024-01-15' } }); | ||
| expect(e.when).toBeInstanceOf(Date); | ||
| }); | ||
|
|
||
| it('accepts a bare time-only string anchored to the Unix epoch', async () => { | ||
| const e = await db.event.create({ data: { label: 'time-only', when: '09:30:00' } }); | ||
| expect(e.when).toBeInstanceOf(Date); | ||
| expect((e.when as Date).getUTCHours()).toBe(9); | ||
| expect((e.when as Date).getUTCMinutes()).toBe(30); | ||
| }); | ||
|
|
||
| it('rejects a non-parseable string', async () => { | ||
| await expect(db.event.create({ data: { label: 'junk', when: 'not-a-date' as any } })).rejects.toThrow(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('strictDateInput: true', () => { | ||
| let db: any; | ||
|
|
||
| beforeEach(async () => { | ||
| db = await createTestClient(schema, { usePrismaPush: true, provider: 'sqlite', strictDateInput: true }); | ||
| }); | ||
| afterEach(async () => db?.$disconnect()); | ||
|
|
||
| it('accepts a Date object', async () => { | ||
| const e = await db.event.create({ data: { label: 'date', when: new Date('2024-01-15T10:30:00Z') } }); | ||
| expect(e.when).toBeInstanceOf(Date); | ||
| }); | ||
|
|
||
| it('rejects an ISO datetime string', async () => { | ||
| await expect(db.event.create({ data: { label: 'iso', when: '2024-01-15T10:30:00.000Z' as any } })).rejects.toThrow(); | ||
| }); | ||
|
|
||
| it('rejects an ISO date string', async () => { | ||
| await expect(db.event.create({ data: { label: 'date-only', when: '2024-01-15' as any } })).rejects.toThrow(); | ||
| }); | ||
|
|
||
| it('rejects a bare time-only string', async () => { | ||
| await expect(db.event.create({ data: { label: 'time-only', when: '09:30:00' as any } })).rejects.toThrow(); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.