Skip to content

Commit 55781e8

Browse files
committed
format date for date error messages
1 parent c2fcc36 commit 55781e8

2 files changed

Lines changed: 53 additions & 32 deletions

File tree

src/translations/de.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"validation/date.exact": "Muss genau {value} sein",
88
"validation/date.invalid_date": "Ungültiges Datum",
99
"validation/date.too_big.exclusive": "Muss vor {value} sein",
10-
"validation/date.too_big.inclusive": "Muss höchstens {value} sein",
10+
"validation/date.too_big.inclusive": "Darf höchstens {value} sein",
1111
"validation/date.too_small.exclusive": "Muss nach {value} sein",
1212
"validation/date.too_small.inclusive": "Muss mindestens {value} sein",
1313
"validation/default": "Ungültige Eingabe",

src/zod/makeZodErrorMap.ts

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { IntlShape } from '@formatjs/intl'
1+
import { FormatDateOptions, IntlShape } from '@formatjs/intl'
22
import { z } from 'zod'
33
import { errorMessages } from './errorMessages'
44

@@ -7,7 +7,7 @@ export function makeZodErrorMap<T>(
77
issue: z.ZodIssueOptionalMessage,
88
intl: IntlShape<T>
99
) {
10-
const descriptorItem = getDescriptorItem(issue)
10+
const descriptorItem = getDescriptorItem<T>(issue, intl)
1111

1212
return descriptorItem.key in errorMessages
1313
? {
@@ -19,7 +19,10 @@ export function makeZodErrorMap<T>(
1919
: { message: intl.formatMessage(errorMessages['default']) }
2020
}
2121

22-
export function getDescriptorItem(issue: z.ZodIssueOptionalMessage): {
22+
export function getDescriptorItem<T>(
23+
issue: z.ZodIssueOptionalMessage,
24+
intl: IntlShape<T>
25+
): {
2326
key: string
2427
values?: Record<string, string | number>
2528
} {
@@ -39,43 +42,61 @@ export function getDescriptorItem(issue: z.ZodIssueOptionalMessage): {
3942
return { key: `date.${issue.code}` }
4043
}
4144

45+
if (
46+
issue.code === z.ZodIssueCode.too_small &&
47+
issue.type === 'string' &&
48+
issue.minimum === 1
49+
) {
50+
return { key: 'string.required' }
51+
}
52+
4253
if (
4354
issue.code === z.ZodIssueCode.too_small ||
4455
issue.code === z.ZodIssueCode.too_big
4556
) {
46-
if (issue.exact) {
47-
return {
48-
key: `${issue.type}.exact`,
49-
values: {
50-
value:
51-
issue.code === z.ZodIssueCode.too_small
52-
? issue.minimum
53-
: issue.code === z.ZodIssueCode.too_big
54-
? issue.maximum
55-
: '?',
56-
},
57-
}
58-
}
57+
let value =
58+
issue.code === z.ZodIssueCode.too_small
59+
? issue.minimum
60+
: issue.code === z.ZodIssueCode.too_big
61+
? issue.maximum
62+
: '-'
63+
64+
if (issue.type === 'date') {
65+
const date = new Date(value)
5966

60-
if (
61-
issue.code === z.ZodIssueCode.too_small &&
62-
issue.type === 'string' &&
63-
issue.minimum === 1
64-
) {
65-
return { key: 'string.required' }
67+
if (isNaN(date.getTime())) {
68+
value = '-'
69+
} else {
70+
const dateOptions: FormatDateOptions = {
71+
day: '2-digit',
72+
month: '2-digit',
73+
year: 'numeric',
74+
}
75+
76+
const timeOptions: FormatDateOptions | null =
77+
date.getUTCHours() === 0 && date.getUTCMinutes() === 0
78+
? null
79+
: {
80+
hour: '2-digit',
81+
minute: '2-digit',
82+
}
83+
84+
value = intl.formatDate(value, {
85+
timeZone: 'UTC',
86+
...dateOptions,
87+
...timeOptions,
88+
})
89+
}
6690
}
6791

6892
return {
69-
key: `${issue.type}.${issue.code}.${
70-
issue.inclusive ? 'inclusive' : 'exclusive'
71-
}`,
93+
key: issue.exact
94+
? `${issue.type}.exact`
95+
: `${issue.type}.${issue.code}.${
96+
issue.inclusive ? 'inclusive' : 'exclusive'
97+
}`,
7298
values: {
73-
value:
74-
issue.code === z.ZodIssueCode.too_small
75-
? issue.minimum
76-
: issue.code === z.ZodIssueCode.too_big
77-
? issue.maximum
78-
: '?',
99+
value,
79100
},
80101
}
81102
}

0 commit comments

Comments
 (0)