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

Function parseObject

ark/type/parser/definition.ts:120–161  ·  view source on GitHub ↗
(def: object, ctx: BaseParseContext)

Source from the content-addressed store, hash-verified

118}
119
120export const parseObject = (def: object, ctx: BaseParseContext): BaseRoot => {
121 const objectKind = objectKindOf(def)
122 switch (objectKind) {
123 case undefined:
124 if (hasArkKind(def, "root")) {
125 return isInstanceOf(def, BaseRoot) ?
126 def.internal
127 : parseForeignRoot(def, ctx)
128 }
129 if (isStandardSchema(def)) return parseStandardSchema(def as never, ctx)
130 return parseObjectLiteral(def as Dict, ctx)
131 case "Array":
132 return parseTuple(def as array, ctx)
133 case "RegExp":
134 return ctx.$.node(
135 "intersection",
136 {
137 domain: ctx.$.node("domain", { domain: "string" }),
138 pattern: [
139 ctx.$.node("pattern", constraintInputOf("pattern", def as RegExp))
140 ]
141 },
142 { reduced: true }
143 )
144 case "Function": {
145 if (hasArkKind(def, "root")) {
146 return isInstanceOf(def, BaseRoot) ?
147 def.internal
148 : parseForeignRoot(def, ctx)
149 }
150 // e.g. Effect's standardSchemaV1, a class that can't be called as a thunk
151 if (isStandardSchema(def)) return parseStandardSchema(def as never, ctx)
152 const resolvedDef = isThunk(def) ? resolveThunk(def) : def
153 if (hasArkKind(resolvedDef, "root")) return parseObject(resolvedDef, ctx)
154 return throwParseError(writeBadDefinitionTypeMessage("Function"))
155 }
156 default:
157 return throwParseError(
158 writeBadDefinitionTypeMessage(objectKind ?? printable(def))
159 )
160 }
161}
162
163// a class whose constructor takes nothing reads as a thunk, and calling it
164// would throw a TypeError in place of the message a class gets. One written

Callers 1

parseInnerDefinitionFunction · 0.85

Calls 15

objectKindOfFunction · 0.90
hasArkKindFunction · 0.90
isInstanceOfFunction · 0.90
isStandardSchemaFunction · 0.90
parseObjectLiteralFunction · 0.90
constraintInputOfFunction · 0.90
isThunkFunction · 0.90
throwParseErrorFunction · 0.90
printableFunction · 0.90
parseForeignRootFunction · 0.85
parseStandardSchemaFunction · 0.85
parseTupleFunction · 0.85

Tested by

no test coverage detected