Skip to content

Commit

Permalink
Update Duration to better support ISO 8601-2
Browse files Browse the repository at this point in the history
Use a better regex to support negatives and decimal fractions to the smallest value.
Add a lot of valid and invalid inputs to the test.
  • Loading branch information
mastermatt authored and colinhacks committed Apr 17, 2024
1 parent d8e4799 commit c7811f2
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 10 deletions.
49 changes: 45 additions & 4 deletions deno/lib/__tests__/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,11 +675,52 @@ test("duration", () => {
const duration = z.string().duration();
expect(duration.isDuration).toEqual(true);

duration.parse("P3Y6M4DT12H30M5S");
const validDurations = [
"P3Y6M4DT12H30M5S",
"P2Y9M3DT12H31M8.001S",
"+P3Y6M4DT12H30M5S",
"-PT0.001S",
"+PT0.001S",
"PT0,001S",
"PT12H30M5S",
"-P2M1D",
"P-2M-1D",
"-P5DT10H",
"P-5DT-10H",
"P1Y",
"P2MT30M",
"PT6H",
"P5W",
"P0.5Y",
"P0,5Y",
"P42YT7.004M",
];

const invalidDurations = [
"foo bar",
"",
" ",
"P",
"T1H",
"P0.5Y1D",
"P0,5Y6M",
"P1YT",
];

for (const val of validDurations) {
const result = duration.safeParse(val);
if (!result.success) {
throw Error(`Valid duration could not be parsed: ${val}`);
}
}

for (const val of invalidDurations) {
const result = duration.safeParse(val);

if (result.success) {
throw Error(`Invalid duration was successful parsed: ${val}`);
}

const result = duration.safeParse("invalidDuration");
expect(result.success).toEqual(false);
if (!result.success) {
expect(result.error.issues[0].message).toEqual("Invalid duration");
}
>>>>>>> 29773e8 (feat: Add support for ISO-8601 Durations)
Expand Down
2 changes: 1 addition & 1 deletion deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ const uuidRegex =
const nanoidRegex = /^[a-z0-9_-]{21}$/i;
=======
const durationRegex =
/^P(?!$)(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?$/;
/^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;

>>>>>>> 29773e8 (feat: Add support for ISO-8601 Durations)
// from https://stackoverflow.com/a/46181/1550155
Expand Down
49 changes: 45 additions & 4 deletions src/__tests__/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,11 +674,52 @@ test("duration", () => {
const duration = z.string().duration();
expect(duration.isDuration).toEqual(true);

duration.parse("P3Y6M4DT12H30M5S");
const validDurations = [
"P3Y6M4DT12H30M5S",
"P2Y9M3DT12H31M8.001S",
"+P3Y6M4DT12H30M5S",
"-PT0.001S",
"+PT0.001S",
"PT0,001S",
"PT12H30M5S",
"-P2M1D",
"P-2M-1D",
"-P5DT10H",
"P-5DT-10H",
"P1Y",
"P2MT30M",
"PT6H",
"P5W",
"P0.5Y",
"P0,5Y",
"P42YT7.004M",
];

const invalidDurations = [
"foo bar",
"",
" ",
"P",
"T1H",
"P0.5Y1D",
"P0,5Y6M",
"P1YT",
];

for (const val of validDurations) {
const result = duration.safeParse(val);
if (!result.success) {
throw Error(`Valid duration could not be parsed: ${val}`);
}
}

for (const val of invalidDurations) {
const result = duration.safeParse(val);

if (result.success) {
throw Error(`Invalid duration was successful parsed: ${val}`);
}

const result = duration.safeParse("invalidDuration");
expect(result.success).toEqual(false);
if (!result.success) {
expect(result.error.issues[0].message).toEqual("Invalid duration");
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ const uuidRegex =
/^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
const nanoidRegex = /^[a-z0-9_-]{21}$/i;
const durationRegex =
/^P(?!$)(\d+Y)?(\d+M)?(\d+W)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+S)?)?$/;
/^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;

// from https://stackoverflow.com/a/46181/1550155
// old version: too slow, didn't support unicode
Expand Down

0 comments on commit c7811f2

Please sign in to comment.