mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-02 03:15:52 +08:00
rename derive (#163)
* Literals.mapMembers * Struct.mapFields * Tuple.mapElements * Union.mapMembers * add test helper
This commit is contained in:
+65
-29
@@ -953,6 +953,38 @@ Output:
|
||||
*/
|
||||
```
|
||||
|
||||
## Literals
|
||||
|
||||
### Deriving new literals
|
||||
|
||||
You can map the members of a `Schema.Literals` schema using the `mapMembers` method. The `mapMembers` method accepts a function from `Literals.members` to new members, and returns a new `Schema.Union` based on the result.
|
||||
|
||||
```ts
|
||||
import { Schema, Tuple } from "effect"
|
||||
|
||||
const schema = Schema.Literals(["red", "green", "blue"]).mapMembers(
|
||||
Tuple.evolve([
|
||||
(a) => Schema.Struct({ _tag: a, a: Schema.String }),
|
||||
(b) => Schema.Struct({ _tag: b, b: Schema.Number }),
|
||||
(c) => Schema.Struct({ _tag: c, c: Schema.Boolean })
|
||||
])
|
||||
)
|
||||
|
||||
/*
|
||||
type Type = {
|
||||
readonly _tag: "red";
|
||||
readonly a: string;
|
||||
} | {
|
||||
readonly _tag: "green";
|
||||
readonly b: number;
|
||||
} | {
|
||||
readonly _tag: "blue";
|
||||
readonly c: boolean;
|
||||
}
|
||||
*/
|
||||
type Type = (typeof schema)["Type"]
|
||||
```
|
||||
|
||||
## Structs
|
||||
|
||||
### Optional and Mutable Keys
|
||||
@@ -1450,7 +1482,7 @@ export type Encoded = typeof schema.Encoded
|
||||
|
||||
### Deriving Structs
|
||||
|
||||
You can map the fields of a struct schema using the `map` method on `Schema.Struct`. The `map` method accepts a function from `Struct.Fields` to new fields, and returns a new `Schema.Struct` based on the result.
|
||||
You can map the fields of a struct schema using the `mapFields` static method on `Schema.Struct`. The `mapFields` static method accepts a function from `Struct.Fields` to new fields, and returns a new `Schema.Struct` based on the result.
|
||||
|
||||
This can be used to pick, omit, modify, or extend struct fields.
|
||||
|
||||
@@ -1471,7 +1503,7 @@ const schema: Schema.Struct<{
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number
|
||||
}).derive(Struct.pick(["a"]))
|
||||
}).mapFields(Struct.pick(["a"]))
|
||||
```
|
||||
|
||||
#### Omit
|
||||
@@ -1491,7 +1523,7 @@ const schema: Schema.Struct<{
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number
|
||||
}).derive(Struct.omit(["b"]))
|
||||
}).mapFields(Struct.omit(["b"]))
|
||||
```
|
||||
|
||||
#### Merge
|
||||
@@ -1513,7 +1545,7 @@ const schema: Schema.Struct<{
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number
|
||||
}).derive(
|
||||
}).mapFields(
|
||||
Struct.merge({
|
||||
c: Schema.Boolean
|
||||
})
|
||||
@@ -1532,7 +1564,7 @@ const original = Schema.Struct({
|
||||
b: Schema.String
|
||||
}).check(SchemaCheck.make(({ a, b }) => a === b, { title: "a === b" }))
|
||||
|
||||
const schema = original.derive(Struct.merge({ c: Schema.String }), {
|
||||
const schema = original.mapFields(Struct.merge({ c: Schema.String }), {
|
||||
preserveChecks: true
|
||||
})
|
||||
|
||||
@@ -1568,7 +1600,7 @@ const schema: Schema.Struct<{
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number
|
||||
}).derive(
|
||||
}).mapFields(
|
||||
Struct.evolve({
|
||||
a: (field) => Schema.optionalKey(field)
|
||||
})
|
||||
@@ -1595,7 +1627,7 @@ const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number,
|
||||
c: Schema.Boolean
|
||||
}).derive(Struct.map(Schema.optionalKey))
|
||||
}).mapFields(Struct.map(Schema.optionalKey))
|
||||
```
|
||||
|
||||
#### Mapping a subset of fields at once
|
||||
@@ -1618,7 +1650,7 @@ const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number,
|
||||
c: Schema.Boolean
|
||||
}).derive(Struct.mapPick(["a", "c"], Schema.optionalKey))
|
||||
}).mapFields(Struct.mapPick(["a", "c"], Schema.optionalKey))
|
||||
```
|
||||
|
||||
Or if it's more convenient, you can use `Struct.mapOmit`.
|
||||
@@ -1637,7 +1669,7 @@ const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number,
|
||||
c: Schema.Boolean
|
||||
}).derive(Struct.mapOmit(["b"], Schema.optionalKey))
|
||||
}).mapFields(Struct.mapOmit(["b"], Schema.optionalKey))
|
||||
```
|
||||
|
||||
#### Mapping individual keys
|
||||
@@ -1658,7 +1690,7 @@ const schema: Schema.Struct<{
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number
|
||||
}).derive(
|
||||
}).mapFields(
|
||||
Struct.evolveKeys({
|
||||
a: (key) => String.toUpperCase(key)
|
||||
})
|
||||
@@ -1681,7 +1713,7 @@ const schema: Schema.Struct<{
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number
|
||||
}).derive(
|
||||
}).mapFields(
|
||||
Struct.renameKeys({
|
||||
a: "A"
|
||||
})
|
||||
@@ -1706,7 +1738,7 @@ const schema: Schema.Struct<{
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number
|
||||
}).derive(
|
||||
}).mapFields(
|
||||
Struct.evolveEntries({
|
||||
a: (key, value) => [String.toUpperCase(key), Schema.optionalKey(value)]
|
||||
})
|
||||
@@ -1733,7 +1765,7 @@ const schema: Schema.Struct<{
|
||||
readonly b: Schema.Number;
|
||||
}>
|
||||
*/
|
||||
const schema = A.derive(
|
||||
const schema = A.mapFields(
|
||||
Struct.evolve({
|
||||
a: (field) => Schema.optionalKey(field)
|
||||
})
|
||||
@@ -2151,7 +2183,7 @@ readonly [string]
|
||||
|
||||
### Deriving Tuples
|
||||
|
||||
You can map the elements of a tuple schema using the `map` method on `Schema.Tuple`. The `map` method accepts a function from `Tuple.elements` to new elements, and returns a new `Schema.Tuple` based on the result.
|
||||
You can map the elements of a tuple schema using the `mapElements` static method on `Schema.Tuple`. The `mapElements` static method accepts a function from `Tuple.elements` to new elements, and returns a new `Schema.Tuple` based on the result.
|
||||
|
||||
#### Pick
|
||||
|
||||
@@ -2169,7 +2201,7 @@ const schema = Schema.Tuple([
|
||||
Schema.String,
|
||||
Schema.Number,
|
||||
Schema.Boolean
|
||||
]).derive(Tuple.pick([0, 2]))
|
||||
]).mapElements(Tuple.pick([0, 2]))
|
||||
```
|
||||
|
||||
#### Omit
|
||||
@@ -2188,7 +2220,7 @@ const schema = Schema.Tuple([
|
||||
Schema.String,
|
||||
Schema.Number,
|
||||
Schema.Boolean
|
||||
]).derive(Tuple.omit([1]))
|
||||
]).mapElements(Tuple.omit([1]))
|
||||
```
|
||||
|
||||
#### Adding Elements
|
||||
@@ -2210,8 +2242,8 @@ const schema: Schema.Tuple<readonly [
|
||||
]>
|
||||
*/
|
||||
const schema = Schema.Tuple([Schema.String, Schema.Number])
|
||||
.derive(Tuple.appendElement(Schema.Boolean)) // adds a single element
|
||||
.derive(Tuple.appendElements([Schema.String, Schema.Number])) // adds multiple elements
|
||||
.mapElements(Tuple.appendElement(Schema.Boolean)) // adds a single element
|
||||
.mapElements(Tuple.appendElements([Schema.String, Schema.Number])) // adds multiple elements
|
||||
```
|
||||
|
||||
#### Mapping individual elements
|
||||
@@ -2234,7 +2266,7 @@ const schema = Schema.Tuple([
|
||||
Schema.String,
|
||||
Schema.Number,
|
||||
Schema.Boolean
|
||||
]).derive(
|
||||
]).mapElements(
|
||||
Tuple.evolve([
|
||||
(v) => Schema.NullOr(v),
|
||||
undefined, // no change
|
||||
@@ -2263,7 +2295,7 @@ const schema = Schema.Tuple([
|
||||
Schema.String,
|
||||
Schema.Number,
|
||||
Schema.Boolean
|
||||
]).derive(Tuple.map(Schema.NullOr))
|
||||
]).mapElements(Tuple.map(Schema.NullOr))
|
||||
```
|
||||
|
||||
#### Mapping a subset of elements at once
|
||||
@@ -2286,7 +2318,7 @@ const schema = Schema.Tuple([
|
||||
Schema.String,
|
||||
Schema.Number,
|
||||
Schema.Boolean
|
||||
]).derive(Tuple.mapPick([0, 2], Schema.NullOr))
|
||||
]).mapElements(Tuple.mapPick([0, 2], Schema.NullOr))
|
||||
```
|
||||
|
||||
Or if it's more convenient, you can use `Tuple.mapOmit`.
|
||||
@@ -2305,7 +2337,7 @@ const schema = Schema.Tuple([
|
||||
Schema.String,
|
||||
Schema.Number,
|
||||
Schema.Boolean
|
||||
]).derive(Tuple.mapOmit([1], Schema.NullOr))
|
||||
]).mapElements(Tuple.mapOmit([1], Schema.NullOr))
|
||||
```
|
||||
|
||||
#### Renaming Indices
|
||||
@@ -2328,7 +2360,7 @@ const schema = Schema.Tuple([
|
||||
Schema.String,
|
||||
Schema.Number,
|
||||
Schema.Boolean
|
||||
]).derive(
|
||||
]).mapElements(
|
||||
Tuple.renameIndices(["1", "0"]) // flip the first and second elements
|
||||
)
|
||||
```
|
||||
@@ -2349,7 +2381,7 @@ const schema = Schema.Tuple([
|
||||
Schema.String,
|
||||
Schema.Number,
|
||||
Schema.Boolean
|
||||
]).derive(
|
||||
]).mapElements(
|
||||
Tuple.renameIndices([
|
||||
"2", // last element becomes first
|
||||
"1", // second element keeps its index
|
||||
@@ -2886,7 +2918,7 @@ Expected exactly one successful result for { readonly "a": string } ⊻ { readon
|
||||
|
||||
### Deriving Unions
|
||||
|
||||
You can map the members of a union schema using the `map` method on `Schema.Union`. The `map` method accepts a function from `Union.members` to new members, and returns a new `Schema.Union` based on the result.
|
||||
You can map the members of a union schema using the `mapMembers` static method on `Schema.Union`. The `mapMembers` static method accepts a function from `Union.members` to new members, and returns a new `Schema.Union` based on the result.
|
||||
|
||||
#### Adding Members
|
||||
|
||||
@@ -2907,8 +2939,8 @@ const schema: Schema.Union<readonly [
|
||||
]>
|
||||
*/
|
||||
const schema = Schema.Union([Schema.String, Schema.Number])
|
||||
.derive(Tuple.appendElement(Schema.Boolean)) // adds a single member
|
||||
.derive(Tuple.appendElements([Schema.String, Schema.Number])) // adds multiple members
|
||||
.mapMembers(Tuple.appendElement(Schema.Boolean)) // adds a single member
|
||||
.mapMembers(Tuple.appendElements([Schema.String, Schema.Number])) // adds multiple members
|
||||
```
|
||||
|
||||
#### Mapping individual members
|
||||
@@ -2931,7 +2963,7 @@ const schema = Schema.Union([
|
||||
Schema.String,
|
||||
Schema.Number,
|
||||
Schema.Boolean
|
||||
]).derive(
|
||||
]).mapMembers(
|
||||
Tuple.evolve([
|
||||
(v) => Schema.Array(v),
|
||||
undefined, // no change
|
||||
@@ -2960,7 +2992,7 @@ const schema = Schema.Union([
|
||||
Schema.String,
|
||||
Schema.Number,
|
||||
Schema.Boolean
|
||||
]).derive(Tuple.map(Schema.Array))
|
||||
]).mapMembers(Tuple.map(Schema.Array))
|
||||
```
|
||||
|
||||
## Transformations Redesign
|
||||
@@ -3826,7 +3858,11 @@ import { Schema } from "effect"
|
||||
|
||||
const schema = Schema.Literals(["red", "green", "blue"])
|
||||
|
||||
// readonly ["red", "green", "blue"]
|
||||
schema.literals
|
||||
|
||||
// readonly [Schema.Literal<"red">, Schema.Literal<"green">, Schema.Literal<"blue">]
|
||||
schema.members
|
||||
```
|
||||
|
||||
### Strings
|
||||
|
||||
@@ -943,22 +943,6 @@ describe("Schema", () => {
|
||||
>()
|
||||
})
|
||||
|
||||
describe("merge", () => {
|
||||
it("Struct", () => {
|
||||
const schema = Schema.Struct({ a: Schema.String }).derive(Struct.merge({ b: Schema.String }))
|
||||
expect(schema).type.toBe<Schema.Struct<{ readonly a: Schema.String; readonly b: Schema.String }>>()
|
||||
})
|
||||
|
||||
it("overlapping fields", () => {
|
||||
const schema = Schema.Struct({ a: Schema.String, b: Schema.String }).derive(
|
||||
Struct.merge({ b: Schema.Number, c: Schema.Number })
|
||||
)
|
||||
expect(schema).type.toBe<
|
||||
Schema.Struct<{ readonly a: Schema.String; readonly b: Schema.Number; readonly c: Schema.Number }>
|
||||
>()
|
||||
})
|
||||
})
|
||||
|
||||
describe("passthrough", () => {
|
||||
it("E = T", () => {
|
||||
Schema.String.pipe(
|
||||
@@ -1662,12 +1646,28 @@ describe("Schema", () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe("Struct.derive", () => {
|
||||
describe("Struct.mapFields", () => {
|
||||
describe("merge", () => {
|
||||
it("non-overlapping fields", () => {
|
||||
const schema = Schema.Struct({ a: Schema.String }).mapFields(Struct.merge({ b: Schema.String }))
|
||||
expect(schema).type.toBe<Schema.Struct<{ readonly a: Schema.String; readonly b: Schema.String }>>()
|
||||
})
|
||||
|
||||
it("overlapping fields", () => {
|
||||
const schema = Schema.Struct({ a: Schema.String, b: Schema.String }).mapFields(
|
||||
Struct.merge({ b: Schema.Number, c: Schema.Number })
|
||||
)
|
||||
expect(schema).type.toBe<
|
||||
Schema.Struct<{ readonly a: Schema.String; readonly b: Schema.Number; readonly c: Schema.Number }>
|
||||
>()
|
||||
})
|
||||
})
|
||||
|
||||
it("evolve", () => {
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number
|
||||
}).derive(Struct.evolve({ a: (v) => Schema.optionalKey(v) }))
|
||||
}).mapFields(Struct.evolve({ a: (v) => Schema.optionalKey(v) }))
|
||||
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<
|
||||
@@ -1686,7 +1686,7 @@ describe("Schema", () => {
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number
|
||||
}).derive(Struct.evolveKeys({ a: (k) => Str.toUpperCase(k) }))
|
||||
}).mapFields(Struct.evolveKeys({ a: (k) => Str.toUpperCase(k) }))
|
||||
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<
|
||||
@@ -1706,7 +1706,7 @@ describe("Schema", () => {
|
||||
a: Schema.String,
|
||||
b: Schema.Number,
|
||||
c: Schema.Boolean
|
||||
}).derive(Struct.renameKeys({ a: "A", b: "B" }))
|
||||
}).mapFields(Struct.renameKeys({ a: "A", b: "B" }))
|
||||
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<
|
||||
@@ -1725,7 +1725,7 @@ describe("Schema", () => {
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number
|
||||
}).derive(Struct.evolveEntries({ a: (k, v) => [Str.toUpperCase(k), Schema.optionalKey(v)] }))
|
||||
}).mapFields(Struct.evolveEntries({ a: (k, v) => [Str.toUpperCase(k), Schema.optionalKey(v)] }))
|
||||
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<
|
||||
@@ -1744,7 +1744,7 @@ describe("Schema", () => {
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number
|
||||
}).derive(Struct.map(Schema.optionalKey))
|
||||
}).mapFields(Struct.map(Schema.optionalKey))
|
||||
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<
|
||||
@@ -1763,7 +1763,7 @@ describe("Schema", () => {
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number
|
||||
}).derive(Struct.mapPick(["a"], Schema.optionalKey))
|
||||
}).mapFields(Struct.mapPick(["a"], Schema.optionalKey))
|
||||
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<
|
||||
@@ -1782,7 +1782,7 @@ describe("Schema", () => {
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number
|
||||
}).derive(Struct.mapOmit(["b"], Schema.optionalKey))
|
||||
}).mapFields(Struct.mapOmit(["b"], Schema.optionalKey))
|
||||
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<
|
||||
@@ -1801,7 +1801,7 @@ describe("Schema", () => {
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number
|
||||
}).derive(Struct.map(Schema.optional))
|
||||
}).mapFields(Struct.map(Schema.optional))
|
||||
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<
|
||||
@@ -1820,7 +1820,7 @@ describe("Schema", () => {
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number
|
||||
}).derive(Struct.map(Schema.mutableKey))
|
||||
}).mapFields(Struct.map(Schema.mutableKey))
|
||||
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<{ a: string; b: number }, { a: string; b: number }, never, never>
|
||||
@@ -1834,7 +1834,7 @@ describe("Schema", () => {
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.Array(Schema.String),
|
||||
b: Schema.Tuple([Schema.Number])
|
||||
}).derive(Struct.map(Schema.mutable))
|
||||
}).mapFields(Struct.map(Schema.mutable))
|
||||
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<
|
||||
@@ -1858,8 +1858,8 @@ describe("Schema", () => {
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.Array(Schema.String),
|
||||
b: Schema.Tuple([Schema.Number])
|
||||
}).derive(Struct.map(Schema.mutable))
|
||||
.derive(Struct.map(Schema.readonly))
|
||||
}).mapFields(Struct.map(Schema.mutable))
|
||||
.mapFields(Struct.map(Schema.readonly))
|
||||
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<
|
||||
@@ -1883,7 +1883,7 @@ describe("Schema", () => {
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number
|
||||
}).derive(Struct.map(Schema.NullOr))
|
||||
}).mapFields(Struct.map(Schema.NullOr))
|
||||
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<
|
||||
@@ -1902,7 +1902,7 @@ describe("Schema", () => {
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number
|
||||
}).derive(Struct.map(Schema.UndefinedOr))
|
||||
}).mapFields(Struct.map(Schema.UndefinedOr))
|
||||
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<
|
||||
@@ -1921,7 +1921,7 @@ describe("Schema", () => {
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number
|
||||
}).derive(Struct.map(Schema.NullishOr))
|
||||
}).mapFields(Struct.map(Schema.NullishOr))
|
||||
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<
|
||||
@@ -1940,7 +1940,7 @@ describe("Schema", () => {
|
||||
const schema = Schema.Struct({
|
||||
a: Schema.String,
|
||||
b: Schema.Number
|
||||
}).derive(Struct.map(Schema.Array))
|
||||
}).mapFields(Struct.map(Schema.Array))
|
||||
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<
|
||||
@@ -1961,7 +1961,7 @@ describe("Schema", () => {
|
||||
b: Schema.Number
|
||||
})) {}
|
||||
|
||||
const schema = A.derive(Struct.map(Schema.Array))
|
||||
const schema = A.mapFields(Struct.map(Schema.Array))
|
||||
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<
|
||||
@@ -1981,7 +1981,7 @@ describe("Schema", () => {
|
||||
a: Schema.String,
|
||||
b: Schema.FiniteFromString,
|
||||
c: Schema.Boolean
|
||||
}).derive(flow(
|
||||
}).mapFields(flow(
|
||||
Struct.map(Schema.NullOr),
|
||||
Struct.mapPick(["a", "c"], Schema.mutableKey)
|
||||
))
|
||||
@@ -2006,9 +2006,9 @@ describe("Schema", () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe("Tuple.derive", () => {
|
||||
describe("Tuple.mapElements", () => {
|
||||
it("appendElement", () => {
|
||||
const schema = Schema.Tuple([Schema.String]).derive(Tuple.appendElement(Schema.Number))
|
||||
const schema = Schema.Tuple([Schema.String]).mapElements(Tuple.appendElement(Schema.Number))
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<readonly [string, number], readonly [string, number], never, never>
|
||||
>()
|
||||
@@ -2016,7 +2016,7 @@ describe("Schema", () => {
|
||||
})
|
||||
|
||||
it("appendElements", () => {
|
||||
const schema = Schema.Tuple([Schema.String]).derive(Tuple.appendElements([Schema.Number, Schema.Boolean]))
|
||||
const schema = Schema.Tuple([Schema.String]).mapElements(Tuple.appendElements([Schema.Number, Schema.Boolean]))
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<readonly [string, number, boolean], readonly [string, number, boolean], never, never>
|
||||
>()
|
||||
@@ -2024,7 +2024,7 @@ describe("Schema", () => {
|
||||
})
|
||||
|
||||
it("pick", () => {
|
||||
const schema = Schema.Tuple([Schema.String, Schema.Number, Schema.Boolean]).derive(Tuple.pick([0, 2]))
|
||||
const schema = Schema.Tuple([Schema.String, Schema.Number, Schema.Boolean]).mapElements(Tuple.pick([0, 2]))
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<readonly [string, boolean], readonly [string, boolean], never, never>
|
||||
>()
|
||||
@@ -2032,7 +2032,7 @@ describe("Schema", () => {
|
||||
})
|
||||
|
||||
it("omit", () => {
|
||||
const schema = Schema.Tuple([Schema.String, Schema.Number, Schema.Boolean]).derive(Tuple.omit([1]))
|
||||
const schema = Schema.Tuple([Schema.String, Schema.Number, Schema.Boolean]).mapElements(Tuple.omit([1]))
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<readonly [string, boolean], readonly [string, boolean], never, never>
|
||||
>()
|
||||
@@ -2041,7 +2041,7 @@ describe("Schema", () => {
|
||||
|
||||
describe("evolve", () => {
|
||||
it("readonly [string] -> readonly [string?]", () => {
|
||||
const schema = Schema.Tuple([Schema.String]).derive(Tuple.evolve([(v) => Schema.optionalKey(v)]))
|
||||
const schema = Schema.Tuple([Schema.String]).mapElements(Tuple.evolve([(v) => Schema.optionalKey(v)]))
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<readonly [string?], readonly [string?], never, never>
|
||||
>()
|
||||
@@ -2049,7 +2049,7 @@ describe("Schema", () => {
|
||||
})
|
||||
|
||||
it("readonly [string, number] -> readonly [string, number?]", () => {
|
||||
const schema = Schema.Tuple([Schema.String, Schema.Number]).derive(
|
||||
const schema = Schema.Tuple([Schema.String, Schema.Number]).mapElements(
|
||||
Tuple.evolve([undefined, (v) => Schema.optionalKey(v)])
|
||||
)
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
@@ -2063,7 +2063,7 @@ describe("Schema", () => {
|
||||
|
||||
describe("renameIndices", () => {
|
||||
it("partial index mapping", () => {
|
||||
const schema = Schema.Tuple([Schema.String, Schema.Number, Schema.Boolean]).derive(
|
||||
const schema = Schema.Tuple([Schema.String, Schema.Number, Schema.Boolean]).mapElements(
|
||||
Tuple.renameIndices(["1", "0"])
|
||||
)
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
@@ -2073,7 +2073,7 @@ describe("Schema", () => {
|
||||
})
|
||||
|
||||
it("full index mapping", () => {
|
||||
const schema = Schema.Tuple([Schema.String, Schema.Number, Schema.Boolean]).derive(
|
||||
const schema = Schema.Tuple([Schema.String, Schema.Number, Schema.Boolean]).mapElements(
|
||||
Tuple.renameIndices(["2", "1", "0"])
|
||||
)
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
@@ -2084,7 +2084,7 @@ describe("Schema", () => {
|
||||
})
|
||||
|
||||
it("optionalKey", () => {
|
||||
const schema = Schema.Tuple([Schema.String, Schema.Number]).derive(Tuple.map(Schema.optionalKey))
|
||||
const schema = Schema.Tuple([Schema.String, Schema.Number]).mapElements(Tuple.map(Schema.optionalKey))
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<readonly [string?, number?], readonly [string?, number?], never, never>
|
||||
>()
|
||||
@@ -2094,7 +2094,7 @@ describe("Schema", () => {
|
||||
})
|
||||
|
||||
it("NullOr", () => {
|
||||
const schema = Schema.Tuple([Schema.String, Schema.Number]).derive(Tuple.map(Schema.NullOr))
|
||||
const schema = Schema.Tuple([Schema.String, Schema.Number]).mapElements(Tuple.map(Schema.NullOr))
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<readonly [string | null, number | null], readonly [string | null, number | null], never, never>
|
||||
>()
|
||||
@@ -2104,7 +2104,7 @@ describe("Schema", () => {
|
||||
})
|
||||
|
||||
it("mapPick", () => {
|
||||
const schema = Schema.Tuple([Schema.String, Schema.Number, Schema.Boolean]).derive(
|
||||
const schema = Schema.Tuple([Schema.String, Schema.Number, Schema.Boolean]).mapElements(
|
||||
Tuple.mapPick([0, 2], Schema.NullOr)
|
||||
)
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
@@ -2121,9 +2121,9 @@ describe("Schema", () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe("Union.derive", () => {
|
||||
describe("Union.mapMembers", () => {
|
||||
it("appendElement", () => {
|
||||
const schema = Schema.Union([Schema.String, Schema.Number]).derive(Tuple.appendElement(Schema.Boolean))
|
||||
const schema = Schema.Union([Schema.String, Schema.Number]).mapMembers(Tuple.appendElement(Schema.Boolean))
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<string | number | boolean, string | number | boolean, never, never>
|
||||
>()
|
||||
@@ -2131,7 +2131,7 @@ describe("Schema", () => {
|
||||
})
|
||||
|
||||
it("evolve", () => {
|
||||
const schema = Schema.Union([Schema.String, Schema.Number, Schema.Boolean]).derive(
|
||||
const schema = Schema.Union([Schema.String, Schema.Number, Schema.Boolean]).mapMembers(
|
||||
Tuple.evolve([
|
||||
(v) => Schema.Array(v),
|
||||
undefined,
|
||||
@@ -2152,7 +2152,7 @@ describe("Schema", () => {
|
||||
})
|
||||
|
||||
it("Array", () => {
|
||||
const schema = Schema.Union([Schema.String, Schema.Number]).derive(Tuple.map(Schema.Array))
|
||||
const schema = Schema.Union([Schema.String, Schema.Number]).mapMembers(Tuple.map(Schema.Array))
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<
|
||||
ReadonlyArray<string> | ReadonlyArray<number>,
|
||||
@@ -2165,7 +2165,7 @@ describe("Schema", () => {
|
||||
})
|
||||
|
||||
it("NonEmptyArray", () => {
|
||||
const schema = Schema.Union([Schema.String, Schema.Number]).derive(Tuple.map(Schema.NonEmptyArray))
|
||||
const schema = Schema.Union([Schema.String, Schema.Number]).mapMembers(Tuple.map(Schema.NonEmptyArray))
|
||||
expect(Schema.revealCodec(schema)).type.toBe<
|
||||
Schema.Codec<
|
||||
NonEmptyReadonlyArray<string> | NonEmptyReadonlyArray<number>,
|
||||
@@ -2199,8 +2199,8 @@ describe("Schema", () => {
|
||||
>()
|
||||
})
|
||||
|
||||
it("Literals.map", () => {
|
||||
const schema = Schema.Literals(["a", "b", "c"]).derive(Tuple.evolve([
|
||||
it("Literals.mapMembers", () => {
|
||||
const schema = Schema.Literals(["a", "b", "c"]).mapMembers(Tuple.evolve([
|
||||
(a) => Schema.Struct({ _tag: a, a: Schema.String }),
|
||||
(b) => Schema.Struct({ _tag: b, b: Schema.Number }),
|
||||
(c) => Schema.Struct({ _tag: c, c: Schema.Boolean })
|
||||
|
||||
@@ -1326,7 +1326,7 @@ export interface Struct<Fields extends Struct.Fields> extends
|
||||
* - `preserveChecks` - if `true`, keep any `.check(...)` constraints that
|
||||
* were attached to the original struct. Defaults to `false`.
|
||||
*/
|
||||
derive<To extends Struct.Fields>(
|
||||
mapFields<To extends Struct.Fields>(
|
||||
f: (fields: Fields) => To,
|
||||
options?: {
|
||||
readonly preserveChecks?: boolean | undefined
|
||||
@@ -1341,7 +1341,7 @@ class Struct$<Fields extends Struct.Fields> extends make$<Struct<Fields>> implem
|
||||
// clone to avoid accidental external mutation
|
||||
this.fields = { ...fields }
|
||||
}
|
||||
derive<To extends Struct.Fields>(
|
||||
mapFields<To extends Struct.Fields>(
|
||||
f: (fields: Fields) => To,
|
||||
options?: {
|
||||
readonly preserveChecks?: boolean | undefined
|
||||
@@ -1712,7 +1712,7 @@ export interface Tuple<Elements extends Tuple.Elements> extends
|
||||
* - `preserveChecks` - if `true`, keep any `.check(...)` constraints that
|
||||
* were attached to the original tuple. Defaults to `false`.
|
||||
*/
|
||||
derive<To extends Tuple.Elements>(
|
||||
mapElements<To extends Tuple.Elements>(
|
||||
f: (elements: Elements) => To,
|
||||
options?: {
|
||||
readonly preserveChecks?: boolean | undefined
|
||||
@@ -1728,7 +1728,7 @@ class Tuple$<Elements extends Tuple.Elements> extends make$<Tuple<Elements>> imp
|
||||
this.elements = [...elements] as any
|
||||
}
|
||||
|
||||
derive<To extends Tuple.Elements>(
|
||||
mapElements<To extends Tuple.Elements>(
|
||||
f: (elements: Elements) => To,
|
||||
options?: {
|
||||
readonly preserveChecks?: boolean | undefined
|
||||
@@ -2014,7 +2014,7 @@ export interface Union<Members extends ReadonlyArray<Top>> extends
|
||||
* - `preserveChecks` - if `true`, keep any `.check(...)` constraints that
|
||||
* were attached to the original union. Defaults to `false`.
|
||||
*/
|
||||
derive<To extends ReadonlyArray<Top>>(
|
||||
mapMembers<To extends ReadonlyArray<Top>>(
|
||||
f: (members: Members) => To,
|
||||
options?: {
|
||||
readonly preserveChecks?: boolean | undefined
|
||||
@@ -2027,7 +2027,7 @@ class Union$<Members extends ReadonlyArray<Top>> extends make$<Union<Members>> i
|
||||
super(ast, (ast) => new Union$(ast, members))
|
||||
}
|
||||
|
||||
derive<To extends ReadonlyArray<Top>>(
|
||||
mapMembers<To extends ReadonlyArray<Top>>(
|
||||
f: (members: Members) => To,
|
||||
options?: {
|
||||
readonly preserveChecks?: boolean | undefined
|
||||
@@ -2075,7 +2075,10 @@ export interface Literals<L extends ReadonlyArray<SchemaAST.Literal>> extends
|
||||
{
|
||||
readonly literals: L
|
||||
readonly members: { readonly [K in keyof L]: Literal<L[K]> }
|
||||
derive<To extends ReadonlyArray<Top>>(f: (members: this["members"]) => To): Union<Simplify<Readonly<To>>>
|
||||
/**
|
||||
* Map over the members of the union.
|
||||
*/
|
||||
mapMembers<To extends ReadonlyArray<Top>>(f: (members: this["members"]) => To): Union<Simplify<Readonly<To>>>
|
||||
}
|
||||
|
||||
class Literals$<L extends ReadonlyArray<SchemaAST.Literal>> extends make$<Literals<L>> implements Literals<L> {
|
||||
@@ -2087,7 +2090,7 @@ class Literals$<L extends ReadonlyArray<SchemaAST.Literal>> extends make$<Litera
|
||||
super(ast, (ast) => new Literals$(ast, literals, members))
|
||||
}
|
||||
|
||||
derive<To extends ReadonlyArray<Top>>(f: (members: this["members"]) => To): Union<Simplify<Readonly<To>>> {
|
||||
mapMembers<To extends ReadonlyArray<Top>>(f: (members: this["members"]) => To): Union<Simplify<Readonly<To>>> {
|
||||
return Union(f(this.members))
|
||||
}
|
||||
}
|
||||
|
||||
+193
-217
File diff suppressed because it is too large
Load Diff
@@ -36,6 +36,10 @@ export const assertions = (asserts: {
|
||||
|
||||
const out = {
|
||||
schema: {
|
||||
format: (schema: Schema.Top, expected: string) => {
|
||||
strictEqual(SchemaAST.format(schema.ast), expected)
|
||||
},
|
||||
|
||||
fields: {
|
||||
equals: (a: Schema.Struct.Fields, b: Schema.Struct.Fields) => {
|
||||
deepStrictEqual(Record.map(a, SchemaAST.getAST), Record.map(b, SchemaAST.getAST))
|
||||
|
||||
Reference in New Issue
Block a user