1
0
Fork 0
mirror of synced 2024-07-14 10:45:51 +12:00

Merge pull request #11743 from Budibase/budi-7476-postgres-sql-invalid-time-value-error-when-saving-json-as

Postgres sql invalid time value error when saving json as text
This commit is contained in:
Martin McKeaveney 2023-09-12 11:18:23 +01:00 committed by GitHub
commit 0c19bac1ec
3 changed files with 30 additions and 4 deletions

View file

@ -58,7 +58,7 @@ function parse(input: any) {
return null
}
if (isIsoDateString(input)) {
return new Date(input)
return new Date(input.trim())
}
return input
}

View file

@ -657,4 +657,29 @@ 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 *`,
})
})
it("should parse and trim valid string as Date", () => {
const dateObj = new Date("2023-09-09T03:21:06.024Z")
let query = new Sql(SqlClient.POSTGRES, limit)._query(
generateCreateJson(TABLE_NAME, {
name: " 2023-09-09T03:21:06.024Z ",
})
)
expect(query).toEqual({
bindings: [dateObj],
sql: `insert into \"test\" (\"name\") values ($1) returning *`,
})
})
})

View file

@ -182,11 +182,12 @@ 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)) {
const trimmedValue = str.trim()
if (!/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/.test(trimmedValue)) {
return false
}
let d = new Date(str)
return d.toISOString() === str
let d = new Date(trimmedValue)
return d.toISOString() === trimmedValue
}
/**