From 12041b4bfbe718126f3ceb42c88dcf1d09b98c42 Mon Sep 17 00:00:00 2001 From: gcanti Date: Fri, 15 Dec 2023 17:05:01 +0100 Subject: [PATCH] replace `toOption: true` with `as: "Option"` --- .changeset/four-suits-fetch.md | 2 +- README.md | 20 ++++++++++---------- docs/modules/Schema.ts.md | 8 ++++---- dtslint/Schema.ts | 22 +++++++++++----------- src/Schema.ts | 16 ++++++++-------- test/Schema/Class.test.ts | 4 ++-- test/Schema/optional.test.ts | 16 ++++++++-------- 7 files changed, 44 insertions(+), 44 deletions(-) diff --git a/.changeset/four-suits-fetch.md b/.changeset/four-suits-fetch.md index d5f8e7cf4..f870b33ee 100644 --- a/.changeset/four-suits-fetch.md +++ b/.changeset/four-suits-fetch.md @@ -8,4 +8,4 @@ Upgrade Guide: - `S.optional(schema, { exact: true })` replaces the old `S.optional(schema)` - `S.optional(schema, { exact: true, default: () => A })` replaces the old `S.optional(schema).withDefault(() => A)` -- `S.optional(schema, { exact: true, toOption: true })` replaces the old `S.optional(schema).toOption()` +- `S.optional(schema, { exact: true, as: "Option" })` replaces the old `S.optional(schema).toOption()` diff --git a/README.md b/README.md index 4a0010ace..e77e5a4ae 100644 --- a/README.md +++ b/README.md @@ -1552,14 +1552,14 @@ S.mutable(S.struct({ a: S.string, b: S.number })); ### Optional fields as `Option`s -| Combinator | From | To | -| ---------- | ----------------------------------------------------------------- | ------------------------------------------------------------------- | -| `optional` | `Schema`, `{ toOption: true }` | `PropertySignature, false>` | -| `optional` | `Schema`, `{ exact: true, toOption: true }` | `PropertySignature, false>` | -| `optional` | `Schema`, `{ nullable: true, toOption: true }` | `PropertySignature, false>` | -| `optional` | `Schema`, `{ exact: true, nullable: true, toOption: true }` | `PropertySignature, false>` | +| Combinator | From | To | +| ---------- | --------------------------------------------------------------- | ------------------------------------------------------------------- | +| `optional` | `Schema`, `{ as: "Option" }` | `PropertySignature, false>` | +| `optional` | `Schema`, `{ exact: true, as: "Option" }` | `PropertySignature, false>` | +| `optional` | `Schema`, `{ nullable: true, as: "Option" }` | `PropertySignature, false>` | +| `optional` | `Schema`, `{ exact: true, nullable: true, as: "Option" }` | `PropertySignature, false>` | -#### optional(schema, { toOption: true }) +#### optional(schema, { as: "Option" }) - decoding - `` -> `Option.none()` @@ -1569,7 +1569,7 @@ S.mutable(S.struct({ a: S.string, b: S.number })); - `Option.none()` -> `` - `Option.some(a)` -> `i` -#### optional(schema, { exact: true, toOption: true }) +#### optional(schema, { exact: true, as: "Option" }) - decoding - `` -> `Option.none()` @@ -1578,7 +1578,7 @@ S.mutable(S.struct({ a: S.string, b: S.number })); - `Option.none()` -> `` - `Option.some(a)` -> `i` -#### optional(schema, { nullable: true, toOption: true }) +#### optional(schema, { nullable: true, as: "Option" }) - decoding - `` -> `Option.none()` @@ -1589,7 +1589,7 @@ S.mutable(S.struct({ a: S.string, b: S.number })); - `Option.none()` -> `` - `Option.some(a)` -> `i` -#### optional(schema, { exact: true, nullable: true, toOption: true }) +#### optional(schema, { exact: true, nullable: true, as: "Option" }) - decoding - `` -> `Option.none()` diff --git a/docs/modules/Schema.ts.md b/docs/modules/Schema.ts.md index 4f228294f..1ed331549 100644 --- a/docs/modules/Schema.ts.md +++ b/docs/modules/Schema.ts.md @@ -4555,11 +4555,11 @@ export declare const optional: { ): PropertySignature ( schema: Schema, - options: { readonly exact: true; readonly nullable: true; readonly toOption: true } + options: { readonly exact: true; readonly nullable: true; readonly as: "Option" } ): PropertySignature, false> ( schema: Schema, - options: { readonly exact: true; readonly toOption: true } + options: { readonly exact: true; readonly as: "Option" } ): PropertySignature, false> (schema: Schema, options: { readonly exact: true }): PropertySignature ( @@ -4569,11 +4569,11 @@ export declare const optional: { (schema: Schema, options: { readonly default: () => A }): PropertySignature ( schema: Schema, - options: { readonly nullable: true; readonly toOption: true } + options: { readonly nullable: true; readonly as: "Option" } ): PropertySignature, false> ( schema: Schema, - options: { readonly toOption: true } + options: { readonly as: "Option" } ): PropertySignature, false> (schema: Schema): PropertySignature } diff --git a/dtslint/Schema.ts b/dtslint/Schema.ts index 2a5406ac0..59af16a2b 100644 --- a/dtslint/Schema.ts +++ b/dtslint/Schema.ts @@ -307,38 +307,38 @@ S.struct({ a: S.optional(S.NumberFromString, { nullable: true, default: () => 0 S.struct({ a: S.optional(S.NumberFromString, { exact: true, nullable: true, default: () => 0 }) }) // --------------------------------------------- -// optional { exact: true, toOption: true } +// optional { exact: true, as: "Option" } // --------------------------------------------- // $ExpectType Schema<{ readonly a: string; readonly b: number; readonly c?: boolean; }, { readonly a: string; readonly b: number; readonly c: Option; }> -S.struct({ a: S.string, b: S.number, c: S.optional(S.boolean, { exact: true, toOption: true }) }) +S.struct({ a: S.string, b: S.number, c: S.optional(S.boolean, { exact: true, as: "Option" }) }) // $ExpectType Schema<{ readonly a: string; readonly b: number; readonly c?: string; }, { readonly a: string; readonly b: number; readonly c: Option; }> S.struct({ a: S.string, b: S.number, - c: S.optional(S.NumberFromString, { exact: true, toOption: true }) + c: S.optional(S.NumberFromString, { exact: true, as: "Option" }) }) // --------------------------------------------- -// optional { toOption: true } +// optional { as: "Option" } // --------------------------------------------- // $ExpectType Schema<{ readonly a: string; readonly b: number; readonly c?: boolean | undefined; }, { readonly a: string; readonly b: number; readonly c: Option; }> -S.struct({ a: S.string, b: S.number, c: S.optional(S.boolean, { toOption: true }) }) +S.struct({ a: S.string, b: S.number, c: S.optional(S.boolean, { as: "Option" }) }) // $ExpectType Schema<{ readonly a: string; readonly b: number; readonly c?: string | undefined; }, { readonly a: string; readonly b: number; readonly c: Option; }> -S.struct({ a: S.string, b: S.number, c: S.optional(S.NumberFromString, { toOption: true }) }) +S.struct({ a: S.string, b: S.number, c: S.optional(S.NumberFromString, { as: "Option" }) }) // --------------------------------------------- -// optional { nullable: true, toOption: true } +// optional { nullable: true, as: "Option" } // --------------------------------------------- // $ExpectType Schema<{ readonly a?: string | null | undefined; }, { readonly a: Option; }> -S.struct({ a: S.optional(S.NumberFromString, { nullable: true, toOption: true }) }) +S.struct({ a: S.optional(S.NumberFromString, { nullable: true, as: "Option" }) }) // $ExpectType Schema<{ readonly a?: string | null; }, { readonly a: Option; }> -S.struct({ a: S.optional(S.NumberFromString, { exact: true, nullable: true, toOption: true }) }) +S.struct({ a: S.optional(S.NumberFromString, { exact: true, nullable: true, as: "Option" }) }) // --------------------------------------------- // pick @@ -489,13 +489,13 @@ pipe( // $ExpectType Schema<{ readonly a: string; readonly b: string; readonly c: string; }, { readonly a: string; readonly b: string; readonly c: string; }> S.extend(S.struct({ a: S.string, b: S.string }), S.struct({ c: S.string })) -// TODO: wait for ts `readonly` fix +// rises an error in TypeScript@5.0 // // $ExpectType Schema<{ readonly [x: string]: string; readonly a: string; readonly b: string; readonly c: string; }, { readonly [x: string]: string; readonly a: string; readonly b: string; readonly c: string; }> // pipe( // S.struct({ a: S.string, b: S.string }), // S.extend(S.struct({ c: S.string })), // S.extend(S.record(S.string, S.string)) -// ); +// ) // --------------------------------------------- // suspend diff --git a/src/Schema.ts b/src/Schema.ts index f22d45b0a..40e375b7f 100644 --- a/src/Schema.ts +++ b/src/Schema.ts @@ -757,11 +757,11 @@ export const optional: { ): PropertySignature ( schema: Schema, - options: { readonly exact: true; readonly nullable: true; readonly toOption: true } + options: { readonly exact: true; readonly nullable: true; readonly as: "Option" } ): PropertySignature, false> ( schema: Schema, - options: { readonly exact: true; readonly toOption: true } + options: { readonly exact: true; readonly as: "Option" } ): PropertySignature, false> ( schema: Schema, @@ -777,11 +777,11 @@ export const optional: { ): PropertySignature ( schema: Schema, - options: { readonly nullable: true; readonly toOption: true } + options: { readonly nullable: true; readonly as: "Option" } ): PropertySignature, false> ( schema: Schema, - options: { readonly toOption: true } + options: { readonly as: "Option" } ): PropertySignature, false> (schema: Schema): PropertySignature } = ( @@ -790,13 +790,13 @@ export const optional: { readonly exact?: true readonly default?: () => A readonly nullable?: true - readonly toOption?: true + readonly as?: "Option" } ): PropertySignature => { const isExact = options?.exact const value = options?.default const isNullable = options?.nullable - const toOption = options?.toOption + const asOption = options?.as == "Option" if (isExact) { if (value) { @@ -816,7 +816,7 @@ export const optional: { ) } } else { - if (toOption) { + if (asOption) { if (isNullable) { return optionalToRequired( nullable(schema), @@ -857,7 +857,7 @@ export const optional: { ) } } else { - if (toOption) { + if (asOption) { if (isNullable) { return optionalToRequired( nullish(schema), diff --git a/test/Schema/Class.test.ts b/test/Schema/Class.test.ts index 697f11e4a..7ff3e94fc 100644 --- a/test/Schema/Class.test.ts +++ b/test/Schema/Class.test.ts @@ -52,7 +52,7 @@ class PersonWithNick extends PersonWithAge.extend()({ class PersonWithTransform extends Person.transform()( { id: S.string, - thing: S.optional(S.struct({ id: S.number }), { exact: true, toOption: true }) + thing: S.optional(S.struct({ id: S.number }), { exact: true, as: "Option" }) }, (input) => PR.succeed({ @@ -70,7 +70,7 @@ class PersonWithTransform extends Person.transform()( class PersonWithTransformFrom extends Person.transformFrom()( { id: S.string, - thing: S.optional(S.struct({ id: S.number }), { exact: true, toOption: true }) + thing: S.optional(S.struct({ id: S.number }), { exact: true, as: "Option" }) }, (input) => PR.succeed({ diff --git a/test/Schema/optional.test.ts b/test/Schema/optional.test.ts index 0fbcc0d4b..9283bc05a 100644 --- a/test/Schema/optional.test.ts +++ b/test/Schema/optional.test.ts @@ -48,10 +48,10 @@ describe("optional APIs", () => { }) }) - describe("optional > { exact: true, toOption: true }", () => { + describe("optional > { exact: true, as: \"Option\" }", () => { it("decoding / encoding", async () => { const schema = S.struct({ - a: S.optional(S.NumberFromString, { exact: true, toOption: true }) + a: S.optional(S.NumberFromString, { exact: true, as: "Option" }) }) await Util.expectParseSuccess(schema, {}, { a: O.none() }) await Util.expectParseSuccess(schema, { a: "1" }, { a: O.some(1) }) @@ -64,10 +64,10 @@ describe("optional APIs", () => { }) }) - describe("optionalToOption > { exact: true, nullable: true, toOption: true }", () => { + describe("optionalToOption > { exact: true, nullable: true, as: \"Option\" }", () => { it("decoding / encoding", async () => { const schema = S.struct({ - a: S.optional(S.NumberFromString, { exact: true, nullable: true, toOption: true }) + a: S.optional(S.NumberFromString, { exact: true, nullable: true, as: "Option" }) }) await Util.expectParseSuccess(schema, {}, { a: O.none() }) await Util.expectParseSuccess(schema, { a: null }, { a: O.none() }) @@ -85,9 +85,9 @@ describe("optional APIs", () => { }) }) - describe("optional > { toOption: true }", () => { + describe("optional > { as: \"Option\" }", () => { it("decoding / encoding", async () => { - const schema = S.struct({ a: S.optional(S.NumberFromString, { toOption: true }) }) + const schema = S.struct({ a: S.optional(S.NumberFromString, { as: "Option" }) }) await Util.expectParseSuccess(schema, {}, { a: O.none() }) await Util.expectParseSuccess(schema, { a: undefined }, { a: O.none() }) await Util.expectParseSuccess(schema, { a: "1" }, { a: O.some(1) }) @@ -104,10 +104,10 @@ describe("optional APIs", () => { }) }) - describe("optional > { nullable: true, toOption: true }", () => { + describe("optional > { nullable: true, as: \"Option\" }", () => { it("decoding / encoding", async () => { const schema = S.struct({ - a: S.optional(S.NumberFromString, { nullable: true, toOption: true }) + a: S.optional(S.NumberFromString, { nullable: true, as: "Option" }) }) await Util.expectParseSuccess(schema, {}, { a: O.none() }) await Util.expectParseSuccess(schema, { a: undefined }, { a: O.none() })