feat: create languageSchema

This commit is contained in:
P. Reis 2024-09-25 16:51:23 -03:00
parent dac11a9055
commit 92aaca0d91

View file

@ -1,3 +1,4 @@
import ISO6391 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. */
@ -40,12 +41,24 @@ 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().max(2).transform((val, ctx) => {
if (!ISO6391.validate(val)) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: 'Not a valid language in ISO-639-1 format',
});
return z.NEVER;
}
return val;
});
export { export {
booleanParamSchema, booleanParamSchema,
decode64Schema, decode64Schema,
fileSchema, fileSchema,
filteredArray, filteredArray,
hashtagSchema, hashtagSchema,
languageSchema,
percentageSchema, percentageSchema,
safeUrlSchema, safeUrlSchema,
}; };