Skip to content

formatPath()

ts
function formatPath(path): string

Formats a failure Path to a JsonPath.

Example

ts
const path = [{ tag: 'object', key: 'name' }]
console.log(formatPath(path))
// "$.name"

Parameters

ParameterTypeDescription
pathPathSegment[]

Returns

string


formatResult()

ts
function formatResult<T>(result, formatValue?): string

Formats a failure to a human-readable string. This is useful for debugging and logging parse results. It formats both successful and unsuccessful parse results.

  • Successful results are formatted as ParseSuccess: <value>, where <value> comes from Object.prototype.toString(). To customize the output, you can pass a toString function that converts the value to a string.
  • Failures include the error message and the path where the failure occurred.

Type Parameters

Type Parameter
T

Parameters

ParameterTypeDescription
resultParseResult<T>The result of a parse operation.
formatValue?(value) => stringOptional function to convert the value to a string. If not provided, it uses string interpolation.

Returns

string

Examples

Format an unsuccessful parse result:

ts
const parseUser = object({ name: parseString })
const res = parseUser({ name: 123 })
console.log(formatResult(res)) // -> "ParseFailure: Expected type string at $.name"

Format a successful parse result:

ts
const parseUser = object({ name: parseString })
const res = parseUser({ name: 'Alice' })
console.log(formatResult(res, JSON.stringify)) // -> "ParseSuccess: {"name":"Alice"}"