feat: languageSchema converts value to lowercase and returns type LanguageCode

This commit is contained in:
P. Reis 2024-10-04 23:37:01 -03:00
parent 2ca421bef2
commit f3b7f91a07

View file

@ -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 {