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

Function validateBoth

ark/aot/shadow.ts:790–884  ·  view source on GitHub ↗
(
	pair: Pair,
	member: string,
	data: unknown,
	{ isWritten }: { isWritten: boolean },
	run: (t: any, data: unknown) => unknown
)

Source from the content-addressed store, hash-verified

788const mismatched = Symbol("mismatched")
789
790const validateBoth = (
791 pair: Pair,
792 member: string,
793 data: unknown,
794 { isWritten }: { isWritten: boolean },
795 run: (t: any, data: unknown) => unknown
796): unknown => {
797 const counterparts = new Map<object, unknown>()
798 const copy = isWritten ? copyOf(data, counterparts) : uncopyable
799 // e.g. ctx.root, which both sides share where it isn't copied
800 if (copy === uncopyable && hasDomain(data, "object"))
801 counterparts.set(data, data)
802 const current: Validation = {
803 phase: "record",
804 skipsApply: false,
805 calls: [],
806 replayed: 0,
807 counterparts,
808 isCopied: copy !== uncopyable,
809 mismatch: undefined
810 }
811 const outer = validation
812 validation = current
813 let arktype: Outcome
814 let precompiled: Outcome
815 try {
816 arktype = inArktype(() => outcomeOf(() => run(pair.original, data)))
817 current.phase = "replay"
818 current.skipsApply = pair.contract.errors === "none" && fails(arktype)
819 precompiled = outcomeOf(() =>
820 run(pair.counterpart, current.isCopied ? copy : data)
821 )
822 } finally {
823 validation = outer
824 }
825 // errors "first" stops at any one issue, before calls arktype makes after it
826 const stopsEarly = pair.contract.errors === "first" && fails(arktype)
827 const unreplayed =
828 current.skipsApply || stopsEarly ?
829 undefined
830 : current.calls[current.replayed]
831 current.mismatch ??= unreplayed && [describeCall(unreplayed), "makes no call"]
832 const observation = () =>
833 `${member}(${printable(data)})${isWritten ? ", the input as arktype's morphs left it" : ""}`
834 const reason =
835 isWritten && !current.isCopied ?
836 "which its validator received too, since it can't be copied"
837 : undefined
838 if (current.mismatch) fail(pair, observation(), ...current.mismatch, reason)
839 const writesInput = !!pair.contract.mutatesInput
840 const arktypeObserved = inArktype(() => observe(arktype, { writesInput }))
841 const precompiledObserved = observe(precompiled, { writesInput })
842 // errors "none" fails with one root issue, compared without its data
843 if (current.skipsApply) {
844 arktypeObserved.issues = [{ code: "predicate", path: [] }]
845 const { issues } = precompiledObserved
846 if (issues) {
847 precompiledObserved.issues = issues.map(({ code, path }) => ({

Callers 2

shadowOfFunction · 0.85
validatorFunction · 0.85

Calls 15

hasDomainFunction · 0.90
hasArkKindFunction · 0.90
inArktypeFunction · 0.85
describeCallFunction · 0.85
observationFunction · 0.85
firstIssueOfFunction · 0.85
comparedOfFunction · 0.85
allIssuesOfFunction · 0.85
copyOfFunction · 0.70
outcomeOfFunction · 0.70
runFunction · 0.70
failsFunction · 0.70

Tested by

no test coverage detected