Don't parse number arrays as date
This commit is contained in:
parent
6c36837d11
commit
ba10756119
2 changed files with 46 additions and 1 deletions
|
@ -106,7 +106,7 @@ function bindingTypeCoerce(bindings: any[]) {
|
||||||
}
|
}
|
||||||
// if not a number, see if it is a date - important to do in this order as any
|
// if not a number, see if it is a date - important to do in this order as any
|
||||||
// integer will be considered a valid date
|
// integer will be considered a valid date
|
||||||
else if (/^\d/.test(binding) && dayjs(binding).isValid()) {
|
else if (/^\d/.test(binding) && dayjs(binding).isValid() && !binding.includes(",")) {
|
||||||
bindings[i] = dayjs(binding).toDate()
|
bindings[i] = dayjs(binding).toDate()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,4 +73,49 @@ describe("MySQL Integration", () => {
|
||||||
expect(response).toEqual([{ deleted: true }])
|
expect(response).toEqual([{ deleted: true }])
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("binding type coerce", () => {
|
||||||
|
it("ignores non-string types ", async () => {
|
||||||
|
const sql = "select * from users;"
|
||||||
|
const date = new Date()
|
||||||
|
await config.integration.read({
|
||||||
|
sql,
|
||||||
|
bindings: [11, date, ["a", "b", "c"], { id: 1}]
|
||||||
|
})
|
||||||
|
expect(config.integration.client.query).toHaveBeenCalledWith(sql, [11, date, ["a", "b", "c"], { id: 1}])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("parses strings matching a number regex", async () => {
|
||||||
|
const sql = "select * from users;"
|
||||||
|
await config.integration.read({
|
||||||
|
sql,
|
||||||
|
bindings: ["101", "3.14"]
|
||||||
|
})
|
||||||
|
expect(config.integration.client.query).toHaveBeenCalledWith(sql, [101, 3.14])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("parses strings matching a valid date format", async () => {
|
||||||
|
const sql = "select * from users;"
|
||||||
|
await config.integration.read({
|
||||||
|
sql,
|
||||||
|
bindings: ["2001-10-30", "2010-09-01T13:30:59.123Z", "2021-02-05 12:01 PM"]
|
||||||
|
})
|
||||||
|
expect(config.integration.client.query).toHaveBeenCalledWith(sql, [
|
||||||
|
new Date("2001-10-30T00:00:00.000Z"),
|
||||||
|
new Date("2010-09-01T13:30:59.123Z"),
|
||||||
|
new Date("2021-02-05T12:01:00.000Z")
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
it("does not parse string matching a valid array of numbers as date", async () => {
|
||||||
|
const sql = "select * from users;"
|
||||||
|
await config.integration.read({
|
||||||
|
sql,
|
||||||
|
bindings: ["1,2,2017"]
|
||||||
|
})
|
||||||
|
expect(config.integration.client.query).toHaveBeenCalledWith(sql, [
|
||||||
|
"1,2,2017"
|
||||||
|
])
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in a new issue