validation.ts86 lines, 2.6 KB
| 1 | import Joi from "joi"; |
| 2 | |
| 3 | export const createTagSchema = Joi.object({ |
| 4 | name: Joi.string() |
| 5 | .min(1) |
| 6 | .max(50) |
| 7 | .required() |
| 8 | .pattern(/^[a-zA-Z0-9\s\-_]+$/) |
| 9 | .messages({ |
| 10 | "string.min": "Tag name must be at least 1 character long", |
| 11 | "string.max": "Tag name must not exceed 50 characters", |
| 12 | "string.pattern.base": |
| 13 | "Tag name can only contain letters, numbers, spaces, hyphens, and underscores", |
| 14 | "any.required": "Tag name is required", |
| 15 | }), |
| 16 | color: Joi.string() |
| 17 | .pattern(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/) |
| 18 | .optional() |
| 19 | .allow(null, "") |
| 20 | .messages({ |
| 21 | "string.pattern.base": |
| 22 | "Color must be a valid hex color format (e.g., #FF5733 or #F73)", |
| 23 | }), |
| 24 | }); |
| 25 | |
| 26 | export const updateTagSchema = Joi.object({ |
| 27 | name: Joi.string() |
| 28 | .min(1) |
| 29 | .max(50) |
| 30 | .pattern(/^[a-zA-Z0-9\s\-_]+$/) |
| 31 | .optional() |
| 32 | .messages({ |
| 33 | "string.min": "Tag name must be at least 1 character long", |
| 34 | "string.max": "Tag name must not exceed 50 characters", |
| 35 | "string.pattern.base": |
| 36 | "Tag name can only contain letters, numbers, spaces, hyphens, and underscores", |
| 37 | }), |
| 38 | color: Joi.string() |
| 39 | .pattern(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/) |
| 40 | .optional() |
| 41 | .allow(null, "") |
| 42 | .messages({ |
| 43 | "string.pattern.base": |
| 44 | "Color must be a valid hex color format (e.g., #FF5733 or #F73)", |
| 45 | }), |
| 46 | }); |
| 47 | |
| 48 | export const getTagsByUserSchema = Joi.object({ |
| 49 | page: Joi.number().integer().min(1).optional().default(1).messages({ |
| 50 | "number.base": "Page must be a number", |
| 51 | "number.integer": "Page must be an integer", |
| 52 | "number.min": "Page must be at least 1", |
| 53 | }), |
| 54 | limit: Joi.number() |
| 55 | .integer() |
| 56 | .min(1) |
| 57 | .max(100) |
| 58 | .optional() |
| 59 | .default(50) |
| 60 | .messages({ |
| 61 | "number.base": "Limit must be a number", |
| 62 | "number.integer": "Limit must be an integer", |
| 63 | "number.min": "Limit must be at least 1", |
| 64 | "number.max": "Limit must not exceed 100", |
| 65 | }), |
| 66 | search: Joi.string().min(1).max(100).optional().messages({ |
| 67 | "string.min": "Search term must be at least 1 character long", |
| 68 | "string.max": "Search term must not exceed 100 characters", |
| 69 | }), |
| 70 | }); |
| 71 | |
| 72 | export const validateTagsSchema = Joi.object({ |
| 73 | tagIds: Joi.array().items(Joi.string().uuid()).min(1).required().messages({ |
| 74 | "array.base": "Tag IDs must be an array", |
| 75 | "array.min": "At least one tag ID is required", |
| 76 | "string.uuid": "Each tag ID must be a valid UUID", |
| 77 | "any.required": "Tag IDs are required", |
| 78 | }), |
| 79 | }); |
| 80 | |
| 81 | export const tagIdParamSchema = Joi.object({ |
| 82 | tagId: Joi.string().uuid().required().messages({ |
| 83 | "string.uuid": "Tag ID must be a valid UUID", |
| 84 | "any.required": "Tag ID is required", |
| 85 | }), |
| 86 | }); |