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

Function readObject

ark/sets/fromJsonSchema.ts:1016–1117  ·  view source on GitHub ↗
(
	o: dict,
	pointer: string,
	ctx: ReadContext
)

Source from the content-addressed store, hash-verified

1014}
1015
1016const readObject = (
1017 o: dict,
1018 pointer: string,
1019 ctx: ReadContext
1020): RootSchema[] => {
1021 const properties = (o.properties ?? {}) as dict
1022 const patternProperties = (o.patternProperties ?? {}) as dict
1023 const additional = withChildValue(ctx, () =>
1024 read(o.additionalProperties ?? true, `${pointer}/additionalProperties`, ctx)
1025 )
1026 const patterns = Object.keys(patternProperties).map(source => {
1027 const patternPointer = `${pointer}/patternProperties/${escapePointer(source)}`
1028 const schema = patternSchemaOf(source, patternPointer)
1029 return {
1030 schema,
1031 matcher: new RegExp(schema.rule, schema.flags),
1032 value: withChildValue(ctx, () =>
1033 read(patternProperties[source], patternPointer, ctx)
1034 )
1035 }
1036 })
1037 const isDeclared = (k: string) =>
1038 hasOwn(properties, k) || patterns.some(p => p.matcher.test(k))
1039 const required = (o.required ?? []) as string[]
1040 const structure = {
1041 required: [] as unknown[],
1042 optional: [] as unknown[],
1043 index: patterns.map(p => ({
1044 signature: { domain: "string", pattern: [p.schema] },
1045 value: p.value
1046 })) as unknown[],
1047 predicate: [] as unknown[],
1048 // stated, so configure({ onUndeclaredKey }) can't change it
1049 undeclared: (additional.isNever() ? "reject" : "ignore") as
1050 | "reject"
1051 | "ignore",
1052 // as properties lists them
1053 order: [] as string[]
1054 }
1055 for (const key of new Set([...Object.keys(properties), ...required])) {
1056 // a declared key meets every pattern it matches, else additionalProperties
1057 const value = patterns
1058 .filter(p => p.matcher.test(key))
1059 .reduce(
1060 (result, p) => intersectRoots(result, p.value),
1061 hasOwn(properties, key) ?
1062 withChildValue(ctx, () =>
1063 read(
1064 properties[key],
1065 `${pointer}/properties/${escapePointer(key)}`,
1066 ctx
1067 )
1068 )
1069 : isDeclared(key) ? $ark.kw.unknown
1070 : additional
1071 )
1072 const isRequired = required.includes(key)
1073 if (isRequired && value.isNever()) return [$ark.kw.never]

Callers 1

fromJsonSchema.tsFile · 0.85

Calls 15

hasOwnFunction · 0.90
withChildValueFunction · 0.85
escapePointerFunction · 0.85
patternSchemaOfFunction · 0.85
intersectRootsFunction · 0.85
isDeclaredFunction · 0.85
opaqueFunction · 0.85
readKeyPredicatesFunction · 0.85
reduceMethod · 0.80
filterMethod · 0.80
testMethod · 0.80

Tested by

no test coverage detected