mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
feat: languageSchema converts value to lowercase and returns type LanguageCode
This commit is contained in:
parent
2ca421bef2
commit
f3b7f91a07
1 changed files with 3 additions and 2 deletions
|
|
@ -1,4 +1,4 @@
|
||||||
import ISO6391 from 'iso-639-1';
|
import ISO6391, { LanguageCode } from 'iso-639-1';
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
|
|
||||||
/** Validates individual items in an array, dropping any that aren't valid. */
|
/** Validates individual items in an array, dropping any that aren't valid. */
|
||||||
|
|
@ -42,6 +42,7 @@ const fileSchema = z.custom<File>((value) => value instanceof File);
|
||||||
const percentageSchema = z.coerce.number().int().gte(1).lte(100);
|
const percentageSchema = z.coerce.number().int().gte(1).lte(100);
|
||||||
|
|
||||||
const languageSchema = z.string().transform((val, ctx) => {
|
const languageSchema = z.string().transform((val, ctx) => {
|
||||||
|
val = val.toLowerCase();
|
||||||
if (!ISO6391.validate(val)) {
|
if (!ISO6391.validate(val)) {
|
||||||
ctx.addIssue({
|
ctx.addIssue({
|
||||||
code: z.ZodIssueCode.custom,
|
code: z.ZodIssueCode.custom,
|
||||||
|
|
@ -49,7 +50,7 @@ const languageSchema = z.string().transform((val, ctx) => {
|
||||||
});
|
});
|
||||||
return z.NEVER;
|
return z.NEVER;
|
||||||
}
|
}
|
||||||
return val;
|
return val as LanguageCode;
|
||||||
});
|
});
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue