From 1ac3d06c90dc952b0beff9a722cfaace5162bb21 Mon Sep 17 00:00:00 2001 From: Giulio Canti Date: Fri, 12 May 2023 09:38:26 +0200 Subject: [PATCH] remove undefined from optionFromNullable (#277) --- .changeset/healthy-points-care.md | 5 +++++ README.md | 6 +++--- docs/modules/Schema.ts.md | 2 +- dtslint/ts4.9/Schema.ts | 10 ++++++++++ src/Schema.ts | 4 ++-- test/data/Option.ts | 8 ++++++-- 6 files changed, 27 insertions(+), 8 deletions(-) create mode 100644 .changeset/healthy-points-care.md diff --git a/.changeset/healthy-points-care.md b/.changeset/healthy-points-care.md new file mode 100644 index 000000000..f6cf45bcb --- /dev/null +++ b/.changeset/healthy-points-care.md @@ -0,0 +1,5 @@ +--- +"@effect/schema": minor +--- + +remove undefined from optionFromNullable diff --git a/README.md b/README.md index 289bc969a..6e634b032 100644 --- a/README.md +++ b/README.md @@ -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; @@ -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); diff --git a/docs/modules/Schema.ts.md b/docs/modules/Schema.ts.md index d56156edc..a9414d448 100644 --- a/docs/modules/Schema.ts.md +++ b/docs/modules/Schema.ts.md @@ -1626,7 +1626,7 @@ Added in v1.0.0 **Signature** ```ts -export declare const optionFromNullable: (value: Schema) => Schema> +export declare const optionFromNullable: (value: Schema) => Schema> ``` Added in v1.0.0 diff --git a/dtslint/ts4.9/Schema.ts b/dtslint/ts4.9/Schema.ts index de3fc6c1d..606132d8e 100644 --- a/dtslint/ts4.9/Schema.ts +++ b/dtslint/ts4.9/Schema.ts @@ -441,6 +441,16 @@ S.optionFromSelf(S.number) // $ExpectType Schema, Option> S.optionFromSelf(NumberFromString) +// --------------------------------------------- +// optionFromNullable +// --------------------------------------------- + +// $ExpectType Schema> +S.optionFromNullable(S.number) + +// $ExpectType Schema> +S.optionFromNullable(NumberFromString) + // --------------------------------------------- // instanceOf // --------------------------------------------- diff --git a/src/Schema.ts b/src/Schema.ts index 5f88f933b..3e962d87b 100644 --- a/src/Schema.ts +++ b/src/Schema.ts @@ -2250,8 +2250,8 @@ export const option = ( */ export const optionFromNullable = ( value: Schema -): Schema> => - transform(union(_undefined, _null, value), to(optionFromSelf(value)), O.fromNullable, O.getOrNull) +): Schema> => + transform(nullable(value), to(optionFromSelf(value)), O.fromNullable, O.getOrNull) // --------------------------------------------- // data/ReadonlyArray diff --git a/test/data/Option.ts b/test/data/Option.ts index f2b0fb918..b3e3d0ac5 100644 --- a/test/data/Option.ts +++ b/test/data/Option.ts @@ -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 {}` ) })