arktype icon indicating copy to clipboard operation
arktype copied to clipboard

Unable to directly configure top-level intersection error message

Open ssalbdivad opened this issue 10 months ago • 1 comments

import { type } from "arktype";

const Thing = type("string.email > 0").configure({
  message: "Email is not valid",
});

// standard bulleted message- config seemingly not applied
const out = Thing("");

Playground: https://arktype.io/playground?code=import%2520%257B%2520type%2520%257D%2520from%2520%2522arktype%2522%250A%250Aconst%2520Thing%2520%253D%2520type%28%2522string.email%2520%253E%25200%2522%29.configure%28%257B%250A%2509message%253A%2520%2522Email%2520is%2520not%2520valid%2522%250A%257D%29%250A%250A%252F%252F%2520standard%2520bulleted%2520message-%2520config%2520seemingly%2520not%2520applied%250Aconst%2520out%2520%253D%2520Thing%28%2522%2522%29%250A

This doesn't work because an intersection error is built from a dynamic list of failures, so metadata from the node is never applied to the error.

Workaround:

Configure intersection errors globally or at a scope-level:

const { type } = scope(
  {},
  {
    intersection: {
      message: (ctx) => {
        if (
          ctx.flat.every(
            (e, i) =>
              i === ctx.flat.length - 1 || e.message === ctx.flat[i + 1].message
          )
        ) {
          return ctx.flat[0].message;
        }
        return ctx.problem;
      },
    },
  }
);

const Thing = type("string.email > 0").configure({
  message: "Email is not valid",
});

const out = Thing("");

// Email is not valid
console.log(out.summary);

Playground: https://arktype.io/playground?code=import%2520%257B%2520scope%2520%257D%2520from%2520%2522arktype%2522%250A%250Aconst%2520%257B%2520type%2520%257D%2520%253D%2520scope%28%250A%2509%257B%257D%252C%250A%2509%257B%250A%2509%2509intersection%253A%2520%257B%250A%2509%2509%2509message%253A%2520%28ctx%29%2520%253D%253E%2520%257B%250A%2509%2509%2509%2509if%2520%28%250A%2509%2509%2509%2509%2509ctx.flat.every%28%250A%2509%2509%2509%2509%2509%2509%28e%252C%2520i%29%2520%253D%253E%250A%2509%2509%2509%2509%2509%2509%2509i%2520%253D%253D%253D%2520ctx.flat.length%2520-%25201%2520%257C%257C%2520e.message%2520%253D%253D%253D%2520ctx.flat%255Bi%2520%252B%25201%255D.message%250A%2509%2509%2509%2509%2509%29%250A%2509%2509%2509%2509%29%2520%257B%250A%2509%2509%2509%2509%2509return%2520ctx.flat%255B0%255D.message%250A%2509%2509%2509%2509%257D%250A%2509%2509%2509%2509return%2520ctx.problem%250A%2509%2509%2509%257D%250A%2509%2509%257D%250A%2509%257D%250A%29%250A%250Aconst%2520Thing%2520%253D%2520type%28%2522string.email%2520%253E%25200%2522%29.configure%28%257B%250A%2509message%253A%2520%2522Email%2520is%2520not%2520valid%2522%250A%257D%29%250A%250Aconst%2520out%2520%253D%2520Thing%28%2522%2522%29%250A

ssalbdivad avatar Apr 09 '25 11:04 ssalbdivad

Related: https://github.com/arktypeio/arktype/issues/1479

robin-blanchard avatar Jul 16 '25 14:07 robin-blanchard