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

Commit

Permalink
replace toOption: true with as: "Option"
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Dec 15, 2023
1 parent 0da600c commit 12041b4
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .changeset/four-suits-fetch.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1552,14 +1552,14 @@ S.mutable(S.struct({ a: S.string, b: S.number }));

### Optional fields as `Option`s

| Combinator | From | To |
| ---------- | ----------------------------------------------------------------- | ------------------------------------------------------------------- |
| `optional` | `Schema<I, A>`, `{ toOption: true }` | `PropertySignature<I \| undefined, true, Option<A>, false>` |
| `optional` | `Schema<I, A>`, `{ exact: true, toOption: true }` | `PropertySignature<I, true, Option<A>, false>` |
| `optional` | `Schema<I, A>`, `{ nullable: true, toOption: true }` | `PropertySignature<I \| undefined \| null, true, Option<A>, false>` |
| `optional` | `Schema<I, A>`, `{ exact: true, nullable: true, toOption: true }` | `PropertySignature<I \| null, true, Option<A>, false>` |
| Combinator | From | To |
| ---------- | --------------------------------------------------------------- | ------------------------------------------------------------------- |
| `optional` | `Schema<I, A>`, `{ as: "Option" }` | `PropertySignature<I \| undefined, true, Option<A>, false>` |
| `optional` | `Schema<I, A>`, `{ exact: true, as: "Option" }` | `PropertySignature<I, true, Option<A>, false>` |
| `optional` | `Schema<I, A>`, `{ nullable: true, as: "Option" }` | `PropertySignature<I \| undefined \| null, true, Option<A>, false>` |
| `optional` | `Schema<I, A>`, `{ exact: true, nullable: true, as: "Option" }` | `PropertySignature<I \| null, true, Option<A>, false>` |

#### optional(schema, { toOption: true })
#### optional(schema, { as: "Option" })

- decoding
- `<missing value>` -> `Option.none()`
Expand All @@ -1569,7 +1569,7 @@ S.mutable(S.struct({ a: S.string, b: S.number }));
- `Option.none()` -> `<missing value>`
- `Option.some(a)` -> `i`

#### optional(schema, { exact: true, toOption: true })
#### optional(schema, { exact: true, as: "Option" })

- decoding
- `<missing value>` -> `Option.none()`
Expand All @@ -1578,7 +1578,7 @@ S.mutable(S.struct({ a: S.string, b: S.number }));
- `Option.none()` -> `<missing value>`
- `Option.some(a)` -> `i`

#### optional(schema, { nullable: true, toOption: true })
#### optional(schema, { nullable: true, as: "Option" })

- decoding
- `<missing value>` -> `Option.none()`
Expand All @@ -1589,7 +1589,7 @@ S.mutable(S.struct({ a: S.string, b: S.number }));
- `Option.none()` -> `<missing value>`
- `Option.some(a)` -> `i`

#### optional(schema, { exact: true, nullable: true, toOption: true })
#### optional(schema, { exact: true, nullable: true, as: "Option" })

- decoding
- `<missing value>` -> `Option.none()`
Expand Down
8 changes: 4 additions & 4 deletions docs/modules/Schema.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -4555,11 +4555,11 @@ export declare const optional: {
): PropertySignature<I, true, A, false>
<I, A>(
schema: Schema<I, A>,
options: { readonly exact: true; readonly nullable: true; readonly toOption: true }
options: { readonly exact: true; readonly nullable: true; readonly as: "Option" }
): PropertySignature<I | null, true, Option.Option<A>, false>
<I, A>(
schema: Schema<I, A>,
options: { readonly exact: true; readonly toOption: true }
options: { readonly exact: true; readonly as: "Option" }
): PropertySignature<I, true, Option.Option<A>, false>
<I, A>(schema: Schema<I, A>, options: { readonly exact: true }): PropertySignature<I, true, A, true>
<I, A>(
Expand All @@ -4569,11 +4569,11 @@ export declare const optional: {
<I, A>(schema: Schema<I, A>, options: { readonly default: () => A }): PropertySignature<I | undefined, true, A, false>
<I, A>(
schema: Schema<I, A>,
options: { readonly nullable: true; readonly toOption: true }
options: { readonly nullable: true; readonly as: "Option" }
): PropertySignature<I | null | undefined, true, Option.Option<A>, false>
<I, A>(
schema: Schema<I, A>,
options: { readonly toOption: true }
options: { readonly as: "Option" }
): PropertySignature<I | undefined, true, Option.Option<A>, false>
<I, A>(schema: Schema<I, A>): PropertySignature<I | undefined, true, A | undefined, true>
}
Expand Down
22 changes: 11 additions & 11 deletions dtslint/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<boolean>; }>
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<number>; }>
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<boolean>; }>
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<number>; }>
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<number>; }>
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<number>; }>
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
Expand Down Expand Up @@ -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 [email protected]
// // $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
Expand Down
16 changes: 8 additions & 8 deletions src/Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,11 +757,11 @@ export const optional: {
): PropertySignature<I, true, A, false>
<I, A>(
schema: Schema<I, A>,
options: { readonly exact: true; readonly nullable: true; readonly toOption: true }
options: { readonly exact: true; readonly nullable: true; readonly as: "Option" }
): PropertySignature<I | null, true, Option.Option<A>, false>
<I, A>(
schema: Schema<I, A>,
options: { readonly exact: true; readonly toOption: true }
options: { readonly exact: true; readonly as: "Option" }
): PropertySignature<I, true, Option.Option<A>, false>
<I, A>(
schema: Schema<I, A>,
Expand All @@ -777,11 +777,11 @@ export const optional: {
): PropertySignature<I | undefined, true, A, false>
<I, A>(
schema: Schema<I, A>,
options: { readonly nullable: true; readonly toOption: true }
options: { readonly nullable: true; readonly as: "Option" }
): PropertySignature<I | undefined | null, true, Option.Option<A>, false>
<I, A>(
schema: Schema<I, A>,
options: { readonly toOption: true }
options: { readonly as: "Option" }
): PropertySignature<I | undefined, true, Option.Option<A>, false>
<I, A>(schema: Schema<I, A>): PropertySignature<I | undefined, true, A | undefined, true>
} = <I, A>(
Expand All @@ -790,13 +790,13 @@ export const optional: {
readonly exact?: true
readonly default?: () => A
readonly nullable?: true
readonly toOption?: true
readonly as?: "Option"
}
): PropertySignature<any, any, any, any> => {
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) {
Expand All @@ -816,7 +816,7 @@ export const optional: {
)
}
} else {
if (toOption) {
if (asOption) {
if (isNullable) {
return optionalToRequired(
nullable(schema),
Expand Down Expand Up @@ -857,7 +857,7 @@ export const optional: {
)
}
} else {
if (toOption) {
if (asOption) {
if (isNullable) {
return optionalToRequired(
nullish(schema),
Expand Down
4 changes: 2 additions & 2 deletions test/Schema/Class.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class PersonWithNick extends PersonWithAge.extend<PersonWithNick>()({
class PersonWithTransform extends Person.transform<PersonWithTransform>()(
{
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({
Expand All @@ -70,7 +70,7 @@ class PersonWithTransform extends Person.transform<PersonWithTransform>()(
class PersonWithTransformFrom extends Person.transformFrom<PersonWithTransformFrom>()(
{
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({
Expand Down
16 changes: 8 additions & 8 deletions test/Schema/optional.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) })
Expand All @@ -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() })
Expand All @@ -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) })
Expand All @@ -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() })
Expand Down

0 comments on commit 12041b4

Please sign in to comment.