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

Function validate

ark/type/parser/definition.ts:209–267  ·  view source on GitHub ↗
(v, ctx)

Source from the content-addressed store, hash-verified

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
260 _m ? _m.call(issue, ctx.path)._l
261 : ctx.path.length === 0 ? _l
262 : undefined
263 )
264 }
265 }
266 return firstError

Callers 1

context.test.tsFile · 0.50

Calls 10

throwErrorFunction · 0.90
asLabeledFunction · 0.90
isCustomIssueFunction · 0.90
asTemplatedFunction · 0.90
isCustomErrorFunction · 0.85
callMethod · 0.80
isThenableFunction · 0.70
mapMethod · 0.65
errorMethod · 0.45

Tested by

no test coverage detected