Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
add Transform issue and add Format module
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Dec 30, 2023
1 parent 58756b0 commit 7ab46bc
Show file tree
Hide file tree
Showing 71 changed files with 1,280 additions and 1,637 deletions.
9 changes: 5 additions & 4 deletions .changeset/gentle-readers-call.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

Refactor `ParseResult` module:

- replace `UnionMember` with `Union`
- add `Union` issue, and replace `UnionMember` with `Union`
- add `Tuple` issue, and replace `Index` with `Tuple`
- add `TypeLiteral` issue
- add `Transform` issue
- add `ast` field to `Member`
- rename `UnionMember` to `Member`
- ast `ast` field to `Member`
- replace `Index` with `Tuple`
- `Unexpected`: rename `ast` to `expected` and make the field required
- add `TypeLiteral`
5 changes: 5 additions & 0 deletions .changeset/rotten-flowers-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": patch
---

add Format module
52 changes: 52 additions & 0 deletions docs/modules/Format.ts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
title: Format.ts
nav_order: 5
parent: Modules
---

## Format overview

Added in v1.0.0

---

<h2 class="text-delta">Table of contents</h2>

- [formatting](#formatting)
- [format](#format)
- [formatAST](#formatast)
- [formatSchema](#formatschema)

---

# formatting

## format

**Signature**

```ts
export declare const format: (u: unknown) => string
```
Added in v1.0.0
## formatAST
**Signature**
```ts
export declare const formatAST: (ast: AST.AST) => string
```
Added in v1.0.0
## formatSchema
**Signature**
```ts
export declare const formatSchema: <I, A>(schema: Schema.Schema<I, A>) => string
```
Added in v1.0.0
2 changes: 1 addition & 1 deletion docs/modules/JSONSchema.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: JSONSchema.ts
nav_order: 6
nav_order: 7
parent: Modules
---

Expand Down
52 changes: 48 additions & 4 deletions docs/modules/ParseResult.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: ParseResult.ts
nav_order: 8
nav_order: 9
parent: Modules
---

Expand All @@ -20,6 +20,7 @@ Added in v1.0.0
- [member](#member)
- [missing](#missing)
- [succeed](#succeed)
- [transform](#transform)
- [try](#try)
- [tuple](#tuple)
- [type](#type)
Expand All @@ -33,6 +34,7 @@ Added in v1.0.0
- [Member (interface)](#member-interface)
- [Missing (interface)](#missing-interface)
- [ParseIssue (type alias)](#parseissue-type-alias)
- [Transform (interface)](#transform-interface)
- [Tuple (interface)](#tuple-interface)
- [Type (interface)](#type-interface)
- [TypeLiteral (interface)](#typeliteral-interface)
Expand Down Expand Up @@ -126,6 +128,21 @@ export declare const succeed: <A>(a: A) => ParseResult<A>
Added in v1.0.0
## transform
**Signature**
```ts
export declare const transform: (
ast: AST.Transform,
actual: unknown,
kind: "From" | "Transformation" | "To",
errors: readonly [ParseIssue, ...ParseIssue[]]
) => Transform
```
Added in v1.0.0
## try
**Signature**
Expand All @@ -141,7 +158,7 @@ Added in v1.0.0
**Signature**
```ts
export declare const tuple: (ast: AST.Tuple, errors: readonly [Index, ...Index[]]) => Tuple
export declare const tuple: (ast: AST.Tuple, actual: unknown, errors: readonly [Index, ...Index[]]) => Tuple
```
Added in v1.0.0
Expand All @@ -161,7 +178,11 @@ Added in v1.0.0
**Signature**
```ts
export declare const typeLiteral: (ast: AST.TypeLiteral, errors: readonly [Key, ...Key[]]) => TypeLiteral
export declare const typeLiteral: (
ast: AST.TypeLiteral,
actual: unknown,
errors: readonly [Key, ...Key[]]
) => TypeLiteral
```
Added in v1.0.0
Expand All @@ -183,6 +204,7 @@ Added in v1.0.0
```ts
export declare const union: (
ast: AST.Union,
actual: unknown,
errors: readonly [Key | Type | Member, ...(Key | Type | Member)[]]
) => Union
```
Expand Down Expand Up @@ -284,9 +306,10 @@ Added in v1.0.0
export type ParseIssue =
// context
| Tuple
| Key
| TypeLiteral
| Union
| Key
| Transform
// primitives
| Type
| Missing
Expand All @@ -296,6 +319,24 @@ export type ParseIssue =
Added in v1.0.0
## Transform (interface)
Error that occurs when a transformation has an error.
**Signature**
```ts
export interface Transform {
readonly _tag: "Transform"
readonly ast: AST.Transform
readonly actual: unknown
readonly kind: "From" | "Transformation" | "To"
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<ParseIssue>
}
```

Added in v1.0.0

## Tuple (interface)

**Signature**
Expand All @@ -304,6 +345,7 @@ Added in v1.0.0
export interface Tuple {
readonly _tag: "Tuple"
readonly ast: AST.Tuple
readonly actual: unknown
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<Index>
}
```
Expand Down Expand Up @@ -339,6 +381,7 @@ Added in v1.0.0
export interface TypeLiteral {
readonly _tag: "TypeLiteral"
readonly ast: AST.TypeLiteral
readonly actual: unknown
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<Key>
}
```
Expand Down Expand Up @@ -370,6 +413,7 @@ Error that occurs when a union has an error.
export interface Union {
readonly _tag: "Union"
readonly ast: AST.Union
readonly actual: unknown
readonly errors: ReadonlyArray.NonEmptyReadonlyArray<Member | Key | Type>
}
```
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/Parser.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Parser.ts
nav_order: 7
nav_order: 8
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/Pretty.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Pretty.ts
nav_order: 9
nav_order: 10
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/Schema.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Schema.ts
nav_order: 10
nav_order: 11
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/Serializable.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Serializable.ts
nav_order: 11
nav_order: 12
parent: Modules
---

Expand Down
2 changes: 1 addition & 1 deletion docs/modules/TreeFormatter.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: TreeFormatter.ts
nav_order: 12
nav_order: 13
parent: Modules
---

Expand Down
15 changes: 14 additions & 1 deletion docs/modules/index.ts.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: index.ts
nav_order: 5
nav_order: 6
parent: Modules
---

Expand All @@ -17,6 +17,7 @@ Added in v1.0.0
- [From "./Arbitrary.js"](#from-arbitraryjs)
- [From "./ArrayFormatter.js"](#from-arrayformatterjs)
- [From "./Equivalence.js"](#from-equivalencejs)
- [From "./Format.js"](#from-formatjs)
- [From "./JSONSchema.js"](#from-jsonschemajs)
- [From "./ParseResult.js"](#from-parseresultjs)
- [From "./Parser.js"](#from-parserjs)
Expand Down Expand Up @@ -77,6 +78,18 @@ export * as Equivalence from "./Equivalence.js"

Added in v1.0.0

## From "./Format.js"

Re-exports all named exports from the "./Format.js" module as `Format`.

**Signature**

```ts
export * as Format from "./Format.js"
```

Added in v1.0.0

## From "./JSONSchema.js"

Re-exports all named exports from the "./JSONSchema.js" module as `JSONSchema`.
Expand Down
8 changes: 5 additions & 3 deletions src/ArrayFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
* @since 1.0.0
*/
import * as ReadonlyArray from "effect/ReadonlyArray"
import * as Format from "./Format.js"
import type { ParseIssue } from "./ParseResult.js"
import { formatAST, getMessage } from "./TreeFormatter.js"
import { formatMessage } from "./TreeFormatter.js"

/**
* @category model
Expand All @@ -19,7 +20,7 @@ const format = (self: ParseIssue, path: ReadonlyArray<PropertyKey> = []): Array<
const _tag = self._tag
switch (_tag) {
case "Type":
return [{ _tag, path, message: getMessage(self) }]
return [{ _tag, path, message: formatMessage(self) }]
case "Key":
return ReadonlyArray.flatMap(self.errors, (e) => format(e, [...path, self.key]))
case "Missing":
Expand All @@ -30,7 +31,7 @@ const format = (self: ParseIssue, path: ReadonlyArray<PropertyKey> = []): Array<
return [{
_tag,
path,
message: `Unexpected, expected ${formatAST(self.expected)}`
message: `Unexpected, expected ${Format.formatAST(self.expected)}`
}]
case "Union":
return ReadonlyArray.flatMap(self.errors, (e) => {
Expand All @@ -53,6 +54,7 @@ const format = (self: ParseIssue, path: ReadonlyArray<PropertyKey> = []): Array<
(key) => ReadonlyArray.flatMap(key.errors, (e) => format(e, [...path, key.key]))
)
}
return ReadonlyArray.flatMap(self.errors, (e) => format(e, path))
}

/**
Expand Down
Loading

0 comments on commit 7ab46bc

Please sign in to comment.