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

Function typeHandlerOf

ark/aot/shadow.ts:261–356  ·  view source on GitHub ↗
(
	pair: Pair & { original: BaseRoot }
)

Source from the content-addressed store, hash-verified

259] as const
260
261const typeHandlerOf = (
262 pair: Pair & { original: BaseRoot }
263): ProxyHandler<any> => {
264 const membersByKey: Record<PropertyKey, unknown> = Object.create(null)
265 // allows runs no morph, so only a validation that can write the input copies it
266 const isWritten = inArktype(() => pair.original.tfs)
267 const validator =
268 (...path: string[]) =>
269 (data: unknown) =>
270 validateBoth(
271 pair,
272 path.join(".") || "call",
273 data,
274 { isWritten: path[0] !== "allows" && isWritten },
275 (t, data) =>
276 path[0] === "~standard" ? asErrors(readAt(t, path, [data]))
277 : path.length ? readAt(t, path, [data])
278 : t(data)
279 )
280 const introspector =
281 (...path: string[]) =>
282 (...args: unknown[]) =>
283 introspect(pair, path, args)
284 const memberOf = (target: BaseRoot, k: PropertyKey): unknown => {
285 const counterpart = pair.counterpart as Record<PropertyKey, unknown>
286 if (includes(droppableMembers, k) && !(k in counterpart))
287 return boundTo(target, checkedMember(pair, target, k))
288 if (includes(validatedMembers, k)) return validator(k)
289 if (
290 includes(introspectedMembers, k) &&
291 !isUnbaked(counterpart, k) &&
292 "value" in outcomeOf(() => counterpart[k])
293 )
294 return introspector(k)
295 if (k === "meta") {
296 if (keywordMetas().has(target.meta)) {
297 compare(
298 pair,
299 "meta",
300 target.meta,
301 counterpart.meta,
302 "since its validator doesn't bake a keyword's"
303 )
304 }
305 // arktype holds the recorder of each function the program passed
306 const meta: Record<PropertyKey, unknown> = { ...target.meta }
307 for (const k in meta)
308 meta[k] = callbackByWrapper.get(meta[k] as never) ?? meta[k]
309 return Object.freeze(meta)
310 }
311 // bound over this proxy's members, so each call it makes compares both
312 if (k === "withContext") {
313 return (context: ArkEnv.context) =>
314 withContextOf(
315 Object.assign(validator(), {
316 assert: validator("assert"),
317 allows: validator("allows") as (data: unknown) => boolean,
318 "~standard": memberOf(target, "~standard") as never

Callers 1

shadowOfFunction · 0.85

Calls 8

inArktypeFunction · 0.85
hasOfFunction · 0.85
boundToFunction · 0.85
introspectFunction · 0.85
memberOfFunction · 0.85
validatorFunction · 0.70
getMethod · 0.65
applyMethod · 0.45

Tested by

no test coverage detected