Fix date validation regex

This commit is contained in:
Mel O'Hagan 2023-09-11 21:26:05 +01:00
parent cb115f4f11
commit 7147752b9c
2 changed files with 13 additions and 1 deletions

View File

@ -657,4 +657,16 @@ describe("SQL query builder", () => {
sql: `select * from (select top (@p0) * from [test] order by [test].[id] asc) as [test]`,
})
})
it("should not parse JSON string as Date", () => {
let query = new Sql(SqlClient.POSTGRES, limit)._query(
generateCreateJson(TABLE_NAME, {
name: '{ "created_at":"2023-09-09T03:21:06.024Z" }',
})
)
expect(query).toEqual({
bindings: ['{ "created_at":"2023-09-09T03:21:06.024Z" }'],
sql: `insert into \"test\" (\"name\") values ($1) returning *`,
})
})
})

View File

@ -182,7 +182,7 @@ export function getSqlQuery(query: SqlQuery | string): SqlQuery {
export const isSQL = helpers.isSQL
export function isIsoDateString(str: string) {
if (!/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z/.test(str)) {
if (!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/.test(str.trim())) {
return false
}
let d = new Date(str)