Validating Fields Based on Another Field Without Using Union

Hello, is there a simple approach to validating fields based on a value from another field? I have tried making a union of two schemas, but I can't modify the error message with annotations for that specific field, so I was wondering maybe there is a different way without using a union?

const staticListSchema = Schema.Struct({
  name: Schema.Trim.annotations({
    message: () => "This field is required",
  }).pipe(
    Schema.minLength(3, {
      message: () => "Must be minimum 3 characters",
    }),
  ),
  slug: Schema.NonEmptyString.annotations({
    message: () => "This field is required",
  }).pipe(
    Schema.filter((slug) =>
      new RegExp(/^\/(\S|\d)*$/gm).test(slug) ? true : "Invalid URL path",
    ),
  ),
  type: Schema.Literal(ListType.STATIC).annotations({
    message: () => ({message: "This field is required", override: true}),
  }),
});

const dynamicListSchema = Schema.Struct({
  name: Schema.Trim.annotations({
    message: () => "This field is required",
  }).pipe(
    Schema.minLength(3, {
      message: () => "Must be minimum 3 characters",
    }),
  ),
  slug: Schema.NonEmptyString.annotations({
    message: () => "This field is required",
  }).pipe(
    Schema.filter((slug) =>
      new RegExp(/^\/(\S|\d)*$/gm).test(slug) ? true : "Invalid URL path",
    ),
  ),
  type: Schema.Literal(ListType.DYNAMIC).annotations({
    message: () => ({message: "This field is required", override: true}),
  }),
  dynamicType: Schema.Enums(DynamicListType).annotations({
    message: () => "This field is required",
  }),
});

const storeListSchema = Schema.Union(
  staticListSchema,
  dynamicListSchema,
).annotations({
  message: (issue) => {
    console.dir(issue, { depth: Infinity });

    // return 'testiranje'
    return {message: "testiranje", override: true};
  },
});


The issue is when type is "" I get this message: Expected "STATIC" | "DYNAMIC", actual "" and I want to modify that message to say This field is required
Was this page helpful?