最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Conditionally required field on controller schema - Stack Overflow

programmeradmin3浏览0评论

I`m looking for a "clean way" to validate if a field of my schema should be required. Usually I know this before accessing the domain layer, but I have ran into a particular scenario.

export const createOfferSchema = {
  body: z
    .object({
      product_id: z.string(),
      cycle_id: z.string(),
      amount: z.coerce.number(),
      price: z.coerce.number(),
      description: z.string().optional(),
      expires_at: z
        .string()
        .regex(/^\d{2}-\d{2}-\d{4}$/, "Formato esperado: DD-MM-YYYY")
        .optional(),
    })
    .refine(notEmpty.validation, notEmpty.warning),
};

I`m currently checking if product PERISHABLE property is true inside the use-case and throwing an error if the expires_at field is not provided, but would prefer to keep the validation only on the controller. Any ideas on how to achieve this directly on the controller?

发布评论

评论列表(0)

  1. 暂无评论