1- import { IntlShape } from '@formatjs/intl'
1+ import { FormatDateOptions , IntlShape } from '@formatjs/intl'
22import { z } from 'zod'
33import { 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} {
@@ -44,15 +47,45 @@ export function getDescriptorItem(issue: z.ZodIssueOptionalMessage): {
4447 issue . code === z . ZodIssueCode . too_big
4548 ) {
4649 if ( issue . exact ) {
50+ let value =
51+ issue . code === z . ZodIssueCode . too_small
52+ ? issue . minimum
53+ : issue . code === z . ZodIssueCode . too_big
54+ ? issue . maximum
55+ : '-'
56+
57+ if ( issue . type === 'date' ) {
58+ const date = new Date ( value )
59+
60+ if ( isNaN ( date . getTime ( ) ) ) {
61+ value = '-'
62+ } else {
63+ const dateOptions : FormatDateOptions = {
64+ day : '2-digit' ,
65+ month : '2-digit' ,
66+ year : 'numeric' ,
67+ }
68+
69+ const timeOptions : FormatDateOptions | null =
70+ date . getUTCHours ( ) === 0 && date . getUTCMinutes ( ) === 0
71+ ? null
72+ : {
73+ hour : '2-digit' ,
74+ minute : '2-digit' ,
75+ }
76+
77+ value = intl . formatDate ( value , {
78+ timeZone : 'UTC' ,
79+ ...dateOptions ,
80+ ...timeOptions ,
81+ } )
82+ }
83+ }
84+
4785 return {
4886 key : `${ issue . type } .exact` ,
4987 values : {
50- value :
51- issue . code === z . ZodIssueCode . too_small
52- ? issue . minimum
53- : issue . code === z . ZodIssueCode . too_big
54- ? issue . maximum
55- : '?' ,
88+ value,
5689 } ,
5790 }
5891 }
0 commit comments