MCPcopy Create free account
hub / github.com/arktypeio/arktype / errorFromUnion

Function errorFromUnion

ark/schema/shared/errors.ts:678–710  ·  view source on GitHub ↗
(
	ctx: InternalTraversal,
	errors: readonly ArkError[],
	meta: ArkEnv.meta,
	data: unknown
)

Source from the content-addressed store, hash-verified

676
677/** adds a union's error from each branch's first */
678export const errorFromUnion = (
679 ctx: InternalTraversal,
680 errors: readonly ArkError[],
681 meta: ArkEnv.meta,
682 data: unknown
683): ArkError => {
684 const path = pathAt(ctx.path)
685 const flat = flattenUnion(errors)
686 // a description written for it is its text, as it is a standalone build's
687 const isDescribed =
688 meta.description !== undefined && meta.keyword === undefined
689 const atBases =
690 !isDescribed &&
691 meta.expected === undefined &&
692 meta.message === undefined &&
693 flat.length !== 0 &&
694 flat.every(e => failsAtBasis(e, path))
695 const error = stampError(
696 {
697 data,
698 path,
699 code: "union",
700 errors: atBases ? [] : flat,
701 // what it lists says what it expects, as a validator's, never its actual
702 meta:
703 atBases && meta.actual !== undefined ? { ...meta, actual: "" } : meta,
704 ...(isDescribed && { description: meta.description })
705 } as never,
706 ctx.config
707 )
708 if (atBases) basesByUnion.set(error, flat)
709 return ctx.addError(error)
710}
711
712// a union whose every branch fails at its basis, at the union's own path,
713// holds no errors and expects what the bases are, as a standalone validator's

Callers 3

applyDefaultMethod · 0.90
applyBranchesMethod · 0.90
failsUnionMethod · 0.85

Calls 6

pathAtFunction · 0.90
flattenUnionFunction · 0.85
failsAtBasisFunction · 0.85
stampErrorFunction · 0.85
setMethod · 0.45
addErrorMethod · 0.45

Tested by

no test coverage detected