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

Commit

Permalink
remove undefined from optionFromNullable (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti authored May 12, 2023
1 parent 36fe56e commit 1ac3d06
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/healthy-points-care.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/schema": minor
---

remove undefined from optionFromNullable
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,7 @@ import * as O from "@effect/data/Option";
/*
const schema: S.Schema<{
readonly a: string;
readonly b: number | null | undefined;
readonly b: number | null;
}, {
readonly a: string;
readonly b: O.Option<number>;
Expand All @@ -1312,11 +1312,11 @@ const schema = S.struct({

// parsing
const parse = S.parse(schema);
parse({ a: "hello", b: undefined }); // { a: "hello", b: none() }
parse({ a: "hello", b: null }); // { a: "hello", b: none() }
parse({ a: "hello", b: 1 }); // { a: "hello", b: some(1) }

parse({ a: "hello" }); // throws key "b" is missing
parse({ a: "hello", b: undefined }); // throws
parse({ a: "hello" }); // throws (key "b" is missing)

// encoding
const encodeOrThrow = S.encode(schema);
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/Schema.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -1626,7 +1626,7 @@ Added in v1.0.0
**Signature**

```ts
export declare const optionFromNullable: <I, A>(value: Schema<I, A>) => Schema<I | null | undefined, Option<A>>
export declare const optionFromNullable: <I, A>(value: Schema<I, A>) => Schema<I | null, Option<A>>
```

Added in v1.0.0
Expand Down
10 changes: 10 additions & 0 deletions dtslint/ts4.9/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,16 @@ S.optionFromSelf(S.number)
// $ExpectType Schema<Option<string>, Option<number>>
S.optionFromSelf(NumberFromString)

// ---------------------------------------------
// optionFromNullable
// ---------------------------------------------

// $ExpectType Schema<number | null, Option<number>>
S.optionFromNullable(S.number)

// $ExpectType Schema<string | null, Option<number>>
S.optionFromNullable(NumberFromString)

// ---------------------------------------------
// instanceOf
// ---------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2250,8 +2250,8 @@ export const option = <I, A>(
*/
export const optionFromNullable = <I, A>(
value: Schema<I, A>
): Schema<I | null | undefined, Option<A>> =>
transform(union(_undefined, _null, value), to(optionFromSelf(value)), O.fromNullable, O.getOrNull)
): Schema<I | null, Option<A>> =>
transform(nullable(value), to(optionFromSelf(value)), O.fromNullable, O.getOrNull)

// ---------------------------------------------
// data/ReadonlyArray
Expand Down
8 changes: 6 additions & 2 deletions test/data/Option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,21 @@ describe.concurrent("Option", () => {

it("Decoder", async () => {
const schema = S.optionFromNullable(NumberFromString)
await Util.expectParseSuccess(schema, undefined, O.none())
await Util.expectParseSuccess(schema, null, O.none())
await Util.expectParseSuccess(schema, "1", O.some(1))

expect(O.isOption(S.decode(schema)(null))).toEqual(true)
expect(O.isOption(S.decode(schema)("1"))).toEqual(true)

await Util.expectParseFailure(
schema,
undefined,
`union member: Expected null, actual undefined, union member: Expected string, actual undefined`
)
await Util.expectParseFailure(
schema,
{},
`union member: Expected undefined, actual {}, union member: Expected null, actual {}, union member: Expected string, actual {}`
`union member: Expected null, actual {}, union member: Expected string, actual {}`
)
})

Expand Down

0 comments on commit 1ac3d06

Please sign in to comment.