Failure
type Failure: {
message: string;
path: PathSegment[];
};Describes why and where parsing failed.
Type declaration
message
message: string;A human-readable description of why parsing failed. Intended for logging and debugging — do not pattern-match on this string, as its exact wording may change between releases.
path
path: PathSegment[];The location in the data structure where parsing failed, as a sequence of path segments from the root. Use formatPath to format it as a JSONPath string.
ParseFailure
type ParseFailure: {
error: Failure;
tag: "failure";
};The parsing failed.
Type declaration
error
error: Failure;tag
tag: "failure";ParseResult<T>
type ParseResult<T>: ParseSuccess<T> | ParseFailure;Describes the result of a parsing operation. The tag and error properties can be used to distinguish between success and failure.
Type Parameters
| Type Parameter |
|---|
T |
Examples
Use error to distinguish between success and failure:
const result = parseNumber(data)
if(result.error) {
console.error(formatResult(result))
return
}
console.log(result.value)Use tag to distinguish between success and failure:
const result = parseNumber(data)
switch (result.tag) {
case 'failure':
console.error(formatResult(result))
break
case 'success':
console.log(result.value)
break
***
## ParseSuccess\<T\>
```ts
type ParseSuccess<T>: {
error: never;
tag: "success";
value: T;
};The data adheres to the schema. The value is equal to the parsed data
Type Parameters
| Type Parameter |
|---|
T |
Type declaration
error?
optional error: never;tag
tag: "success";value
value: T;PathSegment
type PathSegment: {
key: string;
tag: "object";
} | {
index: number;
tag: "array";
};Describes the path in a data structure where parsing failed.
failure()
function failure(message): ParseFailureCreate a failure parsing result.
Example
const customParser: Parser<number> = (data) => {
if (typeof data === 'number') {
return success(data)
}
return failure('Expected a number')
}Parameters
| Parameter | Type | Description |
|---|---|---|
message | string |
Returns
isFailure()
function isFailure<T>(result): result is ParseFailureCheck if the result is a failure
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type | Description |
|---|---|---|
result | ParseResult<T> |
Returns
result is ParseFailure
isSuccess()
function isSuccess<T>(result): result is ParseSuccess<T>Check if the result is a success
Type Parameters
| Type Parameter |
|---|
T |
Parameters
| Parameter | Type | Description |
|---|---|---|
result | ParseResult<T> |
Returns
result is ParseSuccess<T>
propagateFailure()
function propagateFailure(failureRes, pathSegment): ParseFailurePropagate a failure result in a nested structure. When parsing objects and arrays with nested values, the failure at the root level should convey where in the hierarchy the failure occurred.
Parameters
| Parameter | Type | Description |
|---|---|---|
failureRes | ParseFailure | |
pathSegment | PathSegment |
Returns
success()
function success<T>(value): ParseSuccess<T>Create a successful parsing result.
Example
const customParser: Parser<number> = (data) => {
if (typeof data === 'number') {
return success(data)
}
return failure('Expected a number')
}
@param value
### Type Parameters
| Type Parameter |
| ------ |
| `T` |
### Parameters
| Parameter | Type |
| ------ | ------ |
| `value` | `T` |
### Returns
[`ParseSuccess`](ParseResult.md#parsesuccesst)\<`T`\>