effect-app 4.0.0-beta.15 → 4.0.0-beta.150

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.
Files changed (198) hide show
  1. package/CHANGELOG.md +593 -0
  2. package/dist/Array.d.ts +1 -1
  3. package/dist/Chunk.d.ts +1 -1
  4. package/dist/Chunk.d.ts.map +1 -1
  5. package/dist/Config/SecretURL.d.ts +1 -1
  6. package/dist/Config/SecretURL.d.ts.map +1 -1
  7. package/dist/Config/SecretURL.js +2 -2
  8. package/dist/Config/internal/configSecretURL.d.ts +1 -1
  9. package/dist/Config/internal/configSecretURL.d.ts.map +1 -1
  10. package/dist/Config.d.ts +7 -0
  11. package/dist/Config.d.ts.map +1 -0
  12. package/dist/Config.js +6 -0
  13. package/dist/ConfigProvider.d.ts +39 -0
  14. package/dist/ConfigProvider.d.ts.map +1 -0
  15. package/dist/ConfigProvider.js +42 -0
  16. package/dist/Context.d.ts +40 -0
  17. package/dist/Context.d.ts.map +1 -0
  18. package/dist/Context.js +67 -0
  19. package/dist/Effect.d.ts +9 -10
  20. package/dist/Effect.d.ts.map +1 -1
  21. package/dist/Effect.js +3 -6
  22. package/dist/Function.d.ts +1 -1
  23. package/dist/Function.d.ts.map +1 -1
  24. package/dist/Inputify.type.d.ts +1 -1
  25. package/dist/Layer.d.ts +6 -5
  26. package/dist/Layer.d.ts.map +1 -1
  27. package/dist/Layer.js +1 -1
  28. package/dist/NonEmptySet.d.ts +1 -1
  29. package/dist/NonEmptySet.d.ts.map +1 -1
  30. package/dist/Operations.d.ts +372 -50
  31. package/dist/Operations.d.ts.map +1 -1
  32. package/dist/Operations.js +9 -9
  33. package/dist/Option.d.ts +1 -1
  34. package/dist/Option.d.ts.map +1 -1
  35. package/dist/Pure.d.ts +5 -5
  36. package/dist/Pure.d.ts.map +1 -1
  37. package/dist/Pure.js +13 -13
  38. package/dist/Schema/Class.d.ts +69 -20
  39. package/dist/Schema/Class.d.ts.map +1 -1
  40. package/dist/Schema/Class.js +193 -22
  41. package/dist/Schema/FastCheck.d.ts +1 -1
  42. package/dist/Schema/FastCheck.d.ts.map +1 -1
  43. package/dist/Schema/Methods.d.ts +1 -1
  44. package/dist/Schema/SpecialJsonSchema.d.ts +33 -0
  45. package/dist/Schema/SpecialJsonSchema.d.ts.map +1 -0
  46. package/dist/Schema/SpecialJsonSchema.js +122 -0
  47. package/dist/Schema/SpecialOpenApi.d.ts +32 -0
  48. package/dist/Schema/SpecialOpenApi.d.ts.map +1 -0
  49. package/dist/Schema/SpecialOpenApi.js +123 -0
  50. package/dist/Schema/brand.d.ts +7 -2
  51. package/dist/Schema/brand.d.ts.map +1 -1
  52. package/dist/Schema/brand.js +1 -1
  53. package/dist/Schema/email.d.ts +1 -1
  54. package/dist/Schema/email.d.ts.map +1 -1
  55. package/dist/Schema/email.js +7 -4
  56. package/dist/Schema/ext.d.ts +113 -48
  57. package/dist/Schema/ext.d.ts.map +1 -1
  58. package/dist/Schema/ext.js +115 -53
  59. package/dist/Schema/moreStrings.d.ts +111 -11
  60. package/dist/Schema/moreStrings.d.ts.map +1 -1
  61. package/dist/Schema/moreStrings.js +14 -15
  62. package/dist/Schema/numbers.d.ts +127 -15
  63. package/dist/Schema/numbers.d.ts.map +1 -1
  64. package/dist/Schema/numbers.js +10 -12
  65. package/dist/Schema/phoneNumber.d.ts +1 -1
  66. package/dist/Schema/phoneNumber.d.ts.map +1 -1
  67. package/dist/Schema/phoneNumber.js +6 -3
  68. package/dist/Schema/schema.d.ts +1 -1
  69. package/dist/Schema/strings.d.ts +37 -5
  70. package/dist/Schema/strings.d.ts.map +1 -1
  71. package/dist/Schema/strings.js +1 -5
  72. package/dist/Schema.d.ts +88 -53
  73. package/dist/Schema.d.ts.map +1 -1
  74. package/dist/Schema.js +125 -63
  75. package/dist/Set.d.ts +1 -1
  76. package/dist/Set.d.ts.map +1 -1
  77. package/dist/TypeTest.d.ts +1 -1
  78. package/dist/Types.d.ts +1 -1
  79. package/dist/Widen.type.d.ts +1 -1
  80. package/dist/_ext/Array.d.ts +1 -1
  81. package/dist/_ext/Array.d.ts.map +1 -1
  82. package/dist/_ext/date.d.ts +1 -1
  83. package/dist/_ext/misc.d.ts +1 -1
  84. package/dist/_ext/ord.ext.d.ts +1 -1
  85. package/dist/_ext/ord.ext.d.ts.map +1 -1
  86. package/dist/builtin.d.ts +1 -1
  87. package/dist/builtin.d.ts.map +1 -1
  88. package/dist/client/apiClientFactory.d.ts +14 -30
  89. package/dist/client/apiClientFactory.d.ts.map +1 -1
  90. package/dist/client/apiClientFactory.js +18 -19
  91. package/dist/client/clientFor.d.ts +7 -6
  92. package/dist/client/clientFor.d.ts.map +1 -1
  93. package/dist/client/errors.d.ts +44 -19
  94. package/dist/client/errors.d.ts.map +1 -1
  95. package/dist/client/errors.js +35 -10
  96. package/dist/client/makeClient.d.ts +77 -29
  97. package/dist/client/makeClient.d.ts.map +1 -1
  98. package/dist/client/makeClient.js +49 -23
  99. package/dist/client.d.ts +1 -1
  100. package/dist/faker.d.ts +1 -1
  101. package/dist/faker.d.ts.map +1 -1
  102. package/dist/http/Request.d.ts +2 -2
  103. package/dist/http/Request.d.ts.map +1 -1
  104. package/dist/http/Request.js +5 -5
  105. package/dist/http/internal/lib.d.ts +1 -1
  106. package/dist/http.d.ts +1 -1
  107. package/dist/ids.d.ts +3 -3
  108. package/dist/ids.d.ts.map +1 -1
  109. package/dist/ids.js +3 -2
  110. package/dist/index.d.ts +5 -8
  111. package/dist/index.d.ts.map +1 -1
  112. package/dist/index.js +6 -8
  113. package/dist/logger.d.ts +1 -1
  114. package/dist/middleware.d.ts +8 -8
  115. package/dist/middleware.d.ts.map +1 -1
  116. package/dist/middleware.js +8 -8
  117. package/dist/rpc/MiddlewareMaker.d.ts +5 -4
  118. package/dist/rpc/MiddlewareMaker.d.ts.map +1 -1
  119. package/dist/rpc/MiddlewareMaker.js +26 -27
  120. package/dist/rpc/RpcContextMap.d.ts +3 -3
  121. package/dist/rpc/RpcContextMap.d.ts.map +1 -1
  122. package/dist/rpc/RpcContextMap.js +4 -4
  123. package/dist/rpc/RpcMiddleware.d.ts +5 -4
  124. package/dist/rpc/RpcMiddleware.d.ts.map +1 -1
  125. package/dist/rpc/RpcMiddleware.js +1 -1
  126. package/dist/rpc.d.ts +1 -2
  127. package/dist/rpc.d.ts.map +1 -1
  128. package/dist/rpc.js +1 -2
  129. package/dist/transform.d.ts +1 -1
  130. package/dist/transform.d.ts.map +1 -1
  131. package/dist/transform.js +3 -3
  132. package/dist/utils/effectify.d.ts +1 -1
  133. package/dist/utils/extend.d.ts +1 -1
  134. package/dist/utils/extend.d.ts.map +1 -1
  135. package/dist/utils/gen.d.ts +2 -2
  136. package/dist/utils/gen.d.ts.map +1 -1
  137. package/dist/utils/logLevel.d.ts +2 -2
  138. package/dist/utils/logLevel.d.ts.map +1 -1
  139. package/dist/utils/logger.d.ts +3 -3
  140. package/dist/utils/logger.d.ts.map +1 -1
  141. package/dist/utils/logger.js +3 -3
  142. package/dist/utils.d.ts +30 -10
  143. package/dist/utils.d.ts.map +1 -1
  144. package/dist/utils.js +10 -4
  145. package/dist/validation/validators.d.ts +1 -1
  146. package/dist/validation/validators.d.ts.map +1 -1
  147. package/dist/validation.d.ts +1 -1
  148. package/dist/validation.d.ts.map +1 -1
  149. package/eslint.config.mjs +1 -1
  150. package/package.json +35 -19
  151. package/src/Config/SecretURL.ts +2 -1
  152. package/src/Config.ts +14 -0
  153. package/src/ConfigProvider.ts +48 -0
  154. package/src/{ServiceMap.ts → Context.ts} +52 -59
  155. package/src/Effect.ts +12 -14
  156. package/src/Layer.ts +5 -4
  157. package/src/Operations.ts +9 -9
  158. package/src/Pure.ts +17 -18
  159. package/src/Schema/Class.ts +281 -62
  160. package/src/Schema/SpecialJsonSchema.ts +137 -0
  161. package/src/Schema/SpecialOpenApi.ts +130 -0
  162. package/src/Schema/brand.ts +9 -1
  163. package/src/Schema/email.ts +7 -2
  164. package/src/Schema/ext.ts +196 -87
  165. package/src/Schema/moreStrings.ts +22 -20
  166. package/src/Schema/numbers.ts +14 -16
  167. package/src/Schema/phoneNumber.ts +5 -1
  168. package/src/Schema/strings.ts +4 -8
  169. package/src/Schema.ts +256 -98
  170. package/src/client/apiClientFactory.ts +107 -113
  171. package/src/client/clientFor.ts +6 -1
  172. package/src/client/errors.ts +42 -17
  173. package/src/client/makeClient.ts +156 -63
  174. package/src/http/Request.ts +7 -4
  175. package/src/ids.ts +2 -1
  176. package/src/index.ts +5 -10
  177. package/src/middleware.ts +7 -9
  178. package/src/rpc/MiddlewareMaker.ts +36 -47
  179. package/src/rpc/RpcContextMap.ts +6 -5
  180. package/src/rpc/RpcMiddleware.ts +5 -4
  181. package/src/rpc.ts +0 -1
  182. package/src/transform.ts +2 -2
  183. package/src/utils/gen.ts +1 -1
  184. package/src/utils/logger.ts +2 -2
  185. package/src/utils.ts +47 -11
  186. package/test/dist/rpc.test.d.ts.map +1 -1
  187. package/test/dist/secretURL.test.d.ts.map +1 -0
  188. package/test/dist/special.test.d.ts.map +1 -0
  189. package/test/rpc.test.ts +38 -6
  190. package/test/schema.test.ts +594 -4
  191. package/test/secretURL.test.ts +157 -0
  192. package/test/special.test.ts +1009 -0
  193. package/test/utils.test.ts +6 -6
  194. package/tsconfig.base.json +0 -1
  195. package/tsconfig.json +0 -1
  196. package/dist/ServiceMap.d.ts +0 -44
  197. package/dist/ServiceMap.d.ts.map +0 -1
  198. package/dist/ServiceMap.js +0 -91
