prisma icon indicating copy to clipboard operation
prisma copied to clipboard

`DateTime` doesn't support ISO string with offset

Open paulrostorp opened this issue 3 years ago • 5 comments

Bug description

When passing ISO8601 strings with offset information, prisma throws an error.

How to reproduce

With a Prisma schema:

model Task {
  startDate: DateTime @db.Timestampz
}

A Zulu ISO8601 string works:

prisma.task.create({
  data: {
    startDate: "2021-09-28T22:00Z"
  }
})

A ISO8601 string doesn't

prisma.task.create({
  data: {
    startDate: "2021-09-29T00:00+02:00"
  }
})

with the error:

Error: Argument startDate: Got invalid value '2021-09-29T00:00+02:00' on prisma.createTask. Provided String, expected DateTime or Null.

Expected behavior

No response

Prisma information

model Task {
  startDate: DateTime @db.Timestampz
}

Environment & setup

  • OS: mac os , m1 chip
  • Database: PostgreSQL v13
  • Node.js version: node 16

Prisma Version

v2.30.3

paulrostorp avatar Sep 29 '21 14:09 paulrostorp

The issue is still reproducible with the latest Prisma version.

Schema:

generator client {
  provider = "prisma-client-js"
  output   = "../node_modules/.prisma/client"
}

datasource db {
  provider = "postgresql"
  url      = env("TEST_POSTGRES_URI")
}

model Task {
  id        Int      @id @default(autoincrement())
  startDate DateTime @db.Timestamptz
}

Code:

import { PrismaClient } from '.prisma/client'

async function main() {
  const prisma = new PrismaClient()

  const result = await prisma.task.create({
    data: {
      startDate: '2021-09-29T00:00+02:00',
    },
  })

  console.log(result)
}

void main()

Result:

/Users/aqrln/prisma/prisma/reproductions/reproductions/gh-9516/node_modules/.prisma/client/runtime/index.js:28989
    const error2 = new PrismaClientValidationError(renderErrorStr(validationCallsite));
                   ^
PrismaClientValidationError: 
Invalid `prisma.task.create()` invocation in
/Users/aqrln/prisma/prisma/reproductions/reproductions/gh-9516/index.ts:6:36

  3 async function main() {
  4   const prisma = new PrismaClient()
  5 
→ 6   const result = await prisma.task.create({
        data: {
          startDate: '2021-09-29T00:00+02:00'
                     ~~~~~~~~~~~~~~~~~~~~~~~~
        }
      })

Argument startDate: Got invalid value '2021-09-29T00:00+02:00' on prisma.createOneTask. Provided String, expected DateTime.


    at Document.validate (/Users/aqrln/prisma/prisma/reproductions/reproductions/gh-9516/node_modules/.prisma/client/runtime/index.js:28989:20)
    at serializationFn (/Users/aqrln/prisma/prisma/reproductions/reproductions/gh-9516/node_modules/.prisma/client/runtime/index.js:31903:19)
    at runInChildSpan (/Users/aqrln/prisma/prisma/reproductions/reproductions/gh-9516/node_modules/.prisma/client/runtime/index.js:25102:12)
    at PrismaClient._executeRequest (/Users/aqrln/prisma/prisma/reproductions/reproductions/gh-9516/node_modules/.prisma/client/runtime/index.js:31910:31)
    at async PrismaClient._request (/Users/aqrln/prisma/prisma/reproductions/reproductions/gh-9516/node_modules/.prisma/client/runtime/index.js:31839:16)
    at async main (/Users/aqrln/prisma/prisma/reproductions/reproductions/gh-9516/index.ts:6:18) {
  clientVersion: '0.0.0'
}

The regular expression that decides if a string is a valid date somewhere in query.ts doesn't match an ISO string with offset, which needs to be fixed.

The workaround is to pass a Date instead of a string:

   const result = await prisma.task.create({
     data: {
-      startDate: '2021-09-29T00:00+02:00',
+      startDate: new Date('2021-09-29T00:00+02:00'),
     },
   })

aqrln avatar Oct 19 '22 16:10 aqrln

As of 2024 this issue is still here I want to store the simple date and currently passing the Iso format based string

DateFound DateTime @db.Date DateFound DateTime in both cases when I pass the string 1990-05-06 which's the standard iso date format I still the same error

nInvalid value for argument DateFound: premature end of input. Expected ISO-8601 DateTime.

Sameerthe-Big-O avatar Mar 22 '24 05:03 Sameerthe-Big-O

As of 2024 this issue is still here I want to store the simple date and currently passing the Iso format based string

DateFound DateTime @db.Date DateFound DateTime in both cases when I pass the string 1990-05-06 which's the standard iso date format I still the same error

nInvalid value for argument DateFound: premature end of input. Expected ISO-8601 DateTime.

Any news? I'm having the same issue 😞

Gerotha avatar May 07 '24 23:05 Gerotha

Same issue here

R1NEJ0 avatar May 26 '24 23:05 R1NEJ0

Same issue here

hidayatridwan avatar Jun 24 '24 00:06 hidayatridwan

I'm having the same issue as well.

ElBonds avatar Jul 04 '24 17:07 ElBonds