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

Function rootApplyParts

ark/schema/shared/rootApply.ts:46–131  ·  view source on GitHub ↗
(
	node: BaseRoot,
	name: string,
	// names runtime values as the artifact's module does
	refOf?: (param: RootApplyParam) => string
)

Source from the content-addressed store, hash-verified

44// and the one its failure path is. An artifact's reads its name from the node,
45// so what differs between two roots is only what they're passed
46export const rootApplyParts = (
47 node: BaseRoot,
48 name: string,
49 // names runtime values as the artifact's module does
50 refOf?: (param: RootApplyParam) => string
51): [reads: string[], rootApply: string, applyFailed: string | undefined] => {
52 const ref = refOf ?? ((param: RootApplyParam) => param)
53 const js = new CompiledFunction("data", "onFail").indent()
54 const applyFailed = new CompiledFunction("data", "onFail").indent()
55 const newCtx = `new ${ref("Traversal")}(data, ${ref("config")})`
56 // a validator passes its own handler
57 const handler =
58 refOf ? "onFail" : "onFail === undefined ? node.onFail : onFail"
59 const fallback = (body: typeof js) =>
60 node.hasAlias ?
61 body.return(
62 `${ref("applyCyclic")}(node, data, ${newCtx}).fin(${handler})`
63 )
64 : body.return(
65 `${ref("applyRejected")}(node, data, ${ref("config")}, ${handler}, ${ref("Traversal")})`
66 )
67 const returnResult = () =>
68 node.hasMorph ?
69 js
70 .if(
71 `result !== data && typeof result === "function" && result instanceof ${ref("TransformErrors")}`,
72 () =>
73 js
74 .const("ctx", newCtx)
75 .line(`${ref("addMorphErrs")}(ctx, result)`)
76 .return(`ctx.fin(${handler})`)
77 )
78 .return("result")
79 : js.return("result")
80 if (node.applyMode === "contextual") {
81 // failure path is its own function
82 fallback(applyFailed)
83 js.const("ctx", newCtx)
84 .if("!tAllows(data, ctx) || ctx.hasError()", () =>
85 js.return("applyFailed(data, onFail)")
86 )
87 .return("data")
88 } else if (node.applyMode === "allows")
89 js.if("!allows(data)", fallback).return("data")
90 else if (node.applyMode === "contextualTransform") {
91 if (node.hasAlias) {
92 js.const("untracked", `${ref("allowsAcyclic")}(node, data)`)
93 .if(
94 `!(untracked ?? ${ref("allowsInContext")}(node, data, ${ref("config")}, ${ref("Traversal")}))`,
95 fallback
96 )
97 .const("ctx", newCtx)
98 .set("ctx.isTree", "untracked === undefined")
99 .const(
100 "result",
101 `${ref("transformResolution")}(ctx, ${refOf ? "node.id" : `"${name}"`}, data, tTransform)`
102 )
103 } else {

Callers 2

emitRootApplyMethod · 0.85
writeRootApplyFunction · 0.85

Calls 13

returnResultFunction · 0.85
indentMethod · 0.80
returnMethod · 0.80
ifMethod · 0.80
constMethod · 0.80
isMethod · 0.80
filterMethod · 0.80
testMethod · 0.80
refFunction · 0.70
fallbackFunction · 0.70
mapMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected