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

Function parseNode

ark/schema/parse.ts:139–211  ·  view source on GitHub ↗
(ctx: NodeParseContext)

Source from the content-addressed store, hash-verified

137}
138
139export const parseNode = (ctx: NodeParseContext): BaseNode => {
140 const impl = nodeImplementationsByKind[ctx.kind]
141 const input = (impl.applyConfig?.(ctx.def, ctx.$.cfg) ?? ctx.def) as dict & {
142 meta?: ArkEnv.meta | string
143 }
144
145 const metaInput = input.meta
146 const meta: ArkEnv.meta =
147 metaInput === undefined ? {}
148 : typeof metaInput === "string" ? { description: metaInput }
149 : metaInput.label === "" ? withoutLabel(metaInput)
150 : metaInput
151
152 const keys = Object.keys(input)
153 // most schemas list their keys in order already
154 for (let i = 1; i < keys.length; i++) {
155 if (compareInnerKeys(keys[i - 1], keys[i]) > 0) {
156 keys.sort(compareInnerKeys)
157 break
158 }
159 }
160
161 const inner: dict = {}
162 for (let i = 0; i < keys.length; i++) {
163 const k = keys[i]
164 if (k === "meta") continue
165 const keyImpl = hasOwn(impl.keys, k) ? impl.keys[k] : undefined
166 if (!keyImpl)
167 return throwParseError(`Key ${k} is not valid on ${ctx.kind} schema`)
168 const v = input[k]
169 if (v === undefined && !keyImpl.keepUndef) continue
170 // an empty list of children is none, except a union's branches
171 if (keyImpl.child && isArray(v) && !v.length && k !== "branches") continue
172 inner[k] =
173 keyImpl.structural ? deferAliases(v, ctx.$)
174 : k === "unit" && ctx.kind !== "unit" ? normalizeBoundUnit(v as never)
175 : v
176 }
177
178 let unchecked = false
179 if ($ark.sets) {
180 $ark.sets.sort(ctx.kind, inner)
181 if (!ctx.reduced) {
182 const uncheckedBefore = uncheckedQueued()
183 const reduced = $ark.sets.reduce(ctx.kind, inner, ctx.$)
184 if (reduced) {
185 if (reduced instanceof Disjoint) return reduced.throw()
186
187 if (hasNoMeta(meta) && (reduced.c.key || !hasNoMeta(reduced.meta))) {
188 if (reduced.c.key) $ark.reached?.node(reduced)
189 return reduced
190 }
191
192 // we can't cache this reduction for now in case the reduction involved
193 // impliedBy
194 return withMeta(reduced, meta)
195 }
196 unchecked = uncheckedQueued() > uncheckedBefore

Callers 1

scope.tsFile · 0.90

Calls 15

hasOwnFunction · 0.90
throwParseErrorFunction · 0.90
normalizeBoundUnitFunction · 0.90
uncheckedQueuedFunction · 0.90
withoutLabelFunction · 0.85
compareInnerKeysFunction · 0.85
deferAliasesFunction · 0.85
withMetaFunction · 0.85
attachBoundUnitsFunction · 0.85
createNodeFunction · 0.85
sortMethod · 0.80
reduceMethod · 0.80

Tested by

no test coverage detected