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

Function parseStandardSchema

ark/type/parser/definition.ts:202–271  ·  view source on GitHub ↗
(
	def: StandardSchemaV1,
	ctx: BaseParseContext,
	isForeign?: true
)

Source from the content-addressed store, hash-verified

200// a Type from another copy of arktype, like 2.x, is foreign: it has its
201// arkKind but not its methods, so it validates through its Standard Schema
202const parseStandardSchema = (
203 def: StandardSchemaV1,
204 ctx: BaseParseContext,
205 isForeign?: true
206): BaseRoot => {
207 const parsed = standardMorphs.get(def)
208 if (parsed) return ctx.$.intrinsic.unknown.pipe(parsed)
209 const validate: StandardMorph = (v, ctx) => {
210 const result = def["~standard"].validate(v)
211
212 if (isThenable(result)) {
213 return throwError(
214 writeAsyncStandardSchemaMessage(def["~standard"].vendor)
215 )
216 }
217
218 if (!result.issues) return result.value
219
220 // a result with issues fails, even if they're empty
221 if (result.issues.length === 0)
222 return ctx.error({ expected: "valid", message: "is invalid" })
223
224 // read as it fails, since parsing reads nothing of a definition
225 const { vendor, _c: isCustomError } = def["~standard"] as ArkWrittenProps
226 const fromArk = vendor === "arktype"
227 let firstError: ArkError | undefined
228 for (const issue of result.issues) {
229 const relativePath = (issue.path ?? []).map(k =>
230 typeof k === "object" ? k.key : k
231 )
232 // an arktype 2 message starts with the path its issue already has, so
233 // its problem is read
234 const { problem, _c, _l, _m, _n } = issue as ArkWrittenIssue
235 const fromArk2 = isForeign && typeof problem === "string"
236 const error = ctx.error({
237 message: fromArk2 ? problem : issue.message,
238 relativePath
239 })
240 firstError ??= error
241 // another library's message is a sentence of its own, unlike what
242 // arktype wrote from what it expected. Which it is isn't this copy's
243 // to tell where another's errors keep that to themselves, so a Type
244 // answers for its own. A standalone validator's issue holds only its
245 // message, read as its own summary reads it; one an earlier build
246 // wrote notes a sentence of its own as `_c`
247 // a message naming a label is arktype's own, said of its value
248 if (fromArk && typeof _n === "string") asLabeled(error, _n)
249 else if (
250 fromArk2 ||
251 (fromArk &&
252 _c === undefined &&
253 (isCustomError ?
254 !isCustomError(issue)
255 : !isCustomIssue(issue as never)))
256 ) {
257 asTemplated(
258 error,
259 // a union listing its branches' errors names their whole paths

Callers 2

parseObjectFunction · 0.85
parseForeignRootFunction · 0.85

Calls 2

getMethod · 0.65
setMethod · 0.45

Tested by

no test coverage detected