Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
ParseResult: rename ParseErrors to ParseIssue (#640)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored Dec 7, 2023
1 parent deddf6e commit 8dcf12c
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 48 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-penguins-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": minor
---

ParseResult: rename `ParseErrors` to `ParseIssue`
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,11 +286,11 @@ The `parsePerson` function returns an `Either<ParseError, A>`, where `ParseError
interface ParseError {
readonly _tag: "ParseError";
// A non-empty list of errors
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseErrors>;
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseIssue>;
}
```

`ParseError` represents a list of errors that may have occurred during the parsing process, and `A` is the inferred data type described by the `Schema`. A successful parse results in a `Right` value, containing the parsed data `A`. In the case of a failed parse, the result will be a `Left` value containing a non-empty list of `ParseErrors`.
`ParseError` represents a list of errors that may have occurred during the parsing process, and `A` is the inferred data type described by the `Schema`. A successful parse results in a `Right` value, containing the parsed data `A`. In the case of a failed parse, the result will be a `Left` value containing a non-empty list of `ParseIssue`.

Now, let's see another example using the `parseSync` function.

Expand Down
4 changes: 2 additions & 2 deletions docs/modules/ArrayFormatter.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Added in v1.0.0
**Signature**

```ts
export declare const formatErrors: (errors: readonly [ParseErrors, ...ParseErrors[]]) => Array<Issue>
export declare const formatErrors: (errors: readonly [ParseIssue, ...ParseIssue[]]) => Array<Issue>
```
Added in v1.0.0
Expand All @@ -39,7 +39,7 @@ Added in v1.0.0
```ts
export interface Issue {
readonly _tag: ParseErrors["_tag"]
readonly _tag: ParseIssue["_tag"]
readonly path: ReadonlyArray<PropertyKey>
readonly message: string
}
Expand Down
30 changes: 15 additions & 15 deletions docs/modules/ParseResult.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Added in v1.0.0
- [Index (interface)](#index-interface)
- [Key (interface)](#key-interface)
- [Missing (interface)](#missing-interface)
- [ParseErrors (type alias)](#parseerrors-type-alias)
- [ParseIssue (type alias)](#parseissue-type-alias)
- [Type (interface)](#type-interface)
- [Unexpected (interface)](#unexpected-interface)
- [UnionMember (interface)](#unionmember-interface)
Expand All @@ -54,7 +54,7 @@ Added in v1.0.0

```ts
export declare const fail: (
error: ParseError | ParseErrors | ReadonlyArray.NonEmptyReadonlyArray<ParseErrors>
error: ParseError | ParseIssue | ReadonlyArray.NonEmptyReadonlyArray<ParseIssue>
) => ParseResult<never>
```
Expand All @@ -75,7 +75,7 @@ Added in v1.0.0
**Signature**
```ts
export declare const index: (index: number, errors: readonly [ParseErrors, ...ParseErrors[]]) => Index
export declare const index: (index: number, errors: readonly [ParseIssue, ...ParseIssue[]]) => Index
```
Added in v1.0.0
Expand All @@ -85,7 +85,7 @@ Added in v1.0.0
**Signature**
```ts
export declare const key: (key: PropertyKey, errors: readonly [ParseErrors, ...ParseErrors[]]) => Key
export declare const key: (key: PropertyKey, errors: readonly [ParseIssue, ...ParseIssue[]]) => Key
```
Added in v1.0.0
Expand Down Expand Up @@ -145,7 +145,7 @@ Added in v1.0.0
**Signature**
```ts
export declare const unionMember: (errors: readonly [ParseErrors, ...ParseErrors[]]) => UnionMember
export declare const unionMember: (errors: readonly [ParseIssue, ...ParseIssue[]]) => UnionMember
```
Added in v1.0.0
Expand All @@ -154,7 +154,7 @@ Added in v1.0.0
## Forbidden (interface)
The `Forbidden` variant of the `ParseError` type represents an error that occurs when an Effect is encounter but disallowed from execution.
The `Forbidden` variant of the `ParseIssue` type represents an error that occurs when an Effect is encounter but disallowed from execution.
**Signature**
Expand All @@ -179,15 +179,15 @@ that a specific element in an array did not match the expected type or value.
export interface Index {
readonly _tag: "Index"
readonly index: number
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseErrors>
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseIssue>
}
```

Added in v1.0.0

## Key (interface)

The `Key` variant of the `ParseError` type represents an error that occurs when a key in an object is invalid.
The `Key` variant of the `ParseIssue` type represents an error that occurs when a key in an object is invalid.
This error typically occurs when the `actual` value is not a valid key type (e.g. a string or number)
or when the key is not present in the object being decoded. In either case, the `key` field of the error will contain
the invalid key value. This error is typically used in combination with the `Unexpected` error,
Expand All @@ -199,7 +199,7 @@ which indicates that an unexpected key was found in the object being decoded.
export interface Key {
readonly _tag: "Key"
readonly key: PropertyKey
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseErrors>
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseIssue>
}
```

Expand All @@ -219,21 +219,21 @@ export interface Missing {

Added in v1.0.0

## ParseErrors (type alias)
## ParseIssue (type alias)

`ParseErrors` is a type that represents the different types of errors that can occur when decoding a value.

**Signature**

```ts
export type ParseErrors = Type | Index | Key | Missing | Unexpected | UnionMember | Forbidden
export type ParseIssue = Type | Index | Key | Missing | Unexpected | UnionMember | Forbidden
```
Added in v1.0.0
## Type (interface)
The `Type` variant of the `ParseError` type represents an error that occurs when the `actual` value is not of the expected type.
The `Type` variant of the `ParseIssue` type represents an error that occurs when the `actual` value is not of the expected type.
The `expected` field specifies the expected type, and the `actual` field contains the value that caused the error.
This error can occur when trying to decode a value using a schema that is only able to decode values of a specific type,
and the actual value is not of that type. For example, if you are using a schema to decode a string value and the actual value
Expand Down Expand Up @@ -276,7 +276,7 @@ Error that occurs when a member in a union has an error.
```ts
export interface UnionMember {
readonly _tag: "UnionMember"
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseErrors>
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseIssue>
}
```

Expand Down Expand Up @@ -357,7 +357,7 @@ Added in v1.0.0
```ts
export interface ParseError {
readonly _tag: "ParseError"
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseErrors>
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseIssue>
}
```

Expand All @@ -378,7 +378,7 @@ Added in v1.0.0
**Signature**

```ts
export declare const parseError: (errors: readonly [ParseErrors, ...ParseErrors[]]) => ParseError
export declare const parseError: (errors: readonly [ParseIssue, ...ParseIssue[]]) => ParseError
```
Added in v1.0.0
2 changes: 1 addition & 1 deletion docs/modules/TreeFormatter.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Added in v1.0.0
**Signature**

```ts
export declare const formatErrors: (errors: readonly [ParseErrors, ...ParseErrors[]]) => string
export declare const formatErrors: (errors: readonly [ParseIssue, ...ParseIssue[]]) => string
```
Added in v1.0.0
8 changes: 4 additions & 4 deletions src/ArrayFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@
*/
import * as Option from "effect/Option"
import * as ReadonlyArray from "effect/ReadonlyArray"
import type { ParseErrors } from "./ParseResult.js"
import type { ParseIssue } from "./ParseResult.js"
import { formatExpected, getMessage } from "./TreeFormatter.js"

/**
* @category model
* @since 1.0.0
*/
export interface Issue {
readonly _tag: ParseErrors["_tag"]
readonly _tag: ParseIssue["_tag"]
readonly path: ReadonlyArray<PropertyKey>
readonly message: string
}

const format = (self: ParseErrors, path: ReadonlyArray<PropertyKey> = []): Array<Issue> => {
const format = (self: ParseIssue, path: ReadonlyArray<PropertyKey> = []): Array<Issue> => {
const _tag = self._tag
switch (_tag) {
case "Type":
Expand Down Expand Up @@ -46,5 +46,5 @@ const format = (self: ParseErrors, path: ReadonlyArray<PropertyKey> = []): Array
* @since 1.0.0
*/
export const formatErrors = (
errors: ReadonlyArray.NonEmptyReadonlyArray<ParseErrors>
errors: ReadonlyArray.NonEmptyReadonlyArray<ParseIssue>
): Array<Issue> => ReadonlyArray.flatMap(errors, (e) => format(e))
30 changes: 15 additions & 15 deletions src/ParseResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ export interface ParseResult<A> extends Effect.Effect<never, ParseError, A> {}
*/
export interface ParseError {
readonly _tag: "ParseError"
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseErrors>
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseIssue>
}

class ParseErrorImpl implements Inspectable.Inspectable {
readonly _tag = "ParseError"
constructor(readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseErrors>) {}
constructor(readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseIssue>) {}
toString() {
return TreeFormatter.formatErrors(this.errors)
}
Expand All @@ -45,7 +45,7 @@ class ParseErrorImpl implements Inspectable.Inspectable {
* @since 1.0.0
*/
export const parseError = (
errors: ReadonlyArray.NonEmptyReadonlyArray<ParseErrors>
errors: ReadonlyArray.NonEmptyReadonlyArray<ParseIssue>
): ParseError => new ParseErrorImpl(errors)

/**
Expand All @@ -54,7 +54,7 @@ export const parseError = (
* @category model
* @since 1.0.0
*/
export type ParseErrors =
export type ParseIssue =
| Type
| Index
| Key
Expand All @@ -64,7 +64,7 @@ export type ParseErrors =
| Forbidden

/**
* The `Type` variant of the `ParseError` type represents an error that occurs when the `actual` value is not of the expected type.
* The `Type` variant of the `ParseIssue` type represents an error that occurs when the `actual` value is not of the expected type.
* The `expected` field specifies the expected type, and the `actual` field contains the value that caused the error.
* This error can occur when trying to decode a value using a schema that is only able to decode values of a specific type,
* and the actual value is not of that type. For example, if you are using a schema to decode a string value and the actual value
Expand All @@ -81,7 +81,7 @@ export interface Type {
}

/**
* The `Forbidden` variant of the `ParseError` type represents an error that occurs when an Effect is encounter but disallowed from execution.
* The `Forbidden` variant of the `ParseIssue` type represents an error that occurs when an Effect is encounter but disallowed from execution.
*
* @category model
* @since 1.0.0
Expand Down Expand Up @@ -121,7 +121,7 @@ export const forbidden: Forbidden = {
export interface Index {
readonly _tag: "Index"
readonly index: number
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseErrors>
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseIssue>
}

/**
Expand All @@ -130,11 +130,11 @@ export interface Index {
*/
export const index = (
index: number,
errors: ReadonlyArray.NonEmptyReadonlyArray<ParseErrors>
errors: ReadonlyArray.NonEmptyReadonlyArray<ParseIssue>
): Index => ({ _tag: "Index", index, errors })

/**
* The `Key` variant of the `ParseError` type represents an error that occurs when a key in an object is invalid.
* The `Key` variant of the `ParseIssue` type represents an error that occurs when a key in an object is invalid.
* This error typically occurs when the `actual` value is not a valid key type (e.g. a string or number)
* or when the key is not present in the object being decoded. In either case, the `key` field of the error will contain
* the invalid key value. This error is typically used in combination with the `Unexpected` error,
Expand All @@ -146,7 +146,7 @@ export const index = (
export interface Key {
readonly _tag: "Key"
readonly key: PropertyKey
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseErrors>
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseIssue>
}

/**
Expand All @@ -155,7 +155,7 @@ export interface Key {
*/
export const key = (
key: PropertyKey,
errors: ReadonlyArray.NonEmptyReadonlyArray<ParseErrors>
errors: ReadonlyArray.NonEmptyReadonlyArray<ParseIssue>
): Key => ({ _tag: "Key", key, errors })

/**
Expand Down Expand Up @@ -201,15 +201,15 @@ export const unexpected = (
*/
export interface UnionMember {
readonly _tag: "UnionMember"
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseErrors>
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseIssue>
}

/**
* @category constructors
* @since 1.0.0
*/
export const unionMember = (
errors: ReadonlyArray.NonEmptyReadonlyArray<ParseErrors>
errors: ReadonlyArray.NonEmptyReadonlyArray<ParseIssue>
): UnionMember => ({ _tag: "UnionMember", errors })

/**
Expand All @@ -236,9 +236,9 @@ export {
* @since 1.0.0
*/
export const fail = (
error: ParseError | ParseErrors | ReadonlyArray.NonEmptyReadonlyArray<ParseErrors>
error: ParseError | ParseIssue | ReadonlyArray.NonEmptyReadonlyArray<ParseIssue>
): ParseResult<never> => {
const e: any = error
const e = error
if ("_tag" in e) {
return e._tag === "ParseError" ? Either.left(e) : Either.left(parseError([e]))
}
Expand Down
6 changes: 3 additions & 3 deletions src/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ const go = (ast: AST.AST, isDecoding: boolean): Parser<any, any> => {
return ParseResult.fail(ParseResult.type(ast, input))
}
const allErrors = options?.errors === "all"
const es: Array<[number, ParseResult.ParseErrors]> = []
const es: Array<[number, ParseResult.ParseIssue]> = []
let stepKey = 0
// ---------------------------------------------
// handle missing indexes
Expand Down Expand Up @@ -603,7 +603,7 @@ const go = (ast: AST.AST, isDecoding: boolean): Parser<any, any> => {
return ParseResult.fail(ParseResult.type(ast, input))
}
const allErrors = options?.errors === "all"
const es: Array<[number, ParseResult.ParseErrors]> = []
const es: Array<[number, ParseResult.ParseIssue]> = []
let stepKey = 0

// ---------------------------------------------
Expand Down Expand Up @@ -796,7 +796,7 @@ const go = (ast: AST.AST, isDecoding: boolean): Parser<any, any> => {
map.set(ast.types[i], goMemo(ast.types[i], isDecoding))
}
return (input, options) => {
const es: Array<[number, ParseResult.ParseErrors]> = []
const es: Array<[number, ParseResult.ParseIssue]> = []
let stepKey = 0
let candidates: Array<AST.AST> = []
if (len > 0) {
Expand Down
8 changes: 4 additions & 4 deletions src/TreeFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as Option from "effect/Option"
import * as Predicate from "effect/Predicate"
import type { NonEmptyReadonlyArray } from "effect/ReadonlyArray"
import * as AST from "./AST.js"
import type { ParseErrors, Type } from "./ParseResult.js"
import type { ParseIssue, Type } from "./ParseResult.js"

interface Forest<A> extends ReadonlyArray<Tree<A>> {}

Expand All @@ -24,7 +24,7 @@ const make = <A>(value: A, forest: Forest<A> = []): Tree<A> => ({
* @category formatting
* @since 1.0.0
*/
export const formatErrors = (errors: NonEmptyReadonlyArray<ParseErrors>): string =>
export const formatErrors = (errors: NonEmptyReadonlyArray<ParseIssue>): string =>
drawTree(make(`error(s) found`, errors.map(go)))

const drawTree = (tree: Tree<string>): string => tree.value + draw("\n", tree.forest)
Expand Down Expand Up @@ -135,7 +135,7 @@ export const formatExpected = (ast: AST.AST): string => {
}
}

const isCollapsible = (es: Forest<string>, errors: NonEmptyReadonlyArray<ParseErrors>): boolean =>
const isCollapsible = (es: Forest<string>, errors: NonEmptyReadonlyArray<ParseIssue>): boolean =>
es.length === 1 && es[0].forest.length !== 0 && errors[0]._tag !== "UnionMember"

/** @internal */
Expand All @@ -148,7 +148,7 @@ export const getMessage = (e: Type) =>
)
)

const go = (e: ParseErrors): Tree<string> => {
const go = (e: ParseIssue): Tree<string> => {
switch (e._tag) {
case "Type":
return make(getMessage(e))
Expand Down
Loading

0 comments on commit 8dcf12c

Please sign in to comment.