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

Function constObjectRoot

ark/sets/fromJsonSchema.ts:1347–1398  ·  view source on GitHub ↗
(
	value: object,
	pointer: string,
	depth: number,
	seen: Set<object>
)

Source from the content-addressed store, hash-verified

1345}
1346
1347const constObjectRoot = (
1348 value: object,
1349 pointer: string,
1350 depth: number,
1351 seen: Set<object>
1352): BaseRoot => {
1353 const required: RequiredPropEntry[] = []
1354 const index: unknown[] = []
1355 const predicate: unknown[] = [notArray()]
1356 const ownKeys: string[] = []
1357 for (const [key, v] of Object.entries(value)) {
1358 const propValue = constRoot(
1359 v,
1360 `${pointer}/${escapePointer(key)}`,
1361 depth,
1362 seen
1363 )
1364 if (key in Object.prototype)
1365 addPrototypeKeyConstraint({ index, predicate }, key, propValue, true)
1366 else {
1367 required.push({ key, value: propValue })
1368 ownKeys.push(key)
1369 }
1370 }
1371 if (ownKeys.length) {
1372 const sortedKeys = ownKeys.length > 1 ? [...ownKeys].sort() : ownKeys
1373 const cacheKey = JSON.stringify(["ownKeys", sortedKeys])
1374 const predicateFn =
1375 predicatesByKey.get(cacheKey) ??
1376 predicatesByKey.set(
1377 cacheKey,
1378 makeMorph(hasOwnKeys as never, sortedKeys) as unknown as Predicate
1379 )
1380 ownKeysPredicates.add(predicateFn)
1381 predicate.push({
1382 predicate: predicateFn,
1383 meta: {
1384 description:
1385 sortedKeys.length === 1 ?
1386 `an object with its own ${sortedKeys[0]}`
1387 : `an object with its own ${sortedKeys.join(", ")}`
1388 }
1389 })
1390 }
1391 return jsonSchemaRoot({
1392 domain: "object",
1393 predicate,
1394 required,
1395 ...(index.length && { index }),
1396 undeclared: "reject"
1397 } as never)
1398}
1399
1400const constRoot = (
1401 value: unknown,

Callers 1

constRootFunction · 0.85

Calls 10

makeMorphFunction · 0.90
constRootFunction · 0.85
escapePointerFunction · 0.85
jsonSchemaRootFunction · 0.85
sortMethod · 0.80
getMethod · 0.65
pushMethod · 0.45
setMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected