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

Function buildStructureArbitrary

ark/fast-check/buildArbitrary.ts:148–654  ·  view source on GitHub ↗
(
	node: nodeOfKind<"intersection">,
	ctx: Ctx
)

Source from the content-addressed store, hash-verified

146}
147
148export const buildStructureArbitrary = (
149 node: nodeOfKind<"intersection">,
150 ctx: Ctx
151): fc.Arbitrary<unknown> => {
152 const structure = node.structure
153
154 if (node.basis?.is("domain") || (!node.basis && !structure?.sequence)) {
155 if (structure === undefined)
156 throwInternalError("Expected a structure node.")
157
158 if (!hasKey(structure, "index") || !structure.index)
159 return buildObjectArbitrary(structure, ctx)
160
161 const indexArbitrary = buildIndexSignatureArbitrary(
162 structure.index,
163 ctx,
164 structure.undeclared
165 )
166 if (!structure.props.length) return indexArbitrary
167
168 // declared props replace index keys that share their name
169 return fc
170 .tuple(indexArbitrary, buildObjectArbitrary(structure, ctx))
171 .map(([indexed, declared]) => {
172 const result: Record<PropertyKey, unknown> =
173 Object.getPrototypeOf(declared) === null ? Object.create(null) : {}
174 for (const key of Reflect.ownKeys(indexed))
175 defineValue(result, key, indexed[key as never])
176 for (const prop of structure.props) delete result[prop.key]
177 for (const key of Reflect.ownKeys(declared))
178 defineValue(result, key, declared[key as never])
179 return result
180 })
181 }
182
183 const seq = structure?.sequence
184 const fcArrayConstraints: fc.ArrayConstraints = {}
185 if (seq) {
186 if (seq.minLength > 0) fcArrayConstraints.minLength = seq.minLength
187 if (seq.maxLength !== null) fcArrayConstraints.maxLength = seq.maxLength
188 }
189 if (node.inner.exactLength) {
190 fcArrayConstraints.minLength = Math.max(
191 fcArrayConstraints.minLength ?? 0,
192 node.inner.exactLength.rule
193 )
194 fcArrayConstraints.maxLength =
195 fcArrayConstraints.maxLength !== undefined ?
196 Math.min(fcArrayConstraints.maxLength, node.inner.exactLength.rule)
197 : node.inner.exactLength.rule
198 }
199 if (node.inner.minLength) {
200 fcArrayConstraints.minLength = Math.max(
201 fcArrayConstraints.minLength ?? 0,
202 node.inner.minLength.rule
203 )
204 }
205 if (node.inner.maxLength) {

Callers 3

domain.tsFile · 0.90
proto.tsFile · 0.90
buildArbitraryFunction · 0.85

Calls 15

throwInternalErrorFunction · 0.90
hasKeyFunction · 0.90
defineValueFunction · 0.90
throwErrorFunction · 0.90
extractLengthBoundsFunction · 0.90
isArrayIndexKeyFunction · 0.90
buildObjectArbitraryFunction · 0.85
matchesAllStringsFunction · 0.85
isSymbolOnlyFunction · 0.85
clampMaxLengthFunction · 0.85
indexedValueOfFunction · 0.85

Tested by

no test coverage detected