-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschemas.ts
More file actions
24 lines (21 loc) · 848 Bytes
/
schemas.ts
File metadata and controls
24 lines (21 loc) · 848 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { z } from "zod";
// https://github.com/knex/knex/issues/6283
const SQLITE_DT_RE = /^\d{4}-\d{2}-\d{2}[ T]\d{2}:\d{2}:\d{2}(?:\.\d{1,6})?$/;
const SQLiteDatetime = z.stringFormat("YYYY-MM-DD HH:MM:SS", SQLITE_DT_RE);
export const SQLiteDateTimeToISODateTime = z.codec(
z.union([SQLiteDatetime, z.iso.datetime()]),
z.iso.datetime(),
{
decode: (sqlite_datetime: string) => {
if (z.iso.datetime().safeParse(sqlite_datetime).success) {
return sqlite_datetime;
}
const withT = sqlite_datetime.replace(" ", "T");
const hasTz = /([zZ]|[+-]\d{2}:\d{2})$/.test(withT);
return hasTz ? withT : `${withT}Z`;
},
encode: (iso_datetime: string) => {
return iso_datetime.replace(/Z$/i, "").replace("T", " ");
},
},
);