effect-app 4.0.0-beta.21 → 4.0.0-beta.211
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +954 -0
- package/dist/Array.d.ts +1 -1
- package/dist/Chunk.d.ts +1 -1
- package/dist/Chunk.d.ts.map +1 -1
- package/dist/Config/SecretURL.d.ts +1 -1
- package/dist/Config/SecretURL.d.ts.map +1 -1
- package/dist/Config/SecretURL.js +2 -2
- package/dist/Config/internal/configSecretURL.d.ts +1 -1
- package/dist/Config/internal/configSecretURL.d.ts.map +1 -1
- package/dist/Config.d.ts +7 -0
- package/dist/Config.d.ts.map +1 -0
- package/dist/Config.js +6 -0
- package/dist/ConfigProvider.d.ts +39 -0
- package/dist/ConfigProvider.d.ts.map +1 -0
- package/dist/ConfigProvider.js +42 -0
- package/dist/Context.d.ts +40 -0
- package/dist/Context.d.ts.map +1 -0
- package/dist/Context.js +67 -0
- package/dist/Effect.d.ts +9 -10
- package/dist/Effect.d.ts.map +1 -1
- package/dist/Effect.js +3 -6
- package/dist/Function.d.ts +1 -1
- package/dist/Function.d.ts.map +1 -1
- package/dist/Inputify.type.d.ts +1 -1
- package/dist/Layer.d.ts +7 -6
- package/dist/Layer.d.ts.map +1 -1
- package/dist/Layer.js +1 -1
- package/dist/NonEmptySet.d.ts +1 -1
- package/dist/NonEmptySet.d.ts.map +1 -1
- package/dist/Option.d.ts +1 -1
- package/dist/Option.d.ts.map +1 -1
- package/dist/Pure.d.ts +5 -5
- package/dist/Pure.d.ts.map +1 -1
- package/dist/Pure.js +13 -13
- package/dist/Schema/Class.d.ts +66 -20
- package/dist/Schema/Class.d.ts.map +1 -1
- package/dist/Schema/Class.js +189 -22
- package/dist/Schema/FastCheck.d.ts +1 -1
- package/dist/Schema/FastCheck.d.ts.map +1 -1
- package/dist/Schema/Methods.d.ts +1 -1
- package/dist/Schema/SchemaParser.d.ts +5 -0
- package/dist/Schema/SchemaParser.d.ts.map +1 -0
- package/dist/Schema/SchemaParser.js +6 -0
- package/dist/Schema/SpecialJsonSchema.d.ts +33 -0
- package/dist/Schema/SpecialJsonSchema.d.ts.map +1 -0
- package/dist/Schema/SpecialJsonSchema.js +122 -0
- package/dist/Schema/SpecialOpenApi.d.ts +32 -0
- package/dist/Schema/SpecialOpenApi.d.ts.map +1 -0
- package/dist/Schema/SpecialOpenApi.js +123 -0
- package/dist/Schema/brand.d.ts +4 -2
- package/dist/Schema/brand.d.ts.map +1 -1
- package/dist/Schema/brand.js +1 -1
- package/dist/Schema/email.d.ts +1 -1
- package/dist/Schema/email.d.ts.map +1 -1
- package/dist/Schema/email.js +7 -4
- package/dist/Schema/ext.d.ts +117 -45
- package/dist/Schema/ext.d.ts.map +1 -1
- package/dist/Schema/ext.js +131 -42
- package/dist/Schema/moreStrings.d.ts +37 -25
- package/dist/Schema/moreStrings.d.ts.map +1 -1
- package/dist/Schema/moreStrings.js +15 -16
- package/dist/Schema/numbers.d.ts +15 -15
- package/dist/Schema/numbers.d.ts.map +1 -1
- package/dist/Schema/numbers.js +10 -12
- package/dist/Schema/phoneNumber.d.ts +1 -1
- package/dist/Schema/phoneNumber.d.ts.map +1 -1
- package/dist/Schema/phoneNumber.js +6 -3
- package/dist/Schema/schema.d.ts +1 -1
- package/dist/Schema/strings.d.ts +5 -5
- package/dist/Schema/strings.d.ts.map +1 -1
- package/dist/Schema/strings.js +1 -5
- package/dist/Schema.d.ts +147 -15
- package/dist/Schema.d.ts.map +1 -1
- package/dist/Schema.js +131 -16
- package/dist/Set.d.ts +1 -1
- package/dist/Set.d.ts.map +1 -1
- package/dist/TypeTest.d.ts +1 -1
- package/dist/Types.d.ts +1 -1
- package/dist/Widen.type.d.ts +1 -1
- package/dist/_ext/Array.d.ts +1 -1
- package/dist/_ext/Array.d.ts.map +1 -1
- package/dist/_ext/date.d.ts +1 -1
- package/dist/_ext/misc.d.ts +1 -1
- package/dist/_ext/ord.ext.d.ts +1 -1
- package/dist/_ext/ord.ext.d.ts.map +1 -1
- package/dist/builtin.d.ts +1 -1
- package/dist/builtin.d.ts.map +1 -1
- package/dist/client/InvalidationKeys.d.ts +29 -0
- package/dist/client/InvalidationKeys.d.ts.map +1 -0
- package/dist/client/InvalidationKeys.js +33 -0
- package/dist/client/apiClientFactory.d.ts +20 -32
- package/dist/client/apiClientFactory.d.ts.map +1 -1
- package/dist/client/apiClientFactory.js +95 -32
- package/dist/client/clientFor.d.ts +51 -17
- package/dist/client/clientFor.d.ts.map +1 -1
- package/dist/client/clientFor.js +9 -1
- package/dist/client/errors.d.ts +49 -25
- package/dist/client/errors.d.ts.map +1 -1
- package/dist/client/errors.js +43 -17
- package/dist/client/makeClient.d.ts +481 -33
- package/dist/client/makeClient.d.ts.map +1 -1
- package/dist/client/makeClient.js +66 -24
- package/dist/client.d.ts +2 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +2 -1
- package/dist/faker.d.ts +1 -1
- package/dist/faker.d.ts.map +1 -1
- package/dist/http/Request.d.ts +2 -2
- package/dist/http/Request.d.ts.map +1 -1
- package/dist/http/internal/lib.d.ts +1 -1
- package/dist/http.d.ts +1 -1
- package/dist/ids.d.ts +12 -12
- package/dist/ids.d.ts.map +1 -1
- package/dist/ids.js +3 -2
- package/dist/index.d.ts +5 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -8
- package/dist/logger.d.ts +1 -1
- package/dist/middleware.d.ts +14 -8
- package/dist/middleware.d.ts.map +1 -1
- package/dist/middleware.js +14 -8
- package/dist/rpc/Invalidation.d.ts +402 -0
- package/dist/rpc/Invalidation.d.ts.map +1 -0
- package/dist/rpc/Invalidation.js +150 -0
- package/dist/rpc/MiddlewareMaker.d.ts +5 -4
- package/dist/rpc/MiddlewareMaker.d.ts.map +1 -1
- package/dist/rpc/MiddlewareMaker.js +57 -37
- package/dist/rpc/RpcContextMap.d.ts +3 -3
- package/dist/rpc/RpcContextMap.d.ts.map +1 -1
- package/dist/rpc/RpcContextMap.js +4 -4
- package/dist/rpc/RpcMiddleware.d.ts +5 -4
- package/dist/rpc/RpcMiddleware.d.ts.map +1 -1
- package/dist/rpc/RpcMiddleware.js +1 -1
- package/dist/rpc.d.ts +2 -2
- package/dist/rpc.d.ts.map +1 -1
- package/dist/rpc.js +2 -2
- package/dist/transform.d.ts +1 -1
- package/dist/transform.d.ts.map +1 -1
- package/dist/transform.js +3 -3
- package/dist/utils/effectify.d.ts +1 -1
- package/dist/utils/extend.d.ts +1 -1
- package/dist/utils/extend.d.ts.map +1 -1
- package/dist/utils/gen.d.ts +2 -2
- package/dist/utils/gen.d.ts.map +1 -1
- package/dist/utils/logLevel.d.ts +2 -2
- package/dist/utils/logLevel.d.ts.map +1 -1
- package/dist/utils/logger.d.ts +3 -3
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +3 -3
- package/dist/utils.d.ts +31 -38
- package/dist/utils.d.ts.map +1 -1
- package/dist/utils.js +12 -25
- package/dist/validation/validators.d.ts +1 -1
- package/dist/validation/validators.d.ts.map +1 -1
- package/dist/validation.d.ts +1 -1
- package/dist/validation.d.ts.map +1 -1
- package/package.json +46 -24
- package/src/Config/SecretURL.ts +2 -1
- package/src/Config.ts +14 -0
- package/src/ConfigProvider.ts +48 -0
- package/src/{ServiceMap.ts → Context.ts} +52 -59
- package/src/Effect.ts +12 -14
- package/src/Layer.ts +6 -5
- package/src/Pure.ts +17 -18
- package/src/Schema/Class.ts +268 -62
- package/src/Schema/SchemaParser.ts +12 -0
- package/src/Schema/SpecialJsonSchema.ts +137 -0
- package/src/Schema/SpecialOpenApi.ts +130 -0
- package/src/Schema/brand.ts +21 -1
- package/src/Schema/email.ts +7 -2
- package/src/Schema/ext.ts +204 -72
- package/src/Schema/moreStrings.ts +40 -37
- package/src/Schema/numbers.ts +14 -16
- package/src/Schema/phoneNumber.ts +5 -1
- package/src/Schema/strings.ts +4 -8
- package/src/Schema.ts +314 -20
- package/src/client/InvalidationKeys.ts +50 -0
- package/src/client/apiClientFactory.ts +223 -129
- package/src/client/clientFor.ts +95 -29
- package/src/client/errors.ts +52 -26
- package/src/client/makeClient.ts +572 -71
- package/src/client.ts +1 -0
- package/src/ids.ts +3 -2
- package/src/index.ts +5 -10
- package/src/middleware.ts +13 -9
- package/src/rpc/Invalidation.ts +226 -0
- package/src/rpc/MiddlewareMaker.ts +65 -60
- package/src/rpc/README.md +2 -2
- package/src/rpc/RpcContextMap.ts +6 -5
- package/src/rpc/RpcMiddleware.ts +5 -4
- package/src/rpc.ts +1 -1
- package/src/transform.ts +2 -2
- package/src/utils/gen.ts +1 -1
- package/src/utils/logger.ts +2 -2
- package/src/utils.ts +50 -132
- package/test/dist/rpc.test.d.ts.map +1 -1
- package/test/dist/secretURL.test.d.ts.map +1 -0
- package/test/dist/special.test.d.ts.map +1 -0
- package/test/dist/stream-error.types.d.ts +2 -0
- package/test/dist/stream-error.types.d.ts.map +1 -0
- package/test/dist/stream-error.types.js +27 -0
- package/test/rpc.test.ts +45 -6
- package/test/schema.test.ts +581 -7
- package/test/secretURL.test.ts +157 -0
- package/test/special.test.ts +1023 -0
- package/test/utils.test.ts +6 -6
- package/tsconfig.base.json +3 -4
- package/tsconfig.json +0 -1
- package/tsconfig.json.bak +2 -2
- package/tsconfig.src.json +29 -29
- package/tsconfig.test.json +2 -2
- package/dist/Operations.d.ts +0 -123
- package/dist/Operations.d.ts.map +0 -1
- package/dist/Operations.js +0 -29
- package/dist/ServiceMap.d.ts +0 -44
- package/dist/ServiceMap.d.ts.map +0 -1
- package/dist/ServiceMap.js +0 -91
- package/eslint.config.mjs +0 -26
- package/src/Operations.ts +0 -55
package/test/schema.test.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// import { generateFromArbitrary } from "@effect-app/infra/test"
|
|
2
2
|
import { Array, S } from "effect-app"
|
|
3
|
-
import {
|
|
3
|
+
import { specialJsonSchemaDocument } from "effect-app/Schema/SpecialJsonSchema"
|
|
4
|
+
import { describe, expect, expectTypeOf, test } from "vitest"
|
|
4
5
|
|
|
5
6
|
const A = S.Struct({ a: S.NonEmptyString255, email: S.NullOr(S.Email) })
|
|
6
7
|
test("works", () => {
|
|
@@ -14,22 +15,127 @@ test("works", () => {
|
|
|
14
15
|
})
|
|
15
16
|
|
|
16
17
|
test("literal default works", () => {
|
|
17
|
-
const l = S.
|
|
18
|
+
const l = S.Literals(["a", "b"])
|
|
18
19
|
expect(l.Default).toBe("a")
|
|
20
|
+
expectTypeOf(l.Default).toEqualTypeOf<"a">()
|
|
19
21
|
const s = S.Struct({ l: l.withDefault })
|
|
20
|
-
expect(s.
|
|
22
|
+
expect(s.make({}).l).toBe("a")
|
|
21
23
|
|
|
22
24
|
const l2 = l.changeDefault("b")
|
|
23
25
|
const s2 = S.Struct({ l: l2.withDefault })
|
|
24
|
-
expect(s2.
|
|
26
|
+
expect(s2.make({}).l).toBe("b")
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
test("NonEmptyString255.Type uses the named brand alias", () => {
|
|
30
|
+
type A = typeof S.NonEmptyString255.Type
|
|
31
|
+
type B = string & S.NonEmptyString255Brand
|
|
32
|
+
expectTypeOf<A>().toEqualTypeOf<B>()
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
test("Opaque accepts an explicit Encoded type", () => {
|
|
36
|
+
interface User {
|
|
37
|
+
readonly id: string
|
|
38
|
+
readonly _tag: "User"
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
interface UserEncoded {
|
|
42
|
+
readonly id: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const baseSchema = S.Struct({ id: S.String, name: S.String })
|
|
46
|
+
const _UserSchema = S.Opaque<User, UserEncoded>()(baseSchema)
|
|
47
|
+
|
|
48
|
+
expectTypeOf<S.Codec.Encoded<typeof _UserSchema>>().toEqualTypeOf<UserEncoded>()
|
|
49
|
+
expectTypeOf<S.Schema.Type<typeof _UserSchema>>().toEqualTypeOf<User>()
|
|
50
|
+
expectTypeOf<S.Codec.Encoded<typeof _UserSchema>>().not.toEqualTypeOf<S.Codec.Encoded<typeof baseSchema>>()
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
test("Opaque with one generic keeps the base encoded shape", () => {
|
|
54
|
+
interface User {
|
|
55
|
+
readonly id: string
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const baseSchema = S.Struct({ id: S.String })
|
|
59
|
+
const _UserSchema = S.Opaque<User>()(baseSchema)
|
|
60
|
+
|
|
61
|
+
expectTypeOf<S.Codec.Encoded<typeof _UserSchema>>().toEqualTypeOf<{ readonly id: string }>()
|
|
62
|
+
expectTypeOf<S.Schema.Type<typeof _UserSchema>>().toEqualTypeOf<User>()
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
test("Opaque preserves optional Struct.make input", () => {
|
|
66
|
+
interface User {
|
|
67
|
+
readonly a?: string | undefined
|
|
68
|
+
readonly b?: number | undefined
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const schema = S.Opaque<User>()(S.Struct({
|
|
72
|
+
a: S.optional(S.String),
|
|
73
|
+
b: S.optional(S.Number)
|
|
74
|
+
}))
|
|
75
|
+
|
|
76
|
+
const made = schema.make()
|
|
77
|
+
expect(made).toEqual({})
|
|
78
|
+
expectTypeOf(made).toEqualTypeOf<User>()
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
test("Opaque preserves optional TaggedStruct.make input", () => {
|
|
82
|
+
interface OnlyTag {
|
|
83
|
+
readonly _tag: "OnlyTag"
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const schema = S.Opaque<OnlyTag>()(S.TaggedStruct("OnlyTag", {}))
|
|
87
|
+
|
|
88
|
+
const made = schema.make()
|
|
89
|
+
expect(made).toEqual({ _tag: "OnlyTag" })
|
|
90
|
+
expectTypeOf(made).toEqualTypeOf<OnlyTag>()
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
test("S.Literals([\"A\", \"B\"]).Default is typed as \"A\"", () => {
|
|
94
|
+
const l = S.Literals(["A", "B"])
|
|
95
|
+
expect(l.Default).toBe("A")
|
|
96
|
+
expectTypeOf(l.Default).toEqualTypeOf<"A">()
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
test("Struct.make accepts void when all fields are optional", () => {
|
|
100
|
+
const schema = S.Struct({
|
|
101
|
+
a: S.optional(S.String),
|
|
102
|
+
b: S.optional(S.Number)
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
const made = schema.make()
|
|
106
|
+
expect(made).toEqual({})
|
|
107
|
+
expectTypeOf(made).toEqualTypeOf<{ readonly a?: string | undefined; readonly b?: number | undefined }>()
|
|
108
|
+
})
|
|
109
|
+
|
|
110
|
+
test("StructNestedEncoded resolves Struct and from.Encoded", () => {
|
|
111
|
+
const plain = S.Struct({ a: S.String, b: S.NullOr(S.String) })
|
|
112
|
+
const encodedKeys = plain.pipe(S.encodeKeys({ b: "b_encoded" }))
|
|
113
|
+
|
|
114
|
+
expectTypeOf<S.StructNestedEncoded<typeof plain>>().toEqualTypeOf<{
|
|
115
|
+
readonly a: string
|
|
116
|
+
readonly b: string | null
|
|
117
|
+
}>()
|
|
118
|
+
|
|
119
|
+
expectTypeOf<S.StructNestedEncoded<typeof encodedKeys>>().toEqualTypeOf<{
|
|
120
|
+
readonly a: string
|
|
121
|
+
readonly b_encoded: string | null
|
|
122
|
+
}>()
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
test("TaggedStruct.make accepts void when only constructor-default fields exist", () => {
|
|
126
|
+
const schema = S.TaggedStruct("OnlyTag", {})
|
|
127
|
+
|
|
128
|
+
const made = schema.make()
|
|
129
|
+
expect(made).toEqual({ _tag: "OnlyTag" })
|
|
130
|
+
expectTypeOf(made).toEqualTypeOf<{ readonly _tag: "OnlyTag" }>()
|
|
25
131
|
})
|
|
26
132
|
|
|
27
133
|
test("tagged union derives tag map and tags from v4 literal ast", () => {
|
|
28
|
-
const schema = S.TaggedUnion(
|
|
134
|
+
const schema = S.TaggedUnion([
|
|
29
135
|
S.TaggedStruct("A", { a: S.String }),
|
|
30
|
-
S.TaggedStruct("B", { b: S.
|
|
136
|
+
S.TaggedStruct("B", { b: S.Finite }),
|
|
31
137
|
S.TaggedStruct("C", { c: S.Boolean })
|
|
32
|
-
)
|
|
138
|
+
])
|
|
33
139
|
const caseA = schema.cases["A"]
|
|
34
140
|
const caseB = schema.cases["B"]
|
|
35
141
|
const caseC = schema.cases["C"]
|
|
@@ -54,3 +160,471 @@ test("tagged union derives tag map and tags from v4 literal ast", () => {
|
|
|
54
160
|
expect(isAOrB({ _tag: "B", b: 1 })).toBe(true)
|
|
55
161
|
expect(isAOrB({ _tag: "C", c: true })).toBe(false)
|
|
56
162
|
})
|
|
163
|
+
|
|
164
|
+
test("TaggedUnion tags returns a Literals schema with correct literal values", () => {
|
|
165
|
+
const schema = S.TaggedUnion([
|
|
166
|
+
S.TaggedStruct("X", { x: S.String }),
|
|
167
|
+
S.TaggedStruct("Y", { y: S.Finite })
|
|
168
|
+
])
|
|
169
|
+
|
|
170
|
+
expect(schema.tags.literals).toEqual(["X", "Y"])
|
|
171
|
+
expectTypeOf(schema.tags.literals).toMatchTypeOf<readonly ["X", "Y"]>()
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
test("TaggedUnion tags.pick returns a subset of the tag literals", () => {
|
|
175
|
+
const schema = S.TaggedUnion([
|
|
176
|
+
S.TaggedStruct("A", { a: S.String }),
|
|
177
|
+
S.TaggedStruct("B", { b: S.Finite }),
|
|
178
|
+
S.TaggedStruct("C", { c: S.Boolean })
|
|
179
|
+
])
|
|
180
|
+
|
|
181
|
+
const subset = schema.tags.pick(["A", "C"])
|
|
182
|
+
expect(subset.literals).toEqual(["A", "C"])
|
|
183
|
+
expect(S.decodeSync(subset)("A")).toBe("A")
|
|
184
|
+
expect(S.decodeSync(subset)("C")).toBe("C")
|
|
185
|
+
expect(() => S.decodeUnknownSync(subset)("B")).toThrow()
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
test("tags standalone function extracts tags from member schemas", () => {
|
|
189
|
+
const members = [
|
|
190
|
+
S.TaggedStruct("Foo", { foo: S.String }),
|
|
191
|
+
S.TaggedStruct("Bar", { bar: S.Finite })
|
|
192
|
+
] as const
|
|
193
|
+
|
|
194
|
+
const tagSchema = S.tags(members)
|
|
195
|
+
expect(tagSchema.literals).toEqual(["Foo", "Bar"])
|
|
196
|
+
expect(S.decodeSync(tagSchema)("Foo")).toBe("Foo")
|
|
197
|
+
expect(S.decodeSync(tagSchema)("Bar")).toBe("Bar")
|
|
198
|
+
expect(() => S.decodeUnknownSync(tagSchema)("Baz")).toThrow()
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
test("ExtendTaggedUnion adds tags to an existing Union", () => {
|
|
202
|
+
const union = S.Union([
|
|
203
|
+
S.TaggedStruct("P", { p: S.String }),
|
|
204
|
+
S.TaggedStruct("Q", { q: S.Finite })
|
|
205
|
+
])
|
|
206
|
+
const extended = S.ExtendTaggedUnion(union)
|
|
207
|
+
|
|
208
|
+
expect(extended.tags.literals).toEqual(["P", "Q"])
|
|
209
|
+
expect(S.decodeSync(extended.tags)("P")).toBe("P")
|
|
210
|
+
expect(S.decodeSync(extended.tags)("Q")).toBe("Q")
|
|
211
|
+
expect(() => S.decodeUnknownSync(extended.tags)("R")).toThrow()
|
|
212
|
+
expect(extended.cases["P"].fields._tag.ast.literal).toBe("P")
|
|
213
|
+
expect(extended.guards.P({ _tag: "P", p: "ok" })).toBe(true)
|
|
214
|
+
expect(extended.guards.P({ _tag: "Q", q: 1 })).toBe(false)
|
|
215
|
+
})
|
|
216
|
+
|
|
217
|
+
test("TaggedUnion match dispatches on _tag", () => {
|
|
218
|
+
const schema = S.TaggedUnion([
|
|
219
|
+
S.TaggedStruct("A", { a: S.String }),
|
|
220
|
+
S.TaggedStruct("B", { b: S.Finite })
|
|
221
|
+
])
|
|
222
|
+
type T = S.Schema.Type<typeof schema>
|
|
223
|
+
|
|
224
|
+
const matcher = schema.match({
|
|
225
|
+
A: (v) => `got A: ${v.a}`,
|
|
226
|
+
B: (v) => `got B: ${v.b}`
|
|
227
|
+
})
|
|
228
|
+
expect(matcher({ _tag: "A", a: "hello" } as T)).toBe("got A: hello")
|
|
229
|
+
expect(matcher({ _tag: "B", b: 42 } as T)).toBe("got B: 42")
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
test("TaggedUnion with single member", () => {
|
|
233
|
+
const schema = S.TaggedUnion([
|
|
234
|
+
S.TaggedStruct("Only", { val: S.String })
|
|
235
|
+
])
|
|
236
|
+
|
|
237
|
+
expect(schema.tags.literals).toEqual(["Only"])
|
|
238
|
+
expect(S.decodeSync(schema.tags)("Only")).toBe("Only")
|
|
239
|
+
expect(() => S.decodeUnknownSync(schema.tags)("Other")).toThrow()
|
|
240
|
+
expect(schema.guards.Only({ _tag: "Only", val: "x" })).toBe(true)
|
|
241
|
+
})
|
|
242
|
+
|
|
243
|
+
test("TaggedUnion tags type is narrowed to the exact tag literals", () => {
|
|
244
|
+
const schema = S.TaggedUnion([
|
|
245
|
+
S.TaggedStruct("Alpha", { a: S.String }),
|
|
246
|
+
S.TaggedStruct("Beta", { b: S.Finite }),
|
|
247
|
+
S.TaggedStruct("Gamma", { c: S.Boolean })
|
|
248
|
+
])
|
|
249
|
+
|
|
250
|
+
type Tags = S.Schema.Type<typeof schema.tags>
|
|
251
|
+
expectTypeOf<Tags>().toEqualTypeOf<"Alpha" | "Beta" | "Gamma">()
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
test("TaggedUnion with encodeKeys renaming a non-tag key", () => {
|
|
255
|
+
const MemberA = S.TaggedStruct("A", { firstName: S.String }).pipe(
|
|
256
|
+
S.encodeKeys({ firstName: "first_name" })
|
|
257
|
+
)
|
|
258
|
+
const MemberB = S.TaggedStruct("B", { lastName: S.Finite }).pipe(
|
|
259
|
+
S.encodeKeys({ lastName: "last_name" })
|
|
260
|
+
)
|
|
261
|
+
|
|
262
|
+
const schema = S.TaggedUnion([MemberA, MemberB])
|
|
263
|
+
|
|
264
|
+
expect(schema.tags.literals).toEqual(["A", "B"])
|
|
265
|
+
expect(S.decodeSync(schema.tags)("A")).toBe("A")
|
|
266
|
+
expect(S.decodeSync(schema.tags)("B")).toBe("B")
|
|
267
|
+
|
|
268
|
+
// decode from encoded (snake_case) to decoded (camelCase)
|
|
269
|
+
const decoded = S.decodeUnknownSync(schema)({ _tag: "A", first_name: "Alice" })
|
|
270
|
+
expect(decoded).toEqual({ _tag: "A", firstName: "Alice" })
|
|
271
|
+
|
|
272
|
+
const decoded2 = S.decodeUnknownSync(schema)({ _tag: "B", last_name: 42 })
|
|
273
|
+
expect(decoded2).toEqual({ _tag: "B", lastName: 42 })
|
|
274
|
+
|
|
275
|
+
// encode back to snake_case
|
|
276
|
+
type T = S.Schema.Type<typeof schema>
|
|
277
|
+
const encoded = S.encodeSync(schema)({ _tag: "A", firstName: "Alice" } as T)
|
|
278
|
+
expect(encoded).toEqual({ _tag: "A", first_name: "Alice" })
|
|
279
|
+
|
|
280
|
+
// guards work on decoded values
|
|
281
|
+
expect(schema.guards.A({ _tag: "A", firstName: "Alice" })).toBe(true)
|
|
282
|
+
expect(schema.guards.A({ _tag: "B", lastName: 42 })).toBe(false)
|
|
283
|
+
expect(schema.guards.B({ _tag: "B", lastName: 42 })).toBe(true)
|
|
284
|
+
})
|
|
285
|
+
|
|
286
|
+
test("TaggedUnion with TaggedClass members", () => {
|
|
287
|
+
class Foo extends S.TaggedClass<Foo>()("Foo", { name: S.String }) {}
|
|
288
|
+
class Bar extends S.TaggedClass<Bar>()("Bar", { count: S.Finite }) {}
|
|
289
|
+
|
|
290
|
+
const schema = S.TaggedUnion([Foo, Bar])
|
|
291
|
+
|
|
292
|
+
expect(schema.tags.literals).toEqual(["Foo", "Bar"])
|
|
293
|
+
expect(S.decodeSync(schema.tags)("Foo")).toBe("Foo")
|
|
294
|
+
expect(S.decodeSync(schema.tags)("Bar")).toBe("Bar")
|
|
295
|
+
expect(() => S.decodeUnknownSync(schema.tags)("Baz")).toThrow()
|
|
296
|
+
|
|
297
|
+
const decoded = S.decodeUnknownSync(schema)({ _tag: "Foo", name: "Alice" })
|
|
298
|
+
expect(decoded).toBeInstanceOf(Foo)
|
|
299
|
+
expect(decoded).toEqual(new Foo({ name: "Alice" }))
|
|
300
|
+
|
|
301
|
+
const decoded2 = S.decodeUnknownSync(schema)({ _tag: "Bar", count: 3 })
|
|
302
|
+
expect(decoded2).toBeInstanceOf(Bar)
|
|
303
|
+
expect(decoded2).toEqual(new Bar({ count: 3 }))
|
|
304
|
+
|
|
305
|
+
expect(schema.guards.Foo(new Foo({ name: "Alice" }))).toBe(true)
|
|
306
|
+
expect(schema.guards.Foo(new Bar({ count: 3 }))).toBe(false)
|
|
307
|
+
expect(schema.guards.Bar(new Bar({ count: 3 }))).toBe(true)
|
|
308
|
+
})
|
|
309
|
+
|
|
310
|
+
describe("ReadonlySetFromArray", () => {
|
|
311
|
+
test("decodes an array of strings to a Set", () => {
|
|
312
|
+
const schema = S.ReadonlySetFromArray(S.String)
|
|
313
|
+
const decoded = S.decodeUnknownSync(schema)(["a", "b", "c"])
|
|
314
|
+
expect(decoded).toEqual(new Set(["a", "b", "c"]))
|
|
315
|
+
})
|
|
316
|
+
|
|
317
|
+
test("encodes a Set back to an array", () => {
|
|
318
|
+
const schema = S.ReadonlySetFromArray(S.String)
|
|
319
|
+
const encoded = S.encodeSync(schema)(new Set(["a", "b"]))
|
|
320
|
+
expect(encoded).toEqual(["a", "b"])
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
test("decodes with NumberFromString as value", () => {
|
|
324
|
+
const schema = S.ReadonlySetFromArray(S.NumberFromString)
|
|
325
|
+
const decoded = S.decodeUnknownSync(schema)(["1", "2", "3"])
|
|
326
|
+
expect(decoded).toEqual(new Set([1, 2, 3]))
|
|
327
|
+
expectTypeOf(decoded).toEqualTypeOf<ReadonlySet<number>>()
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
test("encodes with NumberFromString as value", () => {
|
|
331
|
+
const schema = S.ReadonlySetFromArray(S.NumberFromString)
|
|
332
|
+
const encoded = S.encodeSync(schema)(new Set([1, 2, 3]))
|
|
333
|
+
expect(encoded).toEqual(["1", "2", "3"])
|
|
334
|
+
})
|
|
335
|
+
|
|
336
|
+
test("rejects invalid input", () => {
|
|
337
|
+
const schema = S.ReadonlySetFromArray(S.NumberFromString)
|
|
338
|
+
expect(() => S.decodeUnknownSync(schema)([1, 2])).toThrow()
|
|
339
|
+
})
|
|
340
|
+
})
|
|
341
|
+
|
|
342
|
+
describe("ReadonlyMapFromArray", () => {
|
|
343
|
+
test("decodes an array of tuples to a Map", () => {
|
|
344
|
+
const schema = S.ReadonlyMap({ key: S.String, value: S.Finite })
|
|
345
|
+
const decoded = S.decodeUnknownSync(schema)([["a", 1], ["b", 2]])
|
|
346
|
+
expect(decoded).toEqual(new Map([["a", 1], ["b", 2]]))
|
|
347
|
+
})
|
|
348
|
+
|
|
349
|
+
test("encodes a Map back to an array of tuples", () => {
|
|
350
|
+
const schema = S.ReadonlyMapFromArray({ key: S.String, value: S.Finite })
|
|
351
|
+
const encoded = S.encodeSync(schema)(new Map([["a", 1], ["b", 2]]))
|
|
352
|
+
expect(encoded).toEqual([["a", 1], ["b", 2]])
|
|
353
|
+
})
|
|
354
|
+
|
|
355
|
+
test("decodes with NumberFromString as key", () => {
|
|
356
|
+
const schema = S.ReadonlyMapFromArray({ key: S.NumberFromString, value: S.String })
|
|
357
|
+
const decoded = S.decodeUnknownSync(schema)([["1", "one"], ["2", "two"]])
|
|
358
|
+
expect(decoded).toEqual(new Map([[1, "one"], [2, "two"]]))
|
|
359
|
+
expectTypeOf(decoded).toEqualTypeOf<ReadonlyMap<number, string>>()
|
|
360
|
+
})
|
|
361
|
+
|
|
362
|
+
test("encodes with NumberFromString as key", () => {
|
|
363
|
+
const schema = S.ReadonlyMapFromArray({ key: S.NumberFromString, value: S.String })
|
|
364
|
+
const encoded = S.encodeSync(schema)(new Map([[1, "one"], [2, "two"]]))
|
|
365
|
+
expect(encoded).toEqual([["1", "one"], ["2", "two"]])
|
|
366
|
+
})
|
|
367
|
+
|
|
368
|
+
test("decodes with NumberFromString as value", () => {
|
|
369
|
+
const schema = S.ReadonlyMapFromArray({ key: S.String, value: S.NumberFromString })
|
|
370
|
+
const decoded = S.decodeUnknownSync(schema)([["a", "10"], ["b", "20"]])
|
|
371
|
+
expect(decoded).toEqual(new Map([["a", 10], ["b", 20]]))
|
|
372
|
+
expectTypeOf(decoded).toEqualTypeOf<ReadonlyMap<string, number>>()
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
test("encodes with NumberFromString as value", () => {
|
|
376
|
+
const schema = S.ReadonlyMapFromArray({ key: S.String, value: S.NumberFromString })
|
|
377
|
+
const encoded = S.encodeSync(schema)(new Map([["a", 10], ["b", 20]]))
|
|
378
|
+
expect(encoded).toEqual([["a", "10"], ["b", "20"]])
|
|
379
|
+
})
|
|
380
|
+
|
|
381
|
+
test("decodes with NumberFromString as both key and value", () => {
|
|
382
|
+
const schema = S.ReadonlyMapFromArray({ key: S.NumberFromString, value: S.NumberFromString })
|
|
383
|
+
const decoded = S.decodeUnknownSync(schema)([["1", "10"], ["2", "20"]])
|
|
384
|
+
expect(decoded).toEqual(new Map([[1, 10], [2, 20]]))
|
|
385
|
+
expectTypeOf(decoded).toEqualTypeOf<ReadonlyMap<number, number>>()
|
|
386
|
+
})
|
|
387
|
+
|
|
388
|
+
test("rejects invalid input", () => {
|
|
389
|
+
const schema = S.ReadonlyMapFromArray({ key: S.NumberFromString, value: S.String })
|
|
390
|
+
expect(() => S.decodeUnknownSync(schema)([[1, "val"]])).toThrow()
|
|
391
|
+
})
|
|
392
|
+
})
|
|
393
|
+
|
|
394
|
+
describe("ReadonlySet (with withDefault)", () => {
|
|
395
|
+
test("make provides withDefault", () => {
|
|
396
|
+
const schema = S.ReadonlySet(S.NumberFromString)
|
|
397
|
+
const struct = S.Struct({ items: schema.withDefault })
|
|
398
|
+
const made = struct.make({})
|
|
399
|
+
expect(made.items).toEqual(new Set())
|
|
400
|
+
})
|
|
401
|
+
|
|
402
|
+
test("decodes array with NumberFromString values", () => {
|
|
403
|
+
const schema = S.ReadonlySet(S.NumberFromString)
|
|
404
|
+
const decoded = S.decodeUnknownSync(schema)(["1", "2"])
|
|
405
|
+
expect(decoded).toEqual(new Set([1, 2]))
|
|
406
|
+
})
|
|
407
|
+
})
|
|
408
|
+
|
|
409
|
+
describe("ReadonlyMap (with withDefault)", () => {
|
|
410
|
+
test("make provides withDefault", () => {
|
|
411
|
+
const schema = S.ReadonlyMap({ key: S.NumberFromString, value: S.String })
|
|
412
|
+
const struct = S.Struct({ items: schema.withDefault })
|
|
413
|
+
const made = struct.make({})
|
|
414
|
+
expect(made.items).toEqual(new Map())
|
|
415
|
+
})
|
|
416
|
+
|
|
417
|
+
test("decodes array of tuples with NumberFromString keys", () => {
|
|
418
|
+
const schema = S.ReadonlyMap({ key: S.NumberFromString, value: S.String })
|
|
419
|
+
const decoded = S.decodeUnknownSync(schema)([["1", "one"]])
|
|
420
|
+
expect(decoded).toEqual(new Map([[1, "one"]]))
|
|
421
|
+
})
|
|
422
|
+
})
|
|
423
|
+
|
|
424
|
+
describe("JSON Schema", () => {
|
|
425
|
+
test("Email has format, minLength, maxLength", () => {
|
|
426
|
+
const doc = S.toJsonSchemaDocument(S.Email)
|
|
427
|
+
expect(doc).toStrictEqual({
|
|
428
|
+
dialect: "draft-2020-12",
|
|
429
|
+
schema: { "$ref": "#/$defs/Email" },
|
|
430
|
+
definitions: {
|
|
431
|
+
Email: {
|
|
432
|
+
type: "string",
|
|
433
|
+
title: "Email",
|
|
434
|
+
description: "an email according to RFC 5322",
|
|
435
|
+
format: "email",
|
|
436
|
+
allOf: [
|
|
437
|
+
{ minLength: 3 },
|
|
438
|
+
{ maxLength: 998 }
|
|
439
|
+
]
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
})
|
|
443
|
+
})
|
|
444
|
+
|
|
445
|
+
test("Email specialJsonSchemaDocument flattens allOf", () => {
|
|
446
|
+
const doc = specialJsonSchemaDocument(S.Email)
|
|
447
|
+
expect(doc).toStrictEqual({
|
|
448
|
+
dialect: "draft-2020-12",
|
|
449
|
+
schema: { "$ref": "#/$defs/Email" },
|
|
450
|
+
definitions: {
|
|
451
|
+
Email: {
|
|
452
|
+
type: "string",
|
|
453
|
+
title: "Email",
|
|
454
|
+
description: "an email according to RFC 5322",
|
|
455
|
+
format: "email",
|
|
456
|
+
minLength: 3,
|
|
457
|
+
maxLength: 998
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
})
|
|
461
|
+
})
|
|
462
|
+
|
|
463
|
+
test("Date has format date-time and description", () => {
|
|
464
|
+
const doc = S.toJsonSchemaDocument(S.Date)
|
|
465
|
+
expect(doc).toStrictEqual({
|
|
466
|
+
dialect: "draft-2020-12",
|
|
467
|
+
schema: { "$ref": "#/$defs/Date" },
|
|
468
|
+
definitions: {
|
|
469
|
+
Date: {
|
|
470
|
+
type: "string",
|
|
471
|
+
description: "a string in ISO 8601 format that will be decoded as a Date",
|
|
472
|
+
format: "date-time"
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
})
|
|
476
|
+
})
|
|
477
|
+
|
|
478
|
+
test("DateValid has format date-time", () => {
|
|
479
|
+
const doc = S.toJsonSchemaDocument(S.DateValid)
|
|
480
|
+
expect(doc).toStrictEqual({
|
|
481
|
+
dialect: "draft-2020-12",
|
|
482
|
+
schema: { "$ref": "#/$defs/Date" },
|
|
483
|
+
definitions: {
|
|
484
|
+
Date: {
|
|
485
|
+
type: "string",
|
|
486
|
+
description: "a string in ISO 8601 format that will be decoded as a Date",
|
|
487
|
+
format: "date-time"
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
})
|
|
491
|
+
})
|
|
492
|
+
|
|
493
|
+
test("PhoneNumber has format phone", () => {
|
|
494
|
+
const doc = specialJsonSchemaDocument(S.PhoneNumber)
|
|
495
|
+
expect(doc).toStrictEqual({
|
|
496
|
+
dialect: "draft-2020-12",
|
|
497
|
+
schema: { "$ref": "#/$defs/PhoneNumber" },
|
|
498
|
+
definitions: {
|
|
499
|
+
PhoneNumber: {
|
|
500
|
+
type: "string",
|
|
501
|
+
title: "PhoneNumber",
|
|
502
|
+
description: "a phone number with at least 7 digits",
|
|
503
|
+
format: "phone"
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
})
|
|
507
|
+
})
|
|
508
|
+
|
|
509
|
+
test("Url has format uri", () => {
|
|
510
|
+
const doc = specialJsonSchemaDocument(S.Url)
|
|
511
|
+
expect(doc).toStrictEqual({
|
|
512
|
+
dialect: "draft-2020-12",
|
|
513
|
+
schema: { "$ref": "#/$defs/Url" },
|
|
514
|
+
definitions: {
|
|
515
|
+
Url: {
|
|
516
|
+
type: "string",
|
|
517
|
+
title: "Url",
|
|
518
|
+
format: "uri"
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
})
|
|
522
|
+
})
|
|
523
|
+
})
|
|
524
|
+
|
|
525
|
+
describe("generateGuards", () => {
|
|
526
|
+
const StateSchema = S.TaggedUnion([
|
|
527
|
+
S.TaggedStruct("Active", { since: S.String }),
|
|
528
|
+
S.TaggedStruct("Inactive", { reason: S.String }),
|
|
529
|
+
S.TaggedStruct("Pending", { eta: S.Finite })
|
|
530
|
+
])
|
|
531
|
+
|
|
532
|
+
type State = S.Schema.Type<typeof StateSchema>
|
|
533
|
+
type Entity = { readonly state: State; readonly name: string }
|
|
534
|
+
|
|
535
|
+
const { isActive, isAnyOf, isInactive, isPending } = StateSchema.generateGuards("state")
|
|
536
|
+
|
|
537
|
+
test("isActive narrows to Active member", () => {
|
|
538
|
+
const entity: Entity = { state: { _tag: "Active", since: "2024-01-01" }, name: "foo" }
|
|
539
|
+
expect(isActive(entity)).toBe(true)
|
|
540
|
+
if (isActive(entity)) {
|
|
541
|
+
expectTypeOf(entity.state).toEqualTypeOf<{ readonly _tag: "Active"; readonly since: string }>()
|
|
542
|
+
}
|
|
543
|
+
})
|
|
544
|
+
|
|
545
|
+
test("isActive returns false for non-Active", () => {
|
|
546
|
+
const entity: Entity = { state: { _tag: "Inactive", reason: "expired" }, name: "foo" }
|
|
547
|
+
expect(isActive(entity)).toBe(false)
|
|
548
|
+
})
|
|
549
|
+
|
|
550
|
+
test("isInactive narrows to Inactive member", () => {
|
|
551
|
+
const entity: Entity = { state: { _tag: "Inactive", reason: "expired" }, name: "foo" }
|
|
552
|
+
expect(isInactive(entity)).toBe(true)
|
|
553
|
+
})
|
|
554
|
+
|
|
555
|
+
test("isPending narrows to Pending member", () => {
|
|
556
|
+
const entity: Entity = { state: { _tag: "Pending", eta: 42 }, name: "foo" }
|
|
557
|
+
expect(isPending(entity)).toBe(true)
|
|
558
|
+
})
|
|
559
|
+
|
|
560
|
+
test("isAnyOf narrows to union of specified members", () => {
|
|
561
|
+
const isActiveOrPending = isAnyOf(["Active", "Pending"])
|
|
562
|
+
const active: Entity = { state: { _tag: "Active", since: "2024-01-01" }, name: "foo" }
|
|
563
|
+
const pending: Entity = { state: { _tag: "Pending", eta: 5 }, name: "bar" }
|
|
564
|
+
const inactive: Entity = { state: { _tag: "Inactive", reason: "expired" }, name: "baz" }
|
|
565
|
+
|
|
566
|
+
expect(isActiveOrPending(active)).toBe(true)
|
|
567
|
+
expect(isActiveOrPending(pending)).toBe(true)
|
|
568
|
+
expect(isActiveOrPending(inactive)).toBe(false)
|
|
569
|
+
|
|
570
|
+
if (isActiveOrPending(active)) {
|
|
571
|
+
expectTypeOf(active.state).toEqualTypeOf<
|
|
572
|
+
{ readonly _tag: "Active"; readonly since: string } | { readonly _tag: "Pending"; readonly eta: number }
|
|
573
|
+
>()
|
|
574
|
+
}
|
|
575
|
+
})
|
|
576
|
+
|
|
577
|
+
test("guards use schema-based validation (built-in guards)", () => {
|
|
578
|
+
expect(StateSchema.guards.Active({ _tag: "Active" })).toBe(false)
|
|
579
|
+
expect(StateSchema.guards.Active({ _tag: "Active", since: "2024-01-01" })).toBe(true)
|
|
580
|
+
})
|
|
581
|
+
})
|
|
582
|
+
|
|
583
|
+
describe("generateGuardsFor", () => {
|
|
584
|
+
const StateSchema = S.TaggedUnion([
|
|
585
|
+
S.TaggedStruct("Active", { since: S.String }),
|
|
586
|
+
S.TaggedStruct("Inactive", { reason: S.String }),
|
|
587
|
+
S.TaggedStruct("Pending", { eta: S.Finite })
|
|
588
|
+
])
|
|
589
|
+
|
|
590
|
+
type State = S.Schema.Type<typeof StateSchema>
|
|
591
|
+
type Entity = { readonly state: State; readonly name: string }
|
|
592
|
+
|
|
593
|
+
const { isActive, isAnyOf } = StateSchema.generateGuardsFor<Entity>()("state")
|
|
594
|
+
|
|
595
|
+
test("isActive narrows to Active member", () => {
|
|
596
|
+
const entity: Entity = { state: { _tag: "Active", since: "2024-01-01" }, name: "foo" }
|
|
597
|
+
expect(isActive(entity)).toBe(true)
|
|
598
|
+
if (isActive(entity)) {
|
|
599
|
+
expectTypeOf(entity.state).toEqualTypeOf<{ readonly _tag: "Active"; readonly since: string }>()
|
|
600
|
+
}
|
|
601
|
+
})
|
|
602
|
+
|
|
603
|
+
test("isActive returns false for non-Active", () => {
|
|
604
|
+
const entity: Entity = { state: { _tag: "Inactive", reason: "expired" }, name: "foo" }
|
|
605
|
+
expect(isActive(entity)).toBe(false)
|
|
606
|
+
})
|
|
607
|
+
|
|
608
|
+
test("isAnyOf narrows to union of specified members", () => {
|
|
609
|
+
const isActiveOrPending = isAnyOf(["Active", "Pending"])
|
|
610
|
+
const active: Entity = { state: { _tag: "Active", since: "2024-01-01" }, name: "foo" }
|
|
611
|
+
const inactive: Entity = { state: { _tag: "Inactive", reason: "expired" }, name: "baz" }
|
|
612
|
+
|
|
613
|
+
expect(isActiveOrPending(active)).toBe(true)
|
|
614
|
+
expect(isActiveOrPending(inactive)).toBe(false)
|
|
615
|
+
})
|
|
616
|
+
|
|
617
|
+
test("ExtendTaggedUnion also exposes generateGuardsFor", () => {
|
|
618
|
+
const union = S.Union([
|
|
619
|
+
S.TaggedStruct("X", { x: S.String }),
|
|
620
|
+
S.TaggedStruct("Y", { y: S.Finite })
|
|
621
|
+
])
|
|
622
|
+
const extended = S.ExtendTaggedUnion(union)
|
|
623
|
+
type Obj = { readonly field: S.Schema.Type<typeof extended> }
|
|
624
|
+
const { isX, isY } = extended.generateGuardsFor<Obj>()("field")
|
|
625
|
+
|
|
626
|
+
expect(isX({ field: { _tag: "X", x: "hi" } })).toBe(true)
|
|
627
|
+
expect(isX({ field: { _tag: "Y", y: 1 } })).toBe(false)
|
|
628
|
+
expect(isY({ field: { _tag: "Y", y: 1 } })).toBe(true)
|
|
629
|
+
})
|
|
630
|
+
})
|