@@ -12,11 +12,16 @@ export type Email = string & EmailBrand
12
12
  export const Email = S
13
13
  .String
14
14
  .pipe(
15
+ S.annotate({
16
+ title: "Email",
17
+ description: "an email according to RFC 5322",
18
+ format: "email"
19
+ }),
20
+ S.check(S.isMinLength(3), /* a@b */ S.isMaxLength(998)),
15
21
  S.refine(isValidEmail as Refinement<string, Email>, {
16
22
  identifier: "Email",
17
- title: "Email",
18
23
  description: "an email according to RFC 5322",
19
- jsonSchema: { format: "email", minLength: 3, /* a@b */ maxLength: 998 }
24
+ jsonSchema: { format: "email", minLength: 3, maxLength: 998 }
20
25
  }),
21
26
  S.annotate({
22
27
  toArbitrary: () => (fc) => fc.emailAddress().map((_) => _ as Email)
package/src/Schema/ext.ts CHANGED
@@ -1,56 +1,94 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
  /* eslint-disable @typescript-eslint/no-unsafe-return */
3
- import { Effect, Option, pipe, Schema, type SchemaAST, SchemaGetter, SchemaIssue, SchemaParser, SchemaTransformation, type ServiceMap } from "effect"
3
+ import { Effect, Function, Option, pipe, type SchemaAST, SchemaIssue, SchemaTransformation } from "effect"
4
4
  import * as S from "effect/Schema"
5
+ import { isDateValid } from "effect/Schema"
5
6
  import { type NonEmptyReadonlyArray } from "../Array.js"
7
+ import * as Context from "../Context.js"
6
8
  import { extendM, typedKeysOf } from "../utils.js"
7
9
  import { type AST } from "./schema.js"
8
10
 
9
- // TODO: v4 migration withConstructorDefault signature changed, propertySignature removed
10
- // Constraint relaxed from `Self extends S.Top & S.WithoutConstructorDefault` to `Self extends S.Top`
11
- // because `.pipe()` widens the schema type to `Top` which doesn't satisfy `WithoutConstructorDefault`.
12
- // The narrowing assertions below are safe — we're asserting "this schema hasn't had a default applied yet".
13
- export const withDefaultConstructor = <A>(
14
- makeDefault: () => NoInfer<A>
15
- ) =>
16
- <Self extends S.Top>(self: Self): S.withConstructorDefault<Self & S.WithoutConstructorDefault> => {
17
- type Narrowed = Self & S.WithoutConstructorDefault
18
- return S.withConstructorDefault<Narrowed>(
19
- () => Option.some(makeDefault() as Narrowed["~type.make.in"])
20
- )(self as Narrowed)
21
- }
11
+ type ProvidedCodec<Self extends S.Top, R> = S.Codec<
12
+ Self["Type"],
13
+ Self["Encoded"],
14
+ Exclude<Self["DecodingServices"], R>,
15
+ Exclude<Self["EncodingServices"], R>
16
+ >
22
17
 
23
18
  // TODO: v4 migration - Date is no longer by default encoded to string.
24
- const DateFromString = Schema.Date.pipe(
25
- Schema.encodeTo(Schema.String, {
26
- decode: SchemaGetter.Date(),
27
- encode: SchemaGetter.transform((_) => _.toISOString())
28
- })
29
- )
19
+
20
+ const DateString = S.String.annotate({
21
+ identifier: "Date",
22
+ description: "a string in ISO 8601 format that will be decoded as a Date",
23
+ format: "date-time"
24
+ })
25
+
26
+ /**
27
+ * Schema type for {@link DateFromString}.
28
+ *
29
+ * @category Schemas
30
+ * @since 4.0.0
31
+ */
32
+ export interface DateFromString extends S.decodeTo<S.Date, S.String> {}
33
+
34
+ /**
35
+ * A transformation schema that parses an ISO 8601 string into a `Date`.
36
+ *
37
+ * Decoding:
38
+ * - A `string` is decoded as a `Date`.
39
+ *
40
+ * Encoding:
41
+ * - A `Date` is encoded as a `string`.
42
+ *
43
+ * @since 4.0.0
44
+ */
45
+ export const DateFromString: DateFromString = DateString.pipe(S.decodeTo(S.Date, SchemaTransformation.dateFromString))
30
46
 
31
47
  /**
32
48
  * Like the default Schema `Date` but from String with `withDefault` => now
33
49
  */
34
50
  export const Date = Object.assign(DateFromString, {
35
- withDefault: DateFromString.pipe(withDefaultConstructor(() => new global.Date()))
51
+ withDefault: DateFromString.pipe(S.withConstructorDefault(Effect.sync(() => new global.Date()))),
52
+ withDecodingDefaultType: DateFromString.pipe(S.withDecodingDefaultType(Effect.sync(() => new global.Date())))
53
+ })
54
+
55
+ /**
56
+ * Like the default Schema `DateValid` but from String with `withDefault` => now
57
+ */
58
+ export const DateValid = Object.assign(Date.check(isDateValid()), {
59
+ withDefault: DateFromString.pipe(S.withConstructorDefault(Effect.sync(() => new global.Date()))),
60
+ withDecodingDefaultType: DateFromString.pipe(S.withDecodingDefaultType(Effect.sync(() => new global.Date())))
36
61
  })
37
62
 
38
63
  /**
39
64
  * Like the default Schema `Boolean` but with `withDefault` => false
40
65
  */
41
66
  export const Boolean = Object.assign(S.Boolean, {
42
- withDefault: S.Boolean.pipe(withDefaultConstructor(() => false))
67
+ withDefault: S.Boolean.pipe(S.withConstructorDefault(Effect.succeed(false))),
68
+ withDecodingDefaultType: S.Boolean.pipe(S.withDecodingDefaultType(Effect.succeed(false)))
43
69
  })
44
70
 
45
71
  /**
72
+ * You probably want to use `Finite` instead of this.
46
73
  * Like the default Schema `Number` but with `withDefault` => 0
47
74
  */
48
- export const Number = Object.assign(S.Number, { withDefault: S.Number.pipe(withDefaultConstructor(() => 0)) })
75
+ export const Number = Object.assign(S.Number, {
76
+ withDefault: S.Number.pipe(S.withConstructorDefault(Effect.succeed(0))),
77
+ withDecodingDefaultType: S.Number.pipe(S.withDecodingDefaultType(Effect.succeed(0)))
78
+ })
49
79
 
50
80
  /**
51
- * Like the default Schema `Literal` but with `withDefault` => literals[0]
81
+ * Like the default Schema `Finite` but with `withDefault` => 0
52
82
  */
53
- export const Literal = <Literals extends NonEmptyReadonlyArray<AST.LiteralValue>>(...literals: Literals) =>
83
+ export const Finite = Object.assign(S.Finite, {
84
+ withDefault: S.Finite.pipe(S.withConstructorDefault(Effect.succeed(0))),
85
+ withDecodingDefaultType: S.Finite.pipe(S.withDecodingDefaultType(Effect.succeed(0)))
86
+ })
87
+
88
+ /**
89
+ * Like the default Schema `Literals` but with `withDefault` => literals[0]
90
+ */
91
+ export const Literals = <const Literals extends NonEmptyReadonlyArray<AST.LiteralValue>>(literals: Literals) =>
54
92
  pipe(
55
93
  S.Literals(literals),
56
94
  (s) =>
@@ -58,79 +96,144 @@ export const Literal = <Literals extends NonEmptyReadonlyArray<AST.LiteralValue>
58
96
  changeDefault: <A extends Literals[number]>(a: A) => {
59
97
  return Object.assign(S.Literals(literals), {
60
98
  Default: a,
61
- withDefault: s.pipe(withDefaultConstructor(() => a))
99
+ withDefault: s.pipe(S.withConstructorDefault(Effect.succeed(a))),
100
+ withDecodingDefaultType: s.pipe(S.withDecodingDefaultType(Effect.succeed(a)))
62
101
  }) // todo: copy annotations from original?
63
102
  },
64
- Default: literals[0] as typeof literals[0],
65
- withDefault: s.pipe(withDefaultConstructor(() => literals[0]))
103
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion -- load-bearing: Object.assign widens the field type without it, breaking `expectTypeOf(l.Default).toEqualTypeOf<"a">()` in tests
104
+ Default: literals[0] as Literals[0],
105
+ withDefault: s.pipe(S.withConstructorDefault(Effect.succeed(literals[0]))),
106
+ withDecodingDefaultType: s.pipe(S.withDecodingDefaultType(Effect.succeed(literals[0])))
66
107
  })
67
108
  )
68
109
 
69
110
  /**
70
111
  * Like the default Schema `Array` but with `withDefault` => []
71
112
  */
72
- export function Array<Value extends S.Top>(value: Value) {
113
+ const co = { parseOptions: { concurrency: "unbounded" as const } }
114
+ export { co as concurrencyUnbounded }
115
+
116
+ export function Array<ValueSchema extends S.Top>(value: ValueSchema) {
73
117
  return pipe(
74
- S.Array(value),
75
- (s) => Object.assign(s, { withDefault: s.pipe(withDefaultConstructor(() => [])) })
118
+ S.Array(value).annotate(co),
119
+ (s) =>
120
+ Object.assign(s, {
121
+ withDefault: s.pipe(S.withConstructorDefault(Effect.sync(() => []))),
122
+ withDecodingDefaultType: s.pipe(S.withDecodingDefaultType(Effect.sync(() => [])))
123
+ })
76
124
  )
77
125
  }
78
126
 
79
127
  /**
80
- * Like the default Schema `Map` but with `withDefault` => []
128
+ * An annotated `S.Array` of unique items that decodes to a `ReadonlySet`.
81
129
  */
82
- function Map_<Key extends S.Top, Value extends S.Top>(input: { key: Key; value: Value }) {
83
- return pipe(
84
- S.ReadonlyMap(input.key, input.value),
85
- (s) => Object.assign(s, { withDefault: s.pipe(withDefaultConstructor(() => new global.Map())) })
130
+ export const ReadonlySetFromArray = <ValueSchema extends S.Top>(value: ValueSchema) => {
131
+ const from = S
132
+ .Array(value)
133
+ .annotate({ ...co, expected: "an array of unique items that will be decoded as a ReadonlySet" })
134
+ const to = S.instanceOf(Set) as S.instanceOf<ReadonlySet<S.Schema.Type<ValueSchema>>>
135
+ const schema = from.pipe(
136
+ S.decodeTo(
137
+ to,
138
+ SchemaTransformation.transform({
139
+ decode: (arr) => new Set(arr) as ReadonlySet<S.Schema.Type<ValueSchema>>,
140
+ encode: (set) => [...set]
141
+ })
142
+ )
86
143
  )
144
+ return schema
87
145
  }
88
146
 
89
- export { Map_ as Map }
147
+ /**
148
+ * An annotated `S.Array` of key-value tuples that decodes to a `ReadonlyMap`.
149
+ */
150
+ export const ReadonlyMapFromArray = <KeySchema extends S.Top, ValueSchema extends S.Top>(pair: {
151
+ readonly key: KeySchema
152
+ readonly value: ValueSchema
153
+ }) => {
154
+ const from = S
155
+ .Array(S.Tuple([pair.key, pair.value]))
156
+ .annotate({ ...co, expected: "an array of key-value tuples that will be decoded as a ReadonlyMap" })
157
+ const to = S.instanceOf(Map) as S.instanceOf<
158
+ ReadonlyMap<S.Schema.Type<KeySchema>, S.Schema.Type<ValueSchema>>
159
+ >
160
+ const schema = from.pipe(
161
+ S.decodeTo(
162
+ to,
163
+ SchemaTransformation.transform({
164
+ decode: (
165
+ arr
166
+ ) => new Map(arr) as ReadonlyMap<S.Schema.Type<KeySchema>, S.Schema.Type<ValueSchema>>,
167
+ encode: (
168
+ map
169
+ ) => [...map.entries()] as any // fu
170
+ })
171
+ )
172
+ )
173
+ return schema
174
+ }
90
175
 
91
176
  /**
92
- * Like the default Schema `ReadonlySet` but with `withDefault` => new Set()
177
+ * Like the default Schema `ReadonlySet` but from Array with `withDefault` => new Set()
93
178
  */
94
- export const ReadonlySet = <Value extends S.Top>(value: Value) =>
179
+ export const ReadonlySet = <ValueSchema extends S.Top>(value: ValueSchema) =>
95
180
  pipe(
96
- S.ReadonlySet(value),
97
- (s) => Object.assign(s, { withDefault: s.pipe(withDefaultConstructor(() => new Set<S.Schema.Type<Value>>())) })
181
+ ReadonlySetFromArray(value),
182
+ (s) =>
183
+ Object.assign(s, {
184
+ withDefault: s.pipe(S.withConstructorDefault(Effect.sync(() => new Set<S.Schema.Type<ValueSchema>>()))),
185
+ withDecodingDefaultType: s.pipe(
186
+ S.withDecodingDefaultType(Effect.sync(() => new Set<S.Schema.Type<ValueSchema>>()))
187
+ )
188
+ })
98
189
  )
99
190
 
100
191
  /**
101
- * Like the default Schema `ReadonlyMap` but with `withDefault` => new Map()
192
+ * Like the default Schema `ReadonlyMap` but from Array with `withDefault` => new Map()
102
193
  */
103
- export const ReadonlyMap = <K extends S.Top, V extends S.Top>(pair: {
104
- readonly key: K
105
- readonly value: V
194
+ export const ReadonlyMap = <KeySchema extends S.Top, ValueSchema extends S.Top>(pair: {
195
+ readonly key: KeySchema
196
+ readonly value: ValueSchema
106
197
  }) =>
107
198
  pipe(
108
- S.ReadonlyMap(pair.key, pair.value),
109
- (s) => Object.assign(s, { withDefault: s.pipe(withDefaultConstructor(() => new Map())) })
199
+ ReadonlyMapFromArray(pair),
200
+ (s) =>
201
+ Object.assign(s, {
202
+ withDefault: s.pipe(S.withConstructorDefault(Effect.sync(() => new Map()))),
203
+ withDecodingDefaultType: s.pipe(S.withDecodingDefaultType(Effect.sync(() => new Map())))
204
+ })
110
205
  )
111
206
 
112
207
  /**
113
208
  * Like the default Schema `NullOr` but with `withDefault` => null
114
209
  */
115
- export const NullOr = <S extends S.Top>(self: S) =>
210
+ export const NullOr = <Schema extends S.Top>(self: Schema) =>
116
211
  pipe(
117
212
  S.NullOr(self),
118
- (s) => Object.assign(s, { withDefault: s.pipe(withDefaultConstructor(() => null)) })
213
+ (s) =>
214
+ Object.assign(s, {
215
+ withDefault: s.pipe(S.withConstructorDefault(Effect.succeed(null))),
216
+ withDecodingDefaultType: s.pipe(S.withDecodingDefaultType(Effect.succeed(null)))
217
+ })
119
218
  )
120
219
 
121
- export const defaultDate = (s: S.Top) => s.pipe(withDefaultConstructor(() => new global.Date()))
220
+ export const defaultDate = <Schema extends S.Top & S.WithoutConstructorDefault>(schema: Schema) =>
221
+ schema.pipe(S.withConstructorDefault(Effect.sync(() => new global.Date())))
122
222
 
123
- export const defaultBool = (s: S.Top) => s.pipe(withDefaultConstructor(() => false))
223
+ export const defaultBool = <Schema extends S.Top & S.WithoutConstructorDefault>(schema: Schema) =>
224
+ schema.pipe(S.withConstructorDefault(Effect.succeed(false)))
124
225
 
125
- export const defaultNullable = (
126
- s: S.Top
127
- ) => s.pipe(withDefaultConstructor(() => null))
226
+ export const defaultNullable = <Schema extends S.Top & S.WithoutConstructorDefault>(schema: Schema) =>
227
+ schema.pipe(S.withConstructorDefault(Effect.succeed(null)))
128
228
 
129
- export const defaultArray = (s: S.Top) => s.pipe(withDefaultConstructor(() => []))
229
+ export const defaultArray = <Schema extends S.Top & S.WithoutConstructorDefault>(schema: Schema) =>
230
+ schema.pipe(S.withConstructorDefault(Effect.sync(() => [])))
130
231
 
131
- export const defaultMap = (s: S.Top) => s.pipe(withDefaultConstructor(() => new Map()))
232
+ export const defaultMap = <Schema extends S.Top & S.WithoutConstructorDefault>(schema: Schema) =>
233
+ schema.pipe(S.withConstructorDefault(Effect.sync(() => new Map())))
132
234
 
133
- export const defaultSet = (s: S.Top) => s.pipe(withDefaultConstructor(() => new Set()))
235
+ export const defaultSet = <Schema extends S.Top & S.WithoutConstructorDefault>(schema: Schema) =>
236
+ schema.pipe(S.withConstructorDefault(Effect.sync(() => new Set())))
134
237
 
135
238
  export const withDefaultMake = <Self extends S.Top>(s: Self) => {
136
239
  const a = Object.assign(S.decodeSync(s as any) as WithDefaults<Self>, s)
@@ -160,8 +263,11 @@ export type WithDefaults<Self extends S.Top> = (
160
263
  // : never
161
264
 
162
265
  export const inputDate = extendM(
163
- S.Union([S.DateValid, S.Date]),
164
- (s) => ({ withDefault: s.pipe(withDefaultConstructor(() => new globalThis.Date())) })
266
+ S.Union([S.DateValid, Date]),
267
+ (s) => ({
268
+ withDefault: s.pipe(S.withConstructorDefault(Effect.sync(() => new globalThis.Date()))),
269
+ withDecodingDefaultType: s.pipe(S.withDecodingDefaultType(Effect.sync(() => new globalThis.Date())))
270
+ })
165
271
  )
166
272
 
167
273
  export interface UnionBrand {}
@@ -211,7 +317,7 @@ export const transformTo = <To extends S.Top, From extends S.Top>(
211
317
  { message: "One way schema transformation, encoding is not allowed" }
212
318
  )
213
319
  )
214
- }) as any
320
+ })
215
321
  )
216
322
  )
217
323
 
@@ -228,7 +334,7 @@ export const transformToOrFail = <To extends S.Top, From extends S.Top, RD>(
228
334
  S.decodeTo(
229
335
  to,
230
336
  SchemaTransformation.transformOrFail({
231
- decode: decode as any,
337
+ decode,
232
338
  encode: (i: any) =>
233
339
  Effect.fail(
234
340
  new SchemaIssue.Forbidden(
@@ -236,33 +342,36 @@ export const transformToOrFail = <To extends S.Top, From extends S.Top, RD>(
236
342
  { message: "One way schema transformation, encoding is not allowed" }
237
343
  )
238
344
  )
239
- }) as any
345
+ })
240
346
  )
241
347
  )
242
348
 
243
- // TODO: v4 migration — S.declare API changed (no [self] + decode/encode pattern)
244
- // Need to find v4 equivalent for contextual schema wrapping
245
- export const provide = <Self extends S.Top, R>(
246
- self: Self,
247
- context: ServiceMap.ServiceMap<R>
248
- ): any => {
349
+ export const provide: {
350
+ <R>(context: Context.Context<R>): <Self extends S.Top>(self: Self) => ProvidedCodec<Self, R>
351
+ <Self extends S.Top, R>(self: Self, context: Context.Context<R>): ProvidedCodec<Self, R>
352
+ } = Function.dual(2, <Self extends S.Top, R>(self: Self, context: Context.Context<R>): ProvidedCodec<Self, R> => {
249
353
  const prov = Effect.provide(context)
250
- return S
251
- .declare((_u: unknown): _u is unknown => true) // placeholder — needs proper v4 declare
252
- .pipe(
253
- S.decodeTo(
254
- self,
255
- SchemaTransformation.transformOrFail({
256
- decode: (n: any) => prov(SchemaParser.decodeUnknownEffect(self)(n)),
257
- encode: (n: any) => prov(SchemaParser.encodeUnknownEffect(self)(n))
258
- }) as any
259
- ) as any
260
- )
261
- }
262
- // TODO: v4 migration — ServiceMap.pick and S.declare pattern removed
263
- export const contextFromServices = <Self extends S.Top, Tags extends readonly any[]>(
264
- _self: Self,
265
- ..._services: Tags
266
- ): any => {
267
- throw new Error("contextFromServices: not yet migrated to v4")
268
- }
354
+ return self.pipe(
355
+ S.middlewareDecoding((effect) => prov(effect)),
356
+ S.middlewareEncoding((effect) => prov(effect))
357
+ )
358
+ })
359
+ export const contextFromServices = Effect.fnUntraced(function*<
360
+ Self extends S.Top,
361
+ Tags extends ReadonlyArray<Context.Key<any, any>>
362
+ >(self: Self, ...services: Tags) {
363
+ const context: Context.Context<Context.Service.Identifier<Tags[number]>> = Context.pick(...services)(
364
+ yield* Effect.context<Context.Service.Identifier<Tags[number]>>()
365
+ )
366
+ return provide(self, context)
367
+ }) as <
368
+ Self extends S.Top,
369
+ Tags extends ReadonlyArray<Context.Key<any, any>>
370
+ >(
371
+ self: Self,
372
+ ...services: Tags
373
+ ) => Effect.Effect<
374
+ ProvidedCodec<Self, Context.Service.Identifier<Tags[number]>>,
375
+ never,
376
+ Context.Service.Identifier<Tags[number]>
377
+ >
@@ -1,4 +1,4 @@
1
- import { pipe } from "effect"
1
+ import { Effect, pipe } from "effect"
2
2
  import type { Refinement } from "effect-app/Function"
3
3
  import { extendM } from "effect-app/utils"
4
4
  import * as S from "effect/Schema"
@@ -6,7 +6,7 @@ import type { Simplify } from "effect/Types"
6
6
  import { customRandom, nanoid, urlAlphabet } from "nanoid"
7
7
  import validator from "validator"
8
8
  import { fromBrand, nominal } from "./brand.js"
9
- import { withDefaultConstructor, withDefaultMake, type WithDefaults } from "./ext.js"
9
+ import { withDefaultMake, type WithDefaults } from "./ext.js"
10
10
  import { type B } from "./schema.js"
11
11
  import type { NonEmptyString255Brand, NonEmptyStringBrand } from "./strings.js"
12
12
 
@@ -27,9 +27,8 @@ export type NonEmptyString50 = string & NonEmptyString50Brand
27
27
  */
28
28
  export const NonEmptyString50 = nonEmptyString.pipe(
29
29
  S.check(S.isMaxLength(50)),
30
- fromBrand(nominal<NonEmptyString50>(), {
30
+ fromBrand<NonEmptyString50>(nominal<NonEmptyString50>(), {
31
31
  identifier: "NonEmptyString50",
32
- title: "NonEmptyString50",
33
32
  jsonSchema: {}
34
33
  }),
35
34
  withDefaultMake
@@ -50,9 +49,8 @@ export type NonEmptyString64 = string & NonEmptyString64Brand
50
49
  */
51
50
  export const NonEmptyString64 = nonEmptyString.pipe(
52
51
  S.check(S.isMaxLength(64)),
53
- fromBrand(nominal<NonEmptyString64>(), {
52
+ fromBrand<NonEmptyString64>(nominal<NonEmptyString64>(), {
54
53
  identifier: "NonEmptyString64",
55
- title: "NonEmptyString64",
56
54
  jsonSchema: {}
57
55
  }),
58
56
  withDefaultMake
@@ -74,9 +72,8 @@ export type NonEmptyString80 = string & NonEmptyString80Brand
74
72
 
75
73
  export const NonEmptyString80 = nonEmptyString.pipe(
76
74
  S.check(S.isMaxLength(80)),
77
- fromBrand(nominal<NonEmptyString80>(), {
75
+ fromBrand<NonEmptyString80>(nominal<NonEmptyString80>(), {
78
76
  identifier: "NonEmptyString80",
79
- title: "NonEmptyString80",
80
77
  jsonSchema: {}
81
78
  }),
82
79
  withDefaultMake
@@ -97,9 +94,8 @@ export type NonEmptyString100 = string & NonEmptyString100Brand
97
94
  */
98
95
  export const NonEmptyString100 = nonEmptyString.pipe(
99
96
  S.check(S.isMaxLength(100)),
100
- fromBrand(nominal<NonEmptyString100>(), {
97
+ fromBrand<NonEmptyString100>(nominal<NonEmptyString100>(), {
101
98
  identifier: "NonEmptyString100",
102
- title: "NonEmptyString100",
103
99
  jsonSchema: {}
104
100
  }),
105
101
  withDefaultMake
@@ -121,7 +117,10 @@ export type Min3String255 = string & Min3String255Brand
121
117
  export const Min3String255 = pipe(
122
118
  S.String,
123
119
  S.check(S.isMinLength(3), S.isMaxLength(255)),
124
- fromBrand(nominal<Min3String255>(), { identifier: "Min3String255", title: "Min3String255", jsonSchema: {} }),
120
+ fromBrand<Min3String255>(nominal<Min3String255>(), {
121
+ identifier: "Min3String255",
122
+ jsonSchema: {}
123
+ }),
125
124
  withDefaultMake
126
125
  )
127
126
 
@@ -151,16 +150,15 @@ export const StringId = extendM(
151
150
  pipe(
152
151
  S.String,
153
152
  S.check(S.isMinLength(minLength), S.isMaxLength(maxLength)),
154
- fromBrand(nominal<StringId>(), {
153
+ fromBrand<StringId>(nominal<StringId>(), {
155
154
  identifier: "StringId",
156
- title: "StringId",
157
155
  toArbitrary: () => (fc) => StringIdArb()(fc),
158
156
  jsonSchema: {}
159
157
  })
160
158
  ),
161
159
  (s) => ({
162
160
  make: makeStringId,
163
- withDefault: s.pipe(withDefaultConstructor(makeStringId))
161
+ withDefault: s.pipe(S.withConstructorDefault(Effect.sync(makeStringId)))
164
162
  })
165
163
  )
166
164
  .pipe(withDefaultMake)
@@ -182,16 +180,15 @@ export function prefixedStringId<Brand extends StringId>() {
182
180
  (x) => (pref + x.substring(0, 50 - pref.length)) as Brand
183
181
  )
184
182
  // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
185
- const s: S.Codec<string & Brand, string> = StringId
183
+ const s = StringId
186
184
  .pipe(
187
185
  S.refine((x: string): x is string & Brand => x.startsWith(pref), {
188
- identifier: name,
189
- title: name
186
+ identifier: name
190
187
  }),
191
188
  S.annotate({
192
189
  toArbitrary: () => (fc) => arb()(fc)
193
190
  })
194
- ) as S.Codec<string & Brand, string>
191
+ )
195
192
  const schema = s.pipe(withDefaultMake)
196
193
  const make = () => (pref + StringId.make().substring(0, 50 - pref.length)) as Brand
197
194
 
@@ -208,7 +205,9 @@ export function prefixedStringId<Brand extends StringId>() {
208
205
  */
209
206
  prefixSafe: <REST extends string>(str: `${Prefix}${Separator}${REST}`) => ex(str),
210
207
  prefix,
211
- withDefault: schema.pipe(withDefaultConstructor(make))
208
+ withDefault: schema.pipe(S.withConstructorDefault<S.Codec<Brand, string> & S.WithoutConstructorDefault>(
209
+ Effect.sync(make)
210
+ ))
212
211
  })
213
212
  )
214
213
  }
@@ -247,9 +246,12 @@ const isUrl: Refinement<string, Url> = (s: string): s is Url => {
247
246
  export const Url = S
248
247
  .String
249
248
  .pipe(
249
+ S.annotate({
250
+ title: "Url",
251
+ format: "uri"
252
+ }),
250
253
  S.refine(isUrl, {
251
254
  identifier: "Url",
252
- title: "Url",
253
255
  jsonSchema: { format: "uri" }
254
256
  }),
255
257
  S.annotate({
@@ -1,8 +1,9 @@
1
+ import { Effect } from "effect"
1
2
  import { extendM } from "effect-app/utils"
2
3
  import * as S from "effect/Schema"
3
4
  import type { Simplify } from "effect/Types"
4
5
  import { fromBrand, nominal } from "./brand.js"
5
- import { withDefaultConstructor, withDefaultMake } from "./ext.js"
6
+ import { withDefaultMake } from "./ext.js"
6
7
  import { type B } from "./schema.js"
7
8
 
8
9
  export interface PositiveIntBrand
@@ -11,10 +12,10 @@ export interface PositiveIntBrand
11
12
  export const PositiveInt = extendM(
12
13
  S.Int.pipe(
13
14
  S.check(S.isGreaterThan(0)),
14
- fromBrand(nominal<PositiveInt>(), { identifier: "PositiveInt", title: "PositiveInt", jsonSchema: {} }),
15
+ fromBrand<PositiveInt>(nominal<PositiveInt>(), { identifier: "PositiveInt", jsonSchema: {} }),
15
16
  withDefaultMake
16
17
  ),
17
- (s) => ({ withDefault: s.pipe(withDefaultConstructor(() => s(1))) })
18
+ (s) => ({ withDefault: s.pipe(S.withConstructorDefault(Effect.sync(() => s(1)))) })
18
19
  )
19
20
  export type PositiveInt = number & PositiveIntBrand
20
21
 
@@ -22,53 +23,50 @@ export interface NonNegativeIntBrand extends Simplify<B.Brand<"NonNegativeInt">
22
23
  export const NonNegativeInt = extendM(
23
24
  S.Int.pipe(
24
25
  S.check(S.isGreaterThanOrEqualTo(0)),
25
- fromBrand(nominal<NonNegativeInt>(), {
26
+ fromBrand<NonNegativeInt>(nominal<NonNegativeInt>(), {
26
27
  identifier: "NonNegativeInt",
27
- title: "NonNegativeInt",
28
28
  jsonSchema: {}
29
29
  }),
30
30
  withDefaultMake
31
31
  ),
32
- (s) => ({ withDefault: s.pipe(withDefaultConstructor(() => s(0))) })
32
+ (s) => ({ withDefault: s.pipe(S.withConstructorDefault(Effect.sync(() => s(0)))) })
33
33
  )
34
34
  export type NonNegativeInt = number & NonNegativeIntBrand
35
35
 
36
36
  export interface IntBrand extends Simplify<B.Brand<"Int">> {}
37
37
  export const Int = extendM(
38
- S.Int.pipe(fromBrand(nominal<Int>(), { identifier: "Int", title: "Int", jsonSchema: {} }), withDefaultMake),
39
- (s) => ({ withDefault: s.pipe(withDefaultConstructor(() => s(0))) })
38
+ S.Int.pipe(fromBrand<Int>(nominal<Int>(), { identifier: "Int", jsonSchema: {} }), withDefaultMake),
39
+ (s) => ({ withDefault: s.pipe(S.withConstructorDefault(Effect.sync(() => s(0)))) })
40
40
  )
41
41
  export type Int = number & IntBrand
42
42
 
43
43
  export interface PositiveNumberBrand extends Simplify<B.Brand<"PositiveNumber"> & NonNegativeNumberBrand> {}
44
44
  export const PositiveNumber = extendM(
45
- S.Number.pipe(
45
+ S.Finite.pipe(
46
46
  S.check(S.isGreaterThan(0)),
47
- fromBrand(nominal<PositiveNumber>(), {
47
+ fromBrand<PositiveNumber>(nominal<PositiveNumber>(), {
48
48
  identifier: "PositiveNumber",
49
- title: "PositiveNumber",
50
49
  jsonSchema: {}
51
50
  }),
52
51
  withDefaultMake
53
52
  ),
54
- (s) => ({ withDefault: s.pipe(withDefaultConstructor(() => s(1))) })
53
+ (s) => ({ withDefault: s.pipe(S.withConstructorDefault(Effect.sync(() => s(1)))) })
55
54
  )
56
55
  export type PositiveNumber = number & PositiveNumberBrand
57
56
 
58
57
  export interface NonNegativeNumberBrand extends Simplify<B.Brand<"NonNegativeNumber">> {}
59
58
  export const NonNegativeNumber = extendM(
60
59
  S
61
- .Number
60
+ .Finite
62
61
  .pipe(
63
62
  S.check(S.isGreaterThanOrEqualTo(0)),
64
- fromBrand(nominal<NonNegativeNumber>(), {
63
+ fromBrand<NonNegativeNumber>(nominal<NonNegativeNumber>(), {
65
64
  identifier: "NonNegativeNumber",
66
- title: "NonNegativeNumber",
67
65
  jsonSchema: {}
68
66
  }),
69
67
  withDefaultMake
70
68
  ),
71
- (s) => ({ withDefault: s.pipe(withDefaultConstructor(() => s(0))) })
69
+ (s) => ({ withDefault: s.pipe(S.withConstructorDefault(Effect.sync(() => s(0)))) })
72
70
  )
73
71
  export type NonNegativeNumber = number & NonNegativeNumberBrand
74
72
 
@@ -13,9 +13,13 @@ export type PhoneNumber = string & PhoneNumberBrand
13
13
  export const PhoneNumber = S
14
14
  .String
15
15
  .pipe(
16
+ S.annotate({
17
+ title: "PhoneNumber",
18
+ description: "a phone number with at least 7 digits",
19
+ format: "phone"
20
+ }),
16
21
  S.refine(isValidPhone as Refinement<string, PhoneNumber>, {
17
22
  identifier: "PhoneNumber",
18
- title: "PhoneNumber",
19
23
  description: "a phone number with at least 7 digits",
20
24
  jsonSchema: { format: "phone" }
21
25
  }),