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

Function readKeyPredicates

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

Source from the content-addressed store, hash-verified

1119// the keywords constraining an object's keys as a whole. One every object
1120// meets adds no predicate, which would keep it from relating to one without
1121const readKeyPredicates = (
1122 o: dict,
1123 pointer: string,
1124 ctx: ReadContext
1125): NodeSchema<"predicate">[] => {
1126 const predicates: NodeSchema<"predicate">[] = []
1127 if (o.propertyNames !== undefined) {
1128 const names = withChildValue(ctx, () =>
1129 read(o.propertyNames, `${pointer}/propertyNames`, ctx)
1130 )
1131 if (!$ark.kw.string.extends(names)) {
1132 predicates.push(
1133 opaque(
1134 ["propertyNames", names.id],
1135 `an object whose keys are ${expectedOf(names)}`,
1136 (data: dict) => Object.keys(data).every(k => names.allows(k)),
1137 "`propertyNames` keyword"
1138 )
1139 )
1140 }
1141 }
1142 const min = (o.minProperties ?? 0) as number
1143 const max = (o.maxProperties ?? Number.POSITIVE_INFINITY) as number
1144 if (min !== 0 || max !== Number.POSITIVE_INFINITY) {
1145 const countFeature =
1146 min !== 0 && max !== Number.POSITIVE_INFINITY ?
1147 "`minProperties`/`maxProperties` keyword"
1148 : min !== 0 ? "`minProperties` keyword"
1149 : "`maxProperties` keyword"
1150 predicates.push(
1151 opaque(
1152 ["properties", min, max],
1153 `an object with ${describeCount(min, max, "property", "properties")}`,
1154 (data: dict) => {
1155 const count = Object.keys(data).length
1156 return count >= min && count <= max
1157 },
1158 countFeature
1159 )
1160 )
1161 }
1162 const dependencyKeywords =
1163 ctx.dialectBefore2019 ? (["dependencies"] as const)
1164 : ctx.dialect2020 ? (["dependentRequired", "dependentSchemas"] as const)
1165 : (["dependencies", "dependentRequired", "dependentSchemas"] as const)
1166 for (const keyword of dependencyKeywords) {
1167 const dependencies = o[keyword] as dict
1168 for (const key in dependencies) {
1169 const dependency = dependencies[key]
1170 if (isArray(dependency)) {
1171 if (dependency.length === 0) continue
1172 predicates.push(
1173 opaque(
1174 ["dependentRequired", key, dependency],
1175 `an object with ${dependency.join(", ")} if it has ${key}`,
1176 (data: dict) =>
1177 !hasOwn(data, key) ||
1178 dependency.every(k => hasOwn(data, k as string)),

Callers 1

readObjectFunction · 0.85

Calls 11

hasOwnFunction · 0.90
withChildValueFunction · 0.85
opaqueFunction · 0.85
describeCountFunction · 0.85
withShallowPredicateFunction · 0.85
escapePointerFunction · 0.85
extendsMethod · 0.80
allowsMethod · 0.80
readFunction · 0.70
expectedOfFunction · 0.70
pushMethod · 0.45

Tested by

no test coverage detected