Skip to content

fix(@db.Date): accept plain date strings in DateTime @db.Date field filters#2572

Open
lsmith77 wants to merge 1 commit intozenstackhq:devfrom
lsmith77:plain-date
Open

fix(@db.Date): accept plain date strings in DateTime @db.Date field filters#2572
lsmith77 wants to merge 1 commit intozenstackhq:devfrom
lsmith77:plain-date

Conversation

@lsmith77
Copy link
Copy Markdown
Contributor

@lsmith77 lsmith77 commented Apr 9, 2026

  • all Date/Datetime fields now accept plain date strings ("2007-05-23") in equality and comparison filters
  • ISO datetime strings still work
  • Date objects still work
  • Invalid strings are still rejected

Summary by CodeRabbit

  • New Features

    • DateTime filters now accept plain YYYY-MM-DD strings (in addition to ISO datetimes and Date objects) for equality and range queries, including both date-only and regular DateTime fields.
  • Tests

    • Added end-to-end tests validating schema acceptance/rejection of plain date strings and correct query results.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 9, 2026

📝 Walkthrough

Walkthrough

Updated Zod DateTime value schema to accept ISO date strings (YYYY-MM-DD) in addition to ISO datetimes and Date objects; added E2E tests that validate schema parsing and runtime queries for @db.Date and non-@db.Date DateTime fields.

Changes

Cohort / File(s) Summary
DateTime Filter Schema
packages/orm/src/client/zod/factory.ts
Expanded the DateTime value union from z.union([z.iso.datetime(), z.date()]) to z.union([z.iso.date(), z.iso.datetime(), z.date()]) so plain YYYY-MM-DD strings are accepted; no other control flow or exported signatures changed.
E2E Tests for @db.Date
tests/e2e/orm/client-api/db-date-filter.test.ts
Added Vitest E2E tests that spin up a PostgreSQL-backed test client, assert client.$zod.makeFindManySchema('Event') accepts ISO date strings, ISO datetimes, and Date objects (and rejects invalid strings), and verify runtime findMany queries with equality and range operators return expected rows.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Poem

🐇 I nibbled through the schema grain,
Found YYYY‑MM‑DD could hop on the train.
ISO, Date, or a plain date string,
Filters now accept — the tests sing!
Tiny hops, tidy code, carrots for the brain. 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarizes the main change: accepting plain date strings in DateTime @db.Date field filters, which is the core fix addressed in the changeset.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
tests/e2e/orm/client-api/db-date-filter.test.ts (1)

30-33: Remove unnecessary writes in schema-only validation test.

This case only checks safeParse, so the two create calls are not needed and add E2E runtime overhead.

♻️ Optional cleanup
 it('accepts a plain date string ("YYYY-MM-DD") in an equality filter on a `@db.Date` field', async () => {
-    await client.event.create({ data: { name: 'Conference', eventDate: new Date('2007-05-23') } });
-    await client.event.create({ data: { name: 'Workshop', eventDate: new Date('2024-01-15') } });
-
     const filterSchema = client.$zod.makeFindManySchema('Event');
     const result = filterSchema.safeParse({ where: { eventDate: '2007-05-23' } });
     expect(result.success, `Expected plain date string to be accepted, got: ${JSON.stringify(result.error)}`).toBe(
         true,
     );
 });
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/e2e/orm/client-api/db-date-filter.test.ts` around lines 30 - 33, The
test "accepts a plain date string (\"YYYY-MM-DD\") in an equality filter on a
`@db.Date` field" currently performs two unnecessary writes via
client.event.create; remove the two await client.event.create(...) lines so the
test only performs schema-only validation (safeParse) to avoid E2E runtime
overhead—keep the rest of the test intact and ensure any references to those
created records are also removed.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@tests/e2e/orm/client-api/db-date-filter.test.ts`:
- Around line 30-33: The test "accepts a plain date string (\"YYYY-MM-DD\") in
an equality filter on a `@db.Date` field" currently performs two unnecessary
writes via client.event.create; remove the two await client.event.create(...)
lines so the test only performs schema-only validation (safeParse) to avoid E2E
runtime overhead—keep the rest of the test intact and ensure any references to
those created records are also removed.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 9e4bbe47-96a5-49d9-9aa8-a631eaa38b88

📥 Commits

Reviewing files that changed from the base of the PR and between 39a0a28 and e900bb6.

📒 Files selected for processing (2)
  • packages/orm/src/client/zod/factory.ts
  • tests/e2e/orm/client-api/db-date-filter.test.ts

@ymc9
Copy link
Copy Markdown
Member

ymc9 commented Apr 13, 2026

Hi @lsmith77 , thanks for working on this. Since the change is only related to filtering, I think we can unconditionally support it for DateTime field (regardless if it carries db.Date). This also helps decouples ORM runtime and the migration engine (db.xxx attributes are only used by migrations).

What do you think?

@lsmith77
Copy link
Copy Markdown
Contributor Author

I made the necessary changes.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@packages/orm/src/client/zod/factory.ts`:
- Line 855: The shared makeDateTimeValueSchema (used via
makeScalarSchema('DateTime')) currently includes z.iso.date(), which
unintentionally permits plain date strings everywhere; remove z.iso.date() from
the union in makeDateTimeValueSchema so the shared DateTime scalar only accepts
Date objects and ISO datetimes (keep z.date() and z.iso.datetime()), and instead
add z.iso.date() only at the field/filter-specific validation code that handles
`@db.Date` filters (i.e., where field-level or filter-level schemas are created),
so date-only strings are accepted only for `@db.Date` contexts.

In `@tests/e2e/orm/client-api/db-date-filter.test.ts`:
- Around line 76-82: The test "plain date string is accepted on a regular
DateTime field (no `@db.Date`)" contradicts the intended behavior; update the test
so the filter schema for Event (created via
client.$zod.makeFindManySchema('Event')) rejects a plain 'YYYY-MM-DD' string for
the createdAt DateTime field: call filterSchema.safeParse({ where: { createdAt:
'2007-05-23' } }) and assert result.success is false (or assert that parsing
fails/throws), ensuring the expectation aligns with the non-@db.Date rule.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 5e4dc56c-2518-44a2-b2ba-a904456d85c6

📥 Commits

Reviewing files that changed from the base of the PR and between 4234f76 and bd695df.

📒 Files selected for processing (2)
  • packages/orm/src/client/zod/factory.ts
  • tests/e2e/orm/client-api/db-date-filter.test.ts

@cache()
private makeDateTimeValueSchema(): ZodType {
const schema = z.union([z.iso.datetime(), z.date()]);
const schema = z.union([z.iso.date(), z.iso.datetime(), z.date()]);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Scope regression: this change accepts plain date strings globally, not only @db.Date filters.

makeDateTimeValueSchema() is shared via makeScalarSchema('DateTime'), so adding z.iso.date() expands acceptance beyond the intended filter-only @db.Date case (including regular DateTime and mutation input validation paths). Please gate date-only acceptance at the field/filter level instead of the shared scalar DateTime value schema.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/orm/src/client/zod/factory.ts` at line 855, The shared
makeDateTimeValueSchema (used via makeScalarSchema('DateTime')) currently
includes z.iso.date(), which unintentionally permits plain date strings
everywhere; remove z.iso.date() from the union in makeDateTimeValueSchema so the
shared DateTime scalar only accepts Date objects and ISO datetimes (keep
z.date() and z.iso.datetime()), and instead add z.iso.date() only at the
field/filter-specific validation code that handles `@db.Date` filters (i.e., where
field-level or filter-level schemas are created), so date-only strings are
accepted only for `@db.Date` contexts.

Comment on lines +76 to +82
it('plain date string is accepted on a regular DateTime field (no @db.Date)', async () => {
// Plain date strings are accepted for all DateTime fields, not just @db.Date ones,
// to decouple filter validation from migration-engine attributes.
const filterSchema = client.$zod.makeFindManySchema('Event');
const result = filterSchema.safeParse({ where: { createdAt: '2007-05-23' } });
expect(result.success).toBe(true);
});
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

This test expectation conflicts with the stated non-@db.Date behavior.

The PR objective says regular DateTime fields should still reject plain YYYY-MM-DD strings, but this test asserts acceptance. That locks in the broader behavior and masks regressions against the intended contract.

Suggested test correction
-    const result = filterSchema.safeParse({ where: { createdAt: '2007-05-23' } });
-    expect(result.success).toBe(true);
+    const result = filterSchema.safeParse({ where: { createdAt: '2007-05-23' } });
+    expect(result.success).toBe(false);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
it('plain date string is accepted on a regular DateTime field (no @db.Date)', async () => {
// Plain date strings are accepted for all DateTime fields, not just @db.Date ones,
// to decouple filter validation from migration-engine attributes.
const filterSchema = client.$zod.makeFindManySchema('Event');
const result = filterSchema.safeParse({ where: { createdAt: '2007-05-23' } });
expect(result.success).toBe(true);
});
it('plain date string is accepted on a regular DateTime field (no `@db.Date`)', async () => {
// Plain date strings are accepted for all DateTime fields, not just `@db.Date` ones,
// to decouple filter validation from migration-engine attributes.
const filterSchema = client.$zod.makeFindManySchema('Event');
const result = filterSchema.safeParse({ where: { createdAt: '2007-05-23' } });
expect(result.success).toBe(false);
});
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@tests/e2e/orm/client-api/db-date-filter.test.ts` around lines 76 - 82, The
test "plain date string is accepted on a regular DateTime field (no `@db.Date`)"
contradicts the intended behavior; update the test so the filter schema for
Event (created via client.$zod.makeFindManySchema('Event')) rejects a plain
'YYYY-MM-DD' string for the createdAt DateTime field: call
filterSchema.safeParse({ where: { createdAt: '2007-05-23' } }) and assert
result.success is false (or assert that parsing fails/throws), ensuring the
expectation aligns with the non-@db.Date rule.

@lsmith77
Copy link
Copy Markdown
Contributor Author

wait .. I need to do some more cleanups in the tests.

@lsmith77
Copy link
Copy Markdown
Contributor Author

ok no. I think all is well. CodeRabbit appears to be confused because of the change of scope of this PR. not sure how to tell CodeRabbit to deal with that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants