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

Function jsExpression

ark/schema/structure/prop.ts:160–202  ·  view source on GitHub ↗
(js: NodeCompiler)

Source from the content-addressed store, hash-verified

158 }
159
160 jsExpression(js: NodeCompiler): string | undefined {
161 const key = js.serialize(this.key)
162 const value = `data${js.prop(this.key)}`
163 // a value no branch of which allows undefined fails a missing key too
164 if (
165 this.is("required") &&
166 (js.trav === "Allows" || (!this.value.tfs && !this.value.entersAlias)) &&
167 this.value.branches.every(branch => {
168 // Allows checks a morph's input
169 const input = branch.is("morph") ? branch.head : branch
170 const basis = input?.is("intersection") ? input.basis : input
171 return (
172 !!basis?.isBasis() && !(basis.is("unit") && basis.unit === undefined)
173 )
174 })
175 ) {
176 if (js.trav === "Apply") {
177 // a value a condition decides is checked by it before it's applied,
178 // a union by its branches
179 const condition =
180 isDecidedByAllows(this.value) && this.value.jsCond?.(js, value)
181 const site = condition && js.failingSiteOf?.(this)
182 const fails =
183 this.value.isBasis() ? `!${js.error(this, key)}`
184 : site ? `!${js.failsSite(site, "data", key)}`
185 : `!${js.failsIn(`${(condition && js.failingApplyOf?.(this.value)) || js.traversal(this.value, "Apply")}, data, ${js.applied(key, value, js.site(this))}`)}`
186 return condition ? `${condition} || ${fails}` : fails
187 }
188 // a value whose predicates read ctx is checked at its key's path
189 if (this.value.allowsCtx && this.value.checksCtx) return
190 return (
191 this.value.jsCond?.(js, value) ?? js.inv(this.value, { arg: value })
192 )
193 }
194 const fails = js.keyFails(key, value, this.value)
195 if (fails === undefined) return
196 return (
197 this.is("optional") ? `!(${key} in data) || !(${fails})`
198 : js.trav === "Apply" ?
199 `${key} in data ? !(${fails}) : !${js.error(this, key)}`
200 : `${key} in data && !(${fails})`
201 )
202 }
203}
204
205const formatPropKey = (key: string): string =>

Callers

nothing calls this directly

Calls 13

isDecidedByAllowsFunction · 0.90
propMethod · 0.80
isMethod · 0.80
traversalMethod · 0.80
siteMethod · 0.80
appliedMethod · 0.65
jsCondMethod · 0.45
failingSiteOfMethod · 0.45
errorMethod · 0.45
failsSiteMethod · 0.45
failsInMethod · 0.45
failingApplyOfMethod · 0.45

Tested by

no test coverage detected