mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 03:19:46 +00:00
feat: create localeSchema
This commit is contained in:
parent
49d815826c
commit
c1c25d7c08
1 changed files with 14 additions and 1 deletions
|
|
@ -42,7 +42,7 @@ const fileSchema = z.custom<File>((value) => value instanceof File);
|
|||
const percentageSchema = z.coerce.number().int().gte(1).lte(100);
|
||||
|
||||
const languageSchema = z.string().transform<LanguageCode>((val, ctx) => {
|
||||
val = (val.toLowerCase()).split('-')[0]; // pt-BR -> pt
|
||||
val = val.toLowerCase();
|
||||
if (!ISO6391.validate(val)) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
|
|
@ -53,6 +53,18 @@ const languageSchema = z.string().transform<LanguageCode>((val, ctx) => {
|
|||
return val as LanguageCode;
|
||||
});
|
||||
|
||||
const localeSchema = z.string().transform<Intl.Locale>((val, ctx) => {
|
||||
try {
|
||||
return new Intl.Locale(val);
|
||||
} catch {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: 'Invalid locale',
|
||||
});
|
||||
return z.NEVER;
|
||||
}
|
||||
});
|
||||
|
||||
export {
|
||||
booleanParamSchema,
|
||||
decode64Schema,
|
||||
|
|
@ -60,6 +72,7 @@ export {
|
|||
filteredArray,
|
||||
hashtagSchema,
|
||||
languageSchema,
|
||||
localeSchema,
|
||||
percentageSchema,
|
||||
safeUrlSchema,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue