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
Parameter | Type | Description |
---|---|---|
path | PathSegment [] |
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 fromObject.prototype.toString()
. To customize the output, you can pass atoString
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
Parameter | Type | Description |
---|---|---|
result | ParseResult <T > | The result of a parse operation. |
formatValue ? | (value ) => string | Optional 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"}"