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

Function checkDefault

ark/schema/structure/optional.ts:189–235  ·  view source on GitHub ↗
(
	node: BaseRoot,
	value: unknown,
	key: PropertyKey | null,
	// the node whose creation checks it, if any
	owner?: BaseNode
)

Source from the content-addressed store, hash-verified

187
188// unlike its input, a thunk's result may be an object
189export const checkDefault = (
190 node: BaseRoot,
191 value: unknown,
192 key: PropertyKey | null,
193 // the node whose creation checks it, if any
194 owner?: BaseNode
195): unknown => {
196 if (!isResolutionFinal() && node.hasAlias) {
197 // cached once it's created, which is before this is known to pass
198 if (owner) owner.c.unchecked = (owner.c.unchecked ?? 0) + 1
199 queueUnchecked(() => {
200 checkDefault(node, value, key, owner)
201 if (owner) owner.c.unchecked!--
202 })
203 return value
204 }
205
206 if ($ark.deferDefaultCheck?.(node, value, key, owner)) return value
207
208 // if the node has a default value, finalize it and apply JIT optimizations
209 // if applicable to ensure behavior + error logging is externally consistent
210 // (using .in here instead of ._in triggers finalization)
211 const out = (node.in as BaseRoot).traverseUnused(value)
212
213 if (out instanceof ArkErrors) {
214 const errors =
215 node.meta.description !== undefined && node.alters ?
216 inputErrors(node, value)
217 : out
218 // a ParseError is for the developer, so it renders in English
219 const en = $ark.defaultConfig.locale
220
221 if (key === null) {
222 // e.g. "Default must be assignable to number (was string)"
223 throwParseError(`Default ${errors.summaryIn(en)}`)
224 }
225
226 const atPath = new ArkErrors(node.$.cfg)
227 atPath._merge(errors, [key])
228
229 // e.g. "Default for bar must be assignable to number (was string)"
230 // e.g. "Default for value at [0] must be assignable to number (was string)"
231 throwParseError(`Default for ${atPath.summaryIn(en)}`)
232 }
233
234 return value
235}
236
237// a default is easily mistaken for a morph's output, so a rejected one reports
238// what its input checks

Callers 3

parseTupleLiteralFunction · 0.90
parseObjectLiteralFunction · 0.90

Calls 7

_mergeMethod · 0.95
summaryInMethod · 0.95
isResolutionFinalFunction · 0.90
queueUncheckedFunction · 0.90
throwParseErrorFunction · 0.90
inputErrorsFunction · 0.85
summaryInMethod · 0.65

Tested by

no test coverage detected