-
-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Schema: add head / headOrFail (#1928)
- Loading branch information
Showing
4 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@effect/schema": patch | ||
--- | ||
|
||
add head / headOrFail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import * as S from "@effect/schema/Schema" | ||
import * as Util from "@effect/schema/test/util" | ||
import * as Option from "effect/Option" | ||
import { describe, it } from "vitest" | ||
|
||
describe("ReadonlyArray > head", () => { | ||
it("decoding", async () => { | ||
const schema = S.head(S.number) | ||
await Util.expectParseSuccess(schema, [], Option.none()) | ||
await Util.expectParseSuccess(schema, [1], Option.some(1)) | ||
await Util.expectParseFailure( | ||
schema, | ||
["a"], | ||
`(ReadonlyArray<number> <-> Option<number>) | ||
└─ From side transformation failure | ||
└─ ReadonlyArray<number> | ||
└─ [0] | ||
└─ Expected a number, actual "a"` | ||
) | ||
}) | ||
|
||
it("encoding", async () => { | ||
const schema = S.head(S.number) | ||
await Util.expectEncodeSuccess(schema, Option.none(), []) | ||
await Util.expectEncodeSuccess(schema, Option.some(1), [1]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import * as S from "@effect/schema/Schema" | ||
import * as Util from "@effect/schema/test/util" | ||
import { describe, it } from "vitest" | ||
|
||
describe("ReadonlyArray > headOrFail", () => { | ||
it("decoding", async () => { | ||
const schema = S.headOrFail(S.number) | ||
await Util.expectParseSuccess(schema, [1], 1) | ||
await Util.expectParseFailure( | ||
schema, | ||
[], | ||
`(ReadonlyArray<number> <-> number) | ||
└─ Transformation process failure | ||
└─ Expected (ReadonlyArray<number> <-> number), actual []` | ||
) | ||
await Util.expectParseFailure( | ||
schema, | ||
["a"], | ||
`(ReadonlyArray<number> <-> number) | ||
└─ From side transformation failure | ||
└─ ReadonlyArray<number> | ||
└─ [0] | ||
└─ Expected a number, actual "a"` | ||
) | ||
}) | ||
|
||
it("encoding", async () => { | ||
const schema = S.headOrFail(S.number) | ||
await Util.expectEncodeSuccess(schema, 1, [1]) | ||
}) | ||
}) |