Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix datetime format for RFC3339 compliance #82

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const fastFormats: DefinedFormats = {
compareTime
),
"date-time": fmtDef(
/^\d\d\d\d-[0-1]\d-[0-3]\dt(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i,
/^\d\d\d\d-((0[1-9])|(1[0-2]))-((0[1-9])|([1-2]\d)|(3[01]))[tT](?:(([0-1]\d)|(2[0-3])):[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:[zZ]|[+-](([0-1]\d)|(2[0-3])):[0-5]\d)$/,
compareDateTime
),
"iso-time": fmtDef(
Expand Down Expand Up @@ -197,7 +197,7 @@ function compareIsoTime(t1: string, t2: string): number | undefined {
return 0
}

const DATE_TIME_SEPARATOR = /t|\s/i
const DATE_TIME_SEPARATOR = /t/i
MarcoKorinth marked this conversation as resolved.
Show resolved Hide resolved
function getDateTime(strictTimeZone?: boolean): (str: string) => boolean {
const time = getTime(strictTimeZone)

Expand Down
5 changes: 5 additions & 0 deletions tests/extras/format.json
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,11 @@
"description": "a valid iso-date-time string (leap second)",
"data": "2016-12-31T23:59:60Z",
"valid": true
},
{
"description": "an invalid iso-date-time string (space separator)",
"data": "2016-12-31 23:59:60Z",
"valid": false
}
]
},
Expand Down
27 changes: 26 additions & 1 deletion tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe("addFormats options", () => {
})

test("should support validation mode", () => {
addFormats(ajv, {mode: "fast", formats: ["date", "time"]})
addFormats(ajv, {mode: "fast", formats: ["date", "time", "date-time"]})
const validateDate = ajv.compile({format: "date"})
expect(validateDate("2020-09-17")).toEqual(true)
expect(validateDate("2020-09-35")).toEqual(true)
Expand All @@ -35,6 +35,31 @@ describe("addFormats options", () => {
expect(validateTime("17:27:38Z")).toEqual(true)
expect(validateTime("25:27:38Z")).toEqual(true)
expect(validateTime("17:27")).toEqual(false)

const validateDatetime = ajv.compile({format: "date-time"})
expect(validateDatetime("2016-12-31T23:59:60Z")).toEqual(true)
expect(validateDatetime("2015-12-31t23:59:60Z")).toEqual(true)
expect(validateDatetime("2015-02-11t22:59:22Z")).toEqual(true)
expect(validateDatetime("2020-01-01T20:00:00.000Z")).toEqual(true)
expect(validateDatetime("2020-01-01T20:00:00.000Z")).toEqual(true)
expect(validateDatetime("2023-05-04T01:14:00+21:00")).toEqual(true)
expect(validateDatetime("2023-05-04T01:14:10+16:20")).toEqual(true)
expect(validateDatetime("2023-05-04T01:14:21+09:50")).toEqual(true)
expect(validateDatetime("2023-05-04T01:14:21-04:31")).toEqual(true)
expect(validateDatetime("2023-05-04T01:14:21-04:31")).toEqual(true)
MarcoKorinth marked this conversation as resolved.
Show resolved Hide resolved
expect(validateDatetime("2016-15-31T23:59:60Z")).toEqual(false)
expect(validateDatetime("2015-00-11t22:59:22+00:00")).toEqual(false)
expect(validateDatetime("2015-01-00T22:59:22+00:00")).toEqual(false)
expect(validateDatetime("2016-12-31 23:59:60Z")).toEqual(false)
MarcoKorinth marked this conversation as resolved.
Show resolved Hide resolved
expect(validateDatetime("2015-02-11t24:59:22Z")).toEqual(false)
expect(validateDatetime("2020-01-01 20:00:00.000")).toEqual(false)
expect(validateDatetime("2020-01-01 20:00:00.000Z")).toEqual(false)
expect(validateDatetime("2023-05-04\t01:14:00+21:00")).toEqual(false)
expect(validateDatetime("2023-05-04\r01:14:10+16:20")).toEqual(false)
expect(validateDatetime("2015-02-11t22:59:22+24:30")).toEqual(false)
expect(validateDatetime("2023-05-04\n01:14:21+09:50")).toEqual(false)
expect(validateDatetime("2023-05-04\n01:14:21-04:31")).toEqual(false)
expect(validateDatetime("2023-05-04t01:14:21-04:31:00")).toEqual(false)
})
})

Expand Down