Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | 1x 1x 1x 1x 1x 1x 1x 1x 142x 1x 1x 142x 328x 186x 284x 306x 306x 308x 214x 194x 21x 193x 214x 214x 191x 8x 9x 9x 9x 191x 214x 214x 191x 1x 1x 1x 1x 191x 214x 214x 191x 1x 1x 191x 214x 214x 191x 1x 1x 1x 1x 190x 214x 214x 191x 9x 9x 9x 9x 9x 9x 9x 8x 8x 8x 8x 8x 8x 190x 214x 214x 191x 5x 5x 5x 5x 4x 4x 4x 190x 214x 214x 190x 190x 214x 214x 192x 1x 1x 1x 1x 1x 1x 1x 191x 214x 214x 191x 1x 1x 1x 1x 190x 214x 214x 191x 1x 1x 1x 1x 1x 1x 190x 214x 214x 191x 1x 1x 1x 1x 190x 214x 214x 190x 190x 214x 214x 192x 1x 1x 1x 1x 1x 1x 1x 191x 214x 214x 194x 4x 1x 4x 4x 1x 4x 4x 1x 1x 1x 1x 4x 4x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 194x 214x 214x 191x 1x 1x 191x 214x 214x 191x 106x 106x 106x 106x 106x 191x 214x 214x 214x 214x 190x 214x 214x 308x 306x 328x 44x | import { isNil } from 'lodash-es';
import { useEffect } from 'react';
import { useIntl } from 'react-intl';
import { match, P } from 'ts-pattern';
import { z } from 'zod';
import { dayjs } from '@amalia/ext/dayjs';
const formatZodDateConstraint = (date: z.core.$ZodIssueTooBig['maximum'] | z.core.$ZodIssueTooSmall['minimum']) =>
dayjs(date as Date | number, typeof date === 'number' ? 'x' : undefined).format('YYYY-MM-DD');
export const useUpdateZodLocale = () => {
const { formatMessage, formatList } = useIntl();
useEffect(() => {
z.config({
localeError: (issue) =>
match<typeof issue, string>(issue)
.with({ code: 'invalid_type' }, (issue) =>
isNil(issue.input)
? formatMessage({ defaultMessage: 'This field is required.' })
: formatMessage({ defaultMessage: 'This field must be of type {type}.' }, { type: issue.expected }),
)
.with({ code: 'invalid_value' }, (issue) =>
isNil(issue.input)
? formatMessage({ defaultMessage: 'This field is required.' })
: formatMessage(
{ defaultMessage: 'This field must be one of the following values: {values}.' },
{ values: formatList(issue.values.map(String), { type: 'disjunction' }) },
),
)
.with({ code: P.union('too_big', 'too_small'), origin: 'string', exact: true }, (issue) =>
formatMessage(
{
defaultMessage: 'This field must be exactly {length, plural, one {# character} other {# characters}}.',
},
{ length: (issue.code === 'too_big' ? issue.maximum : issue.minimum) as number },
),
)
.with({ code: P.union('too_big', 'too_small'), origin: P.union('array', 'set'), exact: true }, (issue) =>
formatMessage(
{ defaultMessage: 'This field must have {length, plural, one {# item} other {# items}}.' },
{ length: (issue.code === 'too_big' ? issue.maximum : issue.minimum) as number },
),
)
.with({ code: 'too_small', origin: 'date' }, (issue) =>
issue.inclusive
? formatMessage(
{ defaultMessage: 'This field must be later than or on {min}.' },
{ min: formatZodDateConstraint(issue.minimum) },
)
: formatMessage(
{ defaultMessage: 'This field must be later than {min}.' },
{ min: formatZodDateConstraint(issue.minimum) },
),
)
.with({ code: 'too_small', origin: 'string' }, (issue) =>
issue.inclusive
? formatMessage(
{
defaultMessage:
'This field must be at least {min, plural, one {# character} other {# characters}}.',
},
{ min: issue.minimum as number },
)
: formatMessage(
{
defaultMessage:
'This field must be more than {min, plural, one {# character} other {# characters}}.',
},
{ min: issue.minimum as number },
),
)
.with({ code: 'too_small', origin: P.union('array', 'set') }, (issue) =>
issue.inclusive
? formatMessage(
{ defaultMessage: 'This field must have at least {min, plural, one {# item} other {# items}}.' },
{ min: issue.minimum as number },
)
: formatMessage(
{ defaultMessage: 'This field must have more than {min, plural, one {# item} other {# items}}.' },
{ min: issue.minimum as number },
),
)
.with({ code: 'too_small', origin: 'file' }, (issue) =>
issue.inclusive
? formatMessage(
{ defaultMessage: 'This field must at least {min, plural, one {# byte} other {# bytes}}.' },
{ min: issue.minimum as number },
)
: formatMessage(
{ defaultMessage: 'This field must be more than {min, plural, one {# byte} other {# bytes}}.' },
{ min: issue.minimum as number },
),
)
.with({ code: 'too_small' }, (issue) =>
issue.inclusive
? formatMessage(
{ defaultMessage: 'This field must be greater than or equal to {min, number}.' },
{ min: issue.minimum as number },
)
: formatMessage(
{ defaultMessage: 'This field must be greater than {min, number}.' },
{ min: issue.minimum as number },
),
)
.with({ code: 'too_big', origin: 'date' }, (issue) =>
issue.inclusive
? formatMessage(
{ defaultMessage: 'This field must be earlier than or on {max}.' },
{ max: formatZodDateConstraint(issue.maximum) },
)
: formatMessage(
{ defaultMessage: 'This field must be earlier than {max}.' },
{ max: formatZodDateConstraint(issue.maximum) },
),
)
.with({ code: 'too_big', origin: 'string' }, (issue) =>
issue.inclusive
? formatMessage(
{
defaultMessage: 'This field must be at most {max, plural, one {# character} other {# characters}}.',
},
{ max: issue.maximum as number },
)
: formatMessage(
{
defaultMessage:
'This field must be less than {max, plural, one {# character} other {# characters}}.',
},
{ max: issue.maximum as number },
),
)
.with({ code: 'too_big', origin: P.union('array', 'set') }, (issue) =>
issue.inclusive
? formatMessage(
{ defaultMessage: 'This field must have at most {max, plural, one {# item} other {# items}}.' },
{ max: issue.maximum as number },
)
: formatMessage(
{ defaultMessage: 'This field must have less than {max, plural, one {# item} other {# items}}.' },
{ max: issue.maximum as number },
),
)
.with({ code: 'too_big', origin: 'file' }, (issue) =>
issue.inclusive
? formatMessage(
{ defaultMessage: 'This field must be at most {max, plural, one {# byte} other {# bytes}}.' },
{ max: issue.maximum as number },
)
: formatMessage(
{ defaultMessage: 'This field must be less than {max, plural, one {# byte} other {# bytes}}.' },
{ max: issue.maximum as number },
),
)
.with({ code: 'too_big' }, (issue) =>
issue.inclusive
? formatMessage(
{ defaultMessage: 'This field must be less than or equal to {max, number}.' },
{ max: issue.maximum as number },
)
: formatMessage(
{ defaultMessage: 'This field must be less than {max, number}.' },
{ max: issue.maximum as number },
),
)
.with({ code: 'invalid_format' }, (issue) =>
match(issue as z.core.$ZodStringFormatIssues)
.with({ format: 'starts_with' }, (issue) =>
formatMessage({ defaultMessage: 'This field must start with “{prefix}”.' }, { prefix: issue.prefix }),
)
.with({ format: 'ends_with' }, (issue) =>
formatMessage({ defaultMessage: 'This field must end with “{suffix}”.' }, { suffix: issue.suffix }),
)
.with({ format: 'includes' }, (issue) =>
formatMessage(
{ defaultMessage: 'This field must include “{includes}”.' },
{ includes: issue.includes },
),
)
.with({ format: 'regex' }, (issue) =>
formatMessage(
{ defaultMessage: 'This field must match the following: “{regex}”.' },
{ regex: issue.pattern },
),
)
.with({ format: 'email' }, () => formatMessage({ defaultMessage: 'This field must be a valid email.' }))
.with({ format: 'url' }, () => formatMessage({ defaultMessage: 'This field must be a valid URL.' }))
.with({ format: 'emoji' }, () => formatMessage({ defaultMessage: 'This field must be a valid emoji.' }))
.with({ format: 'uuid' }, () => formatMessage({ defaultMessage: 'This field must be a valid UUID.' }))
.with({ format: 'guid' }, () => formatMessage({ defaultMessage: 'This field must be a valid GUID.' }))
.with({ format: 'nanoid' }, () =>
formatMessage({ defaultMessage: 'This field must be a valid Nano ID.' }),
)
.with({ format: 'cuid' }, () => formatMessage({ defaultMessage: 'This field must be a valid CUID.' }))
.with({ format: 'cuid2' }, () => formatMessage({ defaultMessage: 'This field must be a valid CUID2.' }))
.with({ format: 'ulid' }, () => formatMessage({ defaultMessage: 'This field must be a valid ULID.' }))
.with({ format: 'xid' }, () => formatMessage({ defaultMessage: 'This field must be a valid XID.' }))
.with({ format: 'ksuid' }, () => formatMessage({ defaultMessage: 'This field must be a valid KSUID.' }))
.with({ format: P.union('datetime', 'date', 'time') }, () =>
formatMessage({ defaultMessage: 'This field must be a valid date.' }),
)
.with({ format: 'duration' }, () =>
formatMessage({ defaultMessage: 'This field must be a valid duration.' }),
)
.with({ format: 'ipv4' }, () =>
formatMessage({ defaultMessage: 'This field must be a valid IPv4 address.' }),
)
.with({ format: 'ipv6' }, () =>
formatMessage({ defaultMessage: 'This field must be a valid IPv6 address.' }),
)
.with({ format: 'cidrv4' }, () =>
formatMessage({ defaultMessage: 'This field must be a valid IPv4 CIDR block.' }),
)
.with({ format: 'cidrv6' }, () =>
formatMessage({ defaultMessage: 'This field must be a valid IPv6 CIDR block.' }),
)
.with({ format: 'jwt' }, () => formatMessage({ defaultMessage: 'This field must be a valid JWT.' }))
.with({ format: 'base64' }, () =>
formatMessage({ defaultMessage: 'This field must be a valid base64 encoded string.' }),
)
.with({ format: 'base64url' }, () =>
formatMessage({ defaultMessage: 'This field must be a valid base64url encoded string.' }),
)
.with({ format: 'json_string' }, () =>
formatMessage({ defaultMessage: 'This field must be a valid JSON object.' }),
)
.with({ format: 'e164' }, () =>
formatMessage({ defaultMessage: 'This field must be a valid E.164 number.' }),
)
.with({ format: 'lowercase' }, () =>
formatMessage({ defaultMessage: 'This field must be a lowercase string.' }),
)
.with({ format: 'uppercase' }, () =>
formatMessage({ defaultMessage: 'This field must be an uppercase string.' }),
)
.exhaustive(),
)
.with({ code: 'not_multiple_of' }, (issue) =>
formatMessage(
{ defaultMessage: 'This field must be a multiple of {divisor, number}.' },
{ divisor: issue.divisor },
),
)
.with({ code: 'unrecognized_keys' }, (issue) =>
formatMessage(
{
defaultMessage:
'This field has {keysCount, plural, one {# unrecognized key} other {# unrecognized keys}}: {keys}.',
},
{ keysCount: issue.keys.length, keys: formatList(issue.keys, { type: 'conjunction' }) },
),
)
.with({ code: 'invalid_key' }, () => formatMessage({ defaultMessage: 'This field has an invalid key.' }))
.with({ code: 'invalid_union' }, () => formatMessage({ defaultMessage: 'This field is invalid.' }))
.with({ code: 'invalid_element' }, () =>
formatMessage({ defaultMessage: 'This field has an invalid element.' }),
)
.with({ code: 'custom' }, () => formatMessage({ defaultMessage: 'This field is invalid.' }))
.exhaustive(),
});
}, [formatList, formatMessage]);
};
|