diff --git a/.changeset/gold-penguins-hunt.md b/.changeset/gold-penguins-hunt.md new file mode 100644 index 000000000..edebb487b --- /dev/null +++ b/.changeset/gold-penguins-hunt.md @@ -0,0 +1,5 @@ +--- +"@effect/schema": minor +--- + +ParseResult: rename `ParseErrors` to `ParseIssue` diff --git a/README.md b/README.md index f2e1c8786..a4b21e1a2 100644 --- a/README.md +++ b/README.md @@ -286,11 +286,11 @@ The `parsePerson` function returns an `Either`, where `ParseError interface ParseError { readonly _tag: "ParseError"; // A non-empty list of errors - readonly errors: ReadonlyArray.NonEmptyReadonlyArray; + readonly errors: ReadonlyArray.NonEmptyReadonlyArray; } ``` -`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. diff --git a/docs/modules/ArrayFormatter.ts.md b/docs/modules/ArrayFormatter.ts.md index 8eb9ed77a..dd2ceb62b 100644 --- a/docs/modules/ArrayFormatter.ts.md +++ b/docs/modules/ArrayFormatter.ts.md @@ -26,7 +26,7 @@ Added in v1.0.0 **Signature** ```ts -export declare const formatErrors: (errors: readonly [ParseErrors, ...ParseErrors[]]) => Array +export declare const formatErrors: (errors: readonly [ParseIssue, ...ParseIssue[]]) => Array ``` Added in v1.0.0 @@ -39,7 +39,7 @@ Added in v1.0.0 ```ts export interface Issue { - readonly _tag: ParseErrors["_tag"] + readonly _tag: ParseIssue["_tag"] readonly path: ReadonlyArray readonly message: string } diff --git a/docs/modules/ParseResult.ts.md b/docs/modules/ParseResult.ts.md index aeae944db..030341420 100644 --- a/docs/modules/ParseResult.ts.md +++ b/docs/modules/ParseResult.ts.md @@ -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) @@ -54,7 +54,7 @@ Added in v1.0.0 ```ts export declare const fail: ( - error: ParseError | ParseErrors | ReadonlyArray.NonEmptyReadonlyArray + error: ParseError | ParseIssue | ReadonlyArray.NonEmptyReadonlyArray ) => ParseResult ``` @@ -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 @@ -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 @@ -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 @@ -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** @@ -179,7 +179,7 @@ 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 + readonly errors: ReadonlyArray.NonEmptyReadonlyArray } ``` @@ -187,7 +187,7 @@ 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, @@ -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 + readonly errors: ReadonlyArray.NonEmptyReadonlyArray } ``` @@ -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 @@ -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 + readonly errors: ReadonlyArray.NonEmptyReadonlyArray } ``` @@ -357,7 +357,7 @@ Added in v1.0.0 ```ts export interface ParseError { readonly _tag: "ParseError" - readonly errors: ReadonlyArray.NonEmptyReadonlyArray + readonly errors: ReadonlyArray.NonEmptyReadonlyArray } ``` @@ -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 diff --git a/docs/modules/TreeFormatter.ts.md b/docs/modules/TreeFormatter.ts.md index abf211b43..95ebbb514 100644 --- a/docs/modules/TreeFormatter.ts.md +++ b/docs/modules/TreeFormatter.ts.md @@ -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 diff --git a/src/ArrayFormatter.ts b/src/ArrayFormatter.ts index f1a27e047..e2346ffc3 100644 --- a/src/ArrayFormatter.ts +++ b/src/ArrayFormatter.ts @@ -3,7 +3,7 @@ */ 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" /** @@ -11,12 +11,12 @@ import { formatExpected, getMessage } from "./TreeFormatter.js" * @since 1.0.0 */ export interface Issue { - readonly _tag: ParseErrors["_tag"] + readonly _tag: ParseIssue["_tag"] readonly path: ReadonlyArray readonly message: string } -const format = (self: ParseErrors, path: ReadonlyArray = []): Array => { +const format = (self: ParseIssue, path: ReadonlyArray = []): Array => { const _tag = self._tag switch (_tag) { case "Type": @@ -46,5 +46,5 @@ const format = (self: ParseErrors, path: ReadonlyArray = []): Array * @since 1.0.0 */ export const formatErrors = ( - errors: ReadonlyArray.NonEmptyReadonlyArray + errors: ReadonlyArray.NonEmptyReadonlyArray ): Array => ReadonlyArray.flatMap(errors, (e) => format(e)) diff --git a/src/ParseResult.ts b/src/ParseResult.ts index 02d82f02d..fb1afe652 100644 --- a/src/ParseResult.ts +++ b/src/ParseResult.ts @@ -21,12 +21,12 @@ export interface ParseResult extends Effect.Effect {} */ export interface ParseError { readonly _tag: "ParseError" - readonly errors: ReadonlyArray.NonEmptyReadonlyArray + readonly errors: ReadonlyArray.NonEmptyReadonlyArray } class ParseErrorImpl implements Inspectable.Inspectable { readonly _tag = "ParseError" - constructor(readonly errors: ReadonlyArray.NonEmptyReadonlyArray) {} + constructor(readonly errors: ReadonlyArray.NonEmptyReadonlyArray) {} toString() { return TreeFormatter.formatErrors(this.errors) } @@ -45,7 +45,7 @@ class ParseErrorImpl implements Inspectable.Inspectable { * @since 1.0.0 */ export const parseError = ( - errors: ReadonlyArray.NonEmptyReadonlyArray + errors: ReadonlyArray.NonEmptyReadonlyArray ): ParseError => new ParseErrorImpl(errors) /** @@ -54,7 +54,7 @@ export const parseError = ( * @category model * @since 1.0.0 */ -export type ParseErrors = +export type ParseIssue = | Type | Index | Key @@ -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 @@ -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 @@ -121,7 +121,7 @@ export const forbidden: Forbidden = { export interface Index { readonly _tag: "Index" readonly index: number - readonly errors: ReadonlyArray.NonEmptyReadonlyArray + readonly errors: ReadonlyArray.NonEmptyReadonlyArray } /** @@ -130,11 +130,11 @@ export interface Index { */ export const index = ( index: number, - errors: ReadonlyArray.NonEmptyReadonlyArray + errors: ReadonlyArray.NonEmptyReadonlyArray ): 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, @@ -146,7 +146,7 @@ export const index = ( export interface Key { readonly _tag: "Key" readonly key: PropertyKey - readonly errors: ReadonlyArray.NonEmptyReadonlyArray + readonly errors: ReadonlyArray.NonEmptyReadonlyArray } /** @@ -155,7 +155,7 @@ export interface Key { */ export const key = ( key: PropertyKey, - errors: ReadonlyArray.NonEmptyReadonlyArray + errors: ReadonlyArray.NonEmptyReadonlyArray ): Key => ({ _tag: "Key", key, errors }) /** @@ -201,7 +201,7 @@ export const unexpected = ( */ export interface UnionMember { readonly _tag: "UnionMember" - readonly errors: ReadonlyArray.NonEmptyReadonlyArray + readonly errors: ReadonlyArray.NonEmptyReadonlyArray } /** @@ -209,7 +209,7 @@ export interface UnionMember { * @since 1.0.0 */ export const unionMember = ( - errors: ReadonlyArray.NonEmptyReadonlyArray + errors: ReadonlyArray.NonEmptyReadonlyArray ): UnionMember => ({ _tag: "UnionMember", errors }) /** @@ -236,9 +236,9 @@ export { * @since 1.0.0 */ export const fail = ( - error: ParseError | ParseErrors | ReadonlyArray.NonEmptyReadonlyArray + error: ParseError | ParseIssue | ReadonlyArray.NonEmptyReadonlyArray ): ParseResult => { - const e: any = error + const e = error if ("_tag" in e) { return e._tag === "ParseError" ? Either.left(e) : Either.left(parseError([e])) } diff --git a/src/Parser.ts b/src/Parser.ts index 74f6603ba..df0f5e39c 100644 --- a/src/Parser.ts +++ b/src/Parser.ts @@ -362,7 +362,7 @@ const go = (ast: AST.AST, isDecoding: boolean): Parser => { 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 @@ -603,7 +603,7 @@ const go = (ast: AST.AST, isDecoding: boolean): Parser => { 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 // --------------------------------------------- @@ -796,7 +796,7 @@ const go = (ast: AST.AST, isDecoding: boolean): Parser => { 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 = [] if (len > 0) { diff --git a/src/TreeFormatter.ts b/src/TreeFormatter.ts index 8bb89af9b..22a19939c 100644 --- a/src/TreeFormatter.ts +++ b/src/TreeFormatter.ts @@ -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 extends ReadonlyArray> {} @@ -24,7 +24,7 @@ const make = (value: A, forest: Forest = []): Tree => ({ * @category formatting * @since 1.0.0 */ -export const formatErrors = (errors: NonEmptyReadonlyArray): string => +export const formatErrors = (errors: NonEmptyReadonlyArray): string => drawTree(make(`error(s) found`, errors.map(go))) const drawTree = (tree: Tree): string => tree.value + draw("\n", tree.forest) @@ -135,7 +135,7 @@ export const formatExpected = (ast: AST.AST): string => { } } -const isCollapsible = (es: Forest, errors: NonEmptyReadonlyArray): boolean => +const isCollapsible = (es: Forest, errors: NonEmptyReadonlyArray): boolean => es.length === 1 && es[0].forest.length !== 0 && errors[0]._tag !== "UnionMember" /** @internal */ @@ -148,7 +148,7 @@ export const getMessage = (e: Type) => ) ) -const go = (e: ParseErrors): Tree => { +const go = (e: ParseIssue): Tree => { switch (e._tag) { case "Type": return make(getMessage(e)) diff --git a/test/util.ts b/test/util.ts index 8799415c0..ae8c5e7d9 100644 --- a/test/util.ts +++ b/test/util.ts @@ -254,12 +254,12 @@ export const expectEncodeFailure = async ( } } -export const formatAll = (errors: NonEmptyReadonlyArray): string => +export const formatAll = (errors: NonEmptyReadonlyArray): string => pipe(errors, RA.map(formatDecodeError), RA.join(", ")) const getMessage = AST.getAnnotation>(AST.MessageAnnotationId) -const formatDecodeError = (e: PR.ParseErrors): string => { +const formatDecodeError = (e: PR.ParseIssue): string => { switch (e._tag) { case "Type": return pipe(