@confect/core 10.1.0 → 10.3.0

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/dist/Types.d.ts CHANGED
@@ -22,8 +22,19 @@ export interface TypeDefect<Message extends string, T = never> {
22
22
  }
23
23
  export type IsRecursive<T> = true extends DetectCycle<T> ? true : false;
24
24
  type NonRecursiveLeaf = Brand.Brand<any> | GenericId<any> | ArrayBuffer;
25
- type DetectCycle<T, Cache extends any[] = []> = IsAny<T> extends true ? false : [T] extends [any] ? T extends NonRecursiveLeaf ? false : T extends Cache[number] ? true : T extends Array<infer U> ? DetectCycle<U, [...Cache, T]> : T extends Map<infer _U, infer V> ? DetectCycle<V, [...Cache, T]> : T extends Set<infer U> ? DetectCycle<U, [...Cache, T]> : T extends object ? true extends {
26
- [K in keyof T]: DetectCycle<T[K], [...Cache, T]>;
25
+ type IsSame<A, B> = (<G>() => G extends A ? 1 : 2) extends <G>() => (G extends B ? 1 : 2) ? true : false;
26
+ type IsObject<T> = T extends object ? true : false;
27
+ /**
28
+ * Whether `T` is one of the types already on the path, compared by identity.
29
+ * Assignability is not enough: a nested struct is assignable to an ancestor
30
+ * whenever the ancestor's extra fields are optional, and two structs whose
31
+ * fields are all optional can be assignable to each other in both directions.
32
+ * `DetectCycle` asks only once `T` is assignable to some type on the path,
33
+ * which keeps the identity comparisons off the common, non-recursive case.
34
+ */
35
+ type IsAncestor<T, Ancestors> = Ancestors extends unknown ? IsSame<T, Ancestors> : never;
36
+ type DetectCycle<T, Ancestors = never> = IsAny<T> extends true ? false : [T] extends [any] ? T extends NonRecursiveLeaf ? false : IsObject<T> extends false ? false : (T extends Ancestors ? true extends IsAncestor<T, Ancestors> ? true : false : false) extends true ? true : T extends Array<infer U> ? DetectCycle<U, Ancestors | T> : T extends Map<infer _U, infer V> ? DetectCycle<V, Ancestors | T> : T extends Set<infer U> ? DetectCycle<U, Ancestors | T> : T extends object ? true extends {
37
+ [K in keyof T]: DetectCycle<T[K], Ancestors | T>;
27
38
  }[keyof T] ? true : false : false : never;
28
39
  export type UnionToTuple<t> = _unionToTuple<t, []> extends infer result ? conform<result, t[]> : never;
29
40
  type _unionToTuple<t, result extends unknown[]> = getLastBranch<t> extends infer current ? [t] extends [never] ? result : _unionToTuple<Exclude<t, current>, [current, ...result]> : never;
@@ -1 +1 @@
1
- {"version":3,"file":"Types.d.ts","sourceRoot":"","sources":["../src/Types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,cAAc,EACd,sBAAsB,EACtB,qBAAqB,EACrB,gBAAgB,EAChB,qBAAqB,EACtB,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC/C,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,QAAQ,CAAC;AAEpC,MAAM,MAAM,UAAU,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,IACzC,EAAE,SAAS,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,IAAI,GAAG,KAAK,CAAC;AAEvC,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,IAAI,GAAG,KAAK,CAAC;AAEtD,MAAM,MAAM,OAAO,CAAC,CAAC,EAAE,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,OAAO,GACvD,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,GACb,KAAK,GACL,IAAI,GACN,KAAK,CAAC;AAGV,MAAM,MAAM,cAAc,CAAC,EAAE,IAAI,CAAC,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,GACjD,KAAK,GACL,OAAO,CAAC,EAAE,CAAC,SAAS,IAAI,GACtB,KAAK,GACL,CAAC,EAAE,CAAC,SAAS,CAAC,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,CAAC,GAC/C,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,CAAC,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IAAI,SAAS,SAAS,MAAM,EAAE,GAC5B;KACG,CAAC,IAAI,MAAM,IAAI,GAAG,sBAAsB,CACvC,cAAc,CAAC,eAAe,EAAE,KAAK,CAAC,EACtC,IAAI,CAAC,CAAC,CAAC,CACR;CACF,GACD,KAAK,GACP,KAAK,CAAC;AAGV,MAAM,MAAM,kBAAkB,CAAC,SAAS,SAAS,gBAAgB,IAAI;IACnE,GAAG,EAAE,qBAAqB,CAAC,SAAS,CAAC,CAAC,KAAK,CAAC,CAAC;IAC7C,KAAK,EAAE,qBAAqB,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,CAAC;CAClD,CAAC"}
package/dist/Types.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"Types.js","names":[],"sources":["../src/Types.ts"],"sourcesContent":["import type {\n DocumentByName,\n FieldTypeFromFieldPath,\n GenericDatabaseReader,\n GenericDataModel,\n TableNamesInDataModel,\n} from \"convex/server\";\nimport type { GenericId } from \"convex/values\";\nimport type { Brand } from \"effect\";\n\nexport type IsOptional<T, K extends keyof T> =\n {} extends Pick<T, K> ? true : false;\n\nexport type IsAny<T> = 0 extends 1 & T ? true : false;\n\nexport type IsUnion<T, U extends T = T> = T extends unknown\n ? [U] extends [T]\n ? false\n : true\n : never;\n\n// https://stackoverflow.com/a/52806744\nexport type IsValueLiteral<Vl> = [Vl] extends [never]\n ? never\n : IsUnion<Vl> extends true\n ? false\n : [Vl] extends [string | number | bigint | boolean]\n ? [string] extends [Vl]\n ? false\n : [number] extends [Vl]\n ? false\n : [boolean] extends [Vl]\n ? false\n : [bigint] extends [Vl]\n ? false\n : true\n : false;\n\n/**\n * Only checks for records with string keys.\n */\nexport type IsRecord<T> = [T] extends [never]\n ? false\n : IsUnion<T> extends true\n ? false\n : T extends Record<string, infer V>\n ? string extends keyof T\n ? keyof T extends string\n ? T extends Record<string, V>\n ? Record<string, V> extends T\n ? true\n : false\n : false\n : false\n : false\n : false;\n\nexport type DeepMutable<T> =\n IsAny<T> extends true\n ? any\n : T extends NonRecursiveLeaf\n ? T\n : T extends ReadonlyMap<infer K, infer V>\n ? Map<DeepMutable<K>, DeepMutable<V>>\n : T extends ReadonlySet<infer V>\n ? Set<DeepMutable<V>>\n : [keyof T] extends [never]\n ? T\n : {\n -readonly [K in keyof T]: IsOptional<T, K> extends true\n ? DeepMutable<Exclude<T[K], undefined>>\n : DeepMutable<T[K]>;\n };\n\nexport interface TypeError<Message extends string, T = never> {\n readonly \"~@confect/core/Types/TypeError\": Message;\n readonly \"~value\"?: T;\n}\n\nexport interface TypeDefect<Message extends string, T = never> {\n readonly \"~@confect/core/Types/TypeDefect\": Message;\n readonly \"~value\"?: T;\n}\n\nexport type IsRecursive<T> = true extends DetectCycle<T> ? true : false;\n\ntype NonRecursiveLeaf = Brand.Brand<any> | GenericId<any> | ArrayBuffer;\n\ntype DetectCycle<T, Cache extends any[] = []> =\n IsAny<T> extends true\n ? false\n : [T] extends [any]\n ? T extends NonRecursiveLeaf\n ? false\n : T extends Cache[number]\n ? true\n : T extends Array<infer U>\n ? DetectCycle<U, [...Cache, T]>\n : T extends Map<infer _U, infer V>\n ? DetectCycle<V, [...Cache, T]>\n : T extends Set<infer U>\n ? DetectCycle<U, [...Cache, T]>\n : T extends object\n ? true extends {\n [K in keyof T]: DetectCycle<T[K], [...Cache, T]>;\n }[keyof T]\n ? true\n : false\n : false\n : never;\n\n//////////////////////////////////\n// START: Vendored from Arktype //\n//////////////////////////////////\n\n// https://github.com/arktypeio/arktype/blob/2e911d01a741ccee7a17e31ee144049817fabbb8/ark/util/unionToTuple.ts#L9\n\nexport type UnionToTuple<t> =\n _unionToTuple<t, []> extends infer result ? conform<result, t[]> : never;\n\ntype _unionToTuple<t, result extends unknown[]> =\n getLastBranch<t> extends infer current\n ? [t] extends [never]\n ? result\n : _unionToTuple<Exclude<t, current>, [current, ...result]>\n : never;\n\ntype getLastBranch<t> =\n intersectUnion<t extends unknown ? (x: t) => void : never> extends (\n x: infer branch,\n ) => void\n ? branch\n : never;\n\ntype intersectUnion<t> = (t extends unknown ? (_: t) => void : never) extends (\n _: infer intersection,\n) => void\n ? intersection\n : never;\n\ntype conform<t, base> = t extends base ? t : base;\n\n////////////////////////////////\n// END: Vendored from Arktype //\n////////////////////////////////\n\nexport type IndexFieldTypesForEq<\n ConvexDataModel extends GenericDataModel,\n Table extends TableNamesInDataModel<ConvexDataModel>,\n T extends string[],\n> = T extends readonly [...infer Rest, any]\n ? Rest extends readonly string[]\n ? {\n [K in keyof Rest]: FieldTypeFromFieldPath<\n DocumentByName<ConvexDataModel, Table>,\n Rest[K]\n >;\n }\n : never\n : never;\n\n// Would prefer to use `BaseDatabaseReader` from the `convex` package, but it's not exported.\nexport type BaseDatabaseReader<DataModel extends GenericDataModel> = {\n get: GenericDatabaseReader<DataModel>[\"get\"];\n query: GenericDatabaseReader<DataModel>[\"query\"];\n};\n"],"mappings":""}
1
+ {"version":3,"file":"Types.js","names":[],"sources":["../src/Types.ts"],"sourcesContent":["import type {\n DocumentByName,\n FieldTypeFromFieldPath,\n GenericDatabaseReader,\n GenericDataModel,\n TableNamesInDataModel,\n} from \"convex/server\";\nimport type { GenericId } from \"convex/values\";\nimport type { Brand } from \"effect\";\n\nexport type IsOptional<T, K extends keyof T> =\n {} extends Pick<T, K> ? true : false;\n\nexport type IsAny<T> = 0 extends 1 & T ? true : false;\n\nexport type IsUnion<T, U extends T = T> = T extends unknown\n ? [U] extends [T]\n ? false\n : true\n : never;\n\n// https://stackoverflow.com/a/52806744\nexport type IsValueLiteral<Vl> = [Vl] extends [never]\n ? never\n : IsUnion<Vl> extends true\n ? false\n : [Vl] extends [string | number | bigint | boolean]\n ? [string] extends [Vl]\n ? false\n : [number] extends [Vl]\n ? false\n : [boolean] extends [Vl]\n ? false\n : [bigint] extends [Vl]\n ? false\n : true\n : false;\n\n/**\n * Only checks for records with string keys.\n */\nexport type IsRecord<T> = [T] extends [never]\n ? false\n : IsUnion<T> extends true\n ? false\n : T extends Record<string, infer V>\n ? string extends keyof T\n ? keyof T extends string\n ? T extends Record<string, V>\n ? Record<string, V> extends T\n ? true\n : false\n : false\n : false\n : false\n : false;\n\nexport type DeepMutable<T> =\n IsAny<T> extends true\n ? any\n : T extends NonRecursiveLeaf\n ? T\n : T extends ReadonlyMap<infer K, infer V>\n ? Map<DeepMutable<K>, DeepMutable<V>>\n : T extends ReadonlySet<infer V>\n ? Set<DeepMutable<V>>\n : [keyof T] extends [never]\n ? T\n : {\n -readonly [K in keyof T]: IsOptional<T, K> extends true\n ? DeepMutable<Exclude<T[K], undefined>>\n : DeepMutable<T[K]>;\n };\n\nexport interface TypeError<Message extends string, T = never> {\n readonly \"~@confect/core/Types/TypeError\": Message;\n readonly \"~value\"?: T;\n}\n\nexport interface TypeDefect<Message extends string, T = never> {\n readonly \"~@confect/core/Types/TypeDefect\": Message;\n readonly \"~value\"?: T;\n}\n\nexport type IsRecursive<T> = true extends DetectCycle<T> ? true : false;\n\ntype NonRecursiveLeaf = Brand.Brand<any> | GenericId<any> | ArrayBuffer;\n\ntype IsSame<A, B> =\n (<G>() => G extends A ? 1 : 2) extends <G>() => G extends B ? 1 : 2\n ? true\n : false;\n\ntype IsObject<T> = T extends object ? true : false;\n\n/**\n * Whether `T` is one of the types already on the path, compared by identity.\n * Assignability is not enough: a nested struct is assignable to an ancestor\n * whenever the ancestor's extra fields are optional, and two structs whose\n * fields are all optional can be assignable to each other in both directions.\n * `DetectCycle` asks only once `T` is assignable to some type on the path,\n * which keeps the identity comparisons off the common, non-recursive case.\n */\ntype IsAncestor<T, Ancestors> = Ancestors extends unknown\n ? IsSame<T, Ancestors>\n : never;\n\ntype DetectCycle<T, Ancestors = never> =\n IsAny<T> extends true\n ? false\n : [T] extends [any]\n ? T extends NonRecursiveLeaf\n ? false\n : IsObject<T> extends false\n ? false\n : (\n T extends Ancestors\n ? true extends IsAncestor<T, Ancestors>\n ? true\n : false\n : false\n ) extends true\n ? true\n : T extends Array<infer U>\n ? DetectCycle<U, Ancestors | T>\n : T extends Map<infer _U, infer V>\n ? DetectCycle<V, Ancestors | T>\n : T extends Set<infer U>\n ? DetectCycle<U, Ancestors | T>\n : T extends object\n ? true extends {\n [K in keyof T]: DetectCycle<T[K], Ancestors | T>;\n }[keyof T]\n ? true\n : false\n : false\n : never;\n\n//////////////////////////////////\n// START: Vendored from Arktype //\n//////////////////////////////////\n\n// https://github.com/arktypeio/arktype/blob/2e911d01a741ccee7a17e31ee144049817fabbb8/ark/util/unionToTuple.ts#L9\n\nexport type UnionToTuple<t> =\n _unionToTuple<t, []> extends infer result ? conform<result, t[]> : never;\n\ntype _unionToTuple<t, result extends unknown[]> =\n getLastBranch<t> extends infer current\n ? [t] extends [never]\n ? result\n : _unionToTuple<Exclude<t, current>, [current, ...result]>\n : never;\n\ntype getLastBranch<t> =\n intersectUnion<t extends unknown ? (x: t) => void : never> extends (\n x: infer branch,\n ) => void\n ? branch\n : never;\n\ntype intersectUnion<t> = (t extends unknown ? (_: t) => void : never) extends (\n _: infer intersection,\n) => void\n ? intersection\n : never;\n\ntype conform<t, base> = t extends base ? t : base;\n\n////////////////////////////////\n// END: Vendored from Arktype //\n////////////////////////////////\n\nexport type IndexFieldTypesForEq<\n ConvexDataModel extends GenericDataModel,\n Table extends TableNamesInDataModel<ConvexDataModel>,\n T extends string[],\n> = T extends readonly [...infer Rest, any]\n ? Rest extends readonly string[]\n ? {\n [K in keyof Rest]: FieldTypeFromFieldPath<\n DocumentByName<ConvexDataModel, Table>,\n Rest[K]\n >;\n }\n : never\n : never;\n\n// Would prefer to use `BaseDatabaseReader` from the `convex` package, but it's not exported.\nexport type BaseDatabaseReader<DataModel extends GenericDataModel> = {\n get: GenericDatabaseReader<DataModel>[\"get\"];\n query: GenericDatabaseReader<DataModel>[\"query\"];\n};\n"],"mappings":""}
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@confect/core",
3
3
  "description": "Shared specs and schemas used by all Confect packages",
4
- "version": "10.1.0",
4
+ "version": "10.3.0",
5
5
  "author": "RJ Dellecese",
6
6
  "bugs": {
7
7
  "url": "https://github.com/rjdellecese/confect/issues"
8
8
  },
9
9
  "devDependencies": {
10
- "@effect/vitest": "4.0.1",
11
- "@types/node": "26.6.4",
10
+ "@effect/vitest": "4.0.4",
11
+ "@types/node": "26.6.5",
12
12
  "confect-bench-harness": "0.0.0",
13
- "effect": "4.0.1",
14
- "tsdown": "0.23.0",
13
+ "effect": "4.0.4",
14
+ "tsdown": "0.23.1",
15
15
  "tsx": "4.23.15",
16
16
  "typescript": "7.0.2",
17
17
  "unrun": "0.3.1",
@@ -61,7 +61,7 @@
61
61
  "type": "module",
62
62
  "types": "./dist/index.d.ts",
63
63
  "scripts": {
64
- "bench": "tsx test/Refs.bench.ts",
64
+ "bench": "tsx test/Refs.bench.ts && tsx test/Types.bench.ts",
65
65
  "clean": "shx rm -rf dist coverage node_modules",
66
66
  "dev": "tsdown --watch --no-clean --config-loader unrun",
67
67
  "test": "vitest run",
package/src/GroupSpec.ts CHANGED
@@ -55,21 +55,6 @@ export interface GroupSpec<
55
55
  MiddlewareSpecs_
56
56
  >;
57
57
 
58
- addGroup<Group extends AnyWithProps>(
59
- group: Group,
60
- ): GroupSpec<Runtime, Name_, Functions_, Groups_ | Group, MiddlewareSpecs_>;
61
-
62
- addGroupAt<const AtName extends string, Group extends AnyWithProps>(
63
- name: AtName,
64
- group: Group,
65
- ): GroupSpec<
66
- Runtime,
67
- Name_,
68
- Functions_,
69
- Groups_ | NamedAt<Group, AtName>,
70
- MiddlewareSpecs_
71
- >;
72
-
73
58
  middleware<MiddlewareSpec_ extends MiddlewareSpec.AnyMiddlewareSpec>(
74
59
  middlewareSpec: MiddlewareSpec_ &
75
60
  MiddlewareSpec.ValidateAttach<
@@ -99,16 +84,6 @@ export interface AnyWithProps extends GroupSpec<
99
84
  MiddlewareSpec.AnyMiddlewareSpec
100
85
  > {}
101
86
 
102
- export interface AnyWithPropsWithRuntime<
103
- Runtime extends RuntimeAndFunctionType.Runtime,
104
- > extends GroupSpec<
105
- Runtime,
106
- string,
107
- FunctionSpec.AnyWithPropsWithRuntime<Runtime>,
108
- AnyWithPropsWithRuntime<Runtime>,
109
- MiddlewareSpec.AnyMiddlewareSpec
110
- > {}
111
-
112
87
  export type Name<Group extends AnyWithProps> = Group["name"];
113
88
 
114
89
  export type Functions<Group extends AnyWithProps> = Group["~Functions"];
@@ -146,16 +121,16 @@ export type AddGroups<
146
121
  > =
147
122
  Group extends GroupSpec<
148
123
  infer Runtime,
149
- infer Name_,
124
+ string,
150
125
  infer Functions_,
151
126
  infer Groups_,
152
127
  infer MiddlewareSpecs_
153
128
  >
154
129
  ? GroupSpec<
155
130
  Runtime,
156
- Name_,
131
+ Group["name"],
157
132
  Functions_,
158
- Groups_ | ExtraGroups,
133
+ Exclude<Groups_, { readonly name: Name<ExtraGroups> }> | ExtraGroups,
159
134
  MiddlewareSpecs_
160
135
  >
161
136
  : never;
@@ -196,32 +171,6 @@ const Proto = {
196
171
  });
197
172
  },
198
173
 
199
- addGroup<Group extends Any>(this: Any, group: Group) {
200
- const this_ = this as AnyWithProps;
201
- const group_ = group as unknown as AnyWithProps;
202
-
203
- return makeProto({
204
- runtime: this_.runtime,
205
- name: this_.name,
206
- functions: this_.functions,
207
- groups: Record.set(this_.groups, group_.name, group_),
208
- middlewareAttachments: this_.middlewareAttachments,
209
- });
210
- },
211
-
212
- addGroupAt<Group extends Any>(this: Any, name: string, group: Group) {
213
- const this_ = this as AnyWithProps;
214
- const group_ = group as unknown as AnyWithProps;
215
-
216
- return makeProto({
217
- runtime: this_.runtime,
218
- name: this_.name,
219
- functions: this_.functions,
220
- groups: Record.set(this_.groups, name, withName(name, group_)),
221
- middlewareAttachments: this_.middlewareAttachments,
222
- });
223
- },
224
-
225
174
  middleware<MiddlewareSpec_ extends MiddlewareSpec.AnyMiddlewareSpec>(
226
175
  this: Any,
227
176
  middlewareSpec: MiddlewareSpec_,
@@ -270,7 +219,7 @@ const makeProto = <
270
219
  Runtime extends RuntimeAndFunctionType.Runtime,
271
220
  Name_ extends string,
272
221
  Functions_ extends FunctionSpec.AnyWithPropsWithRuntime<Runtime>,
273
- Groups_ extends AnyWithPropsWithRuntime<Runtime>,
222
+ Groups_ extends AnyWithProps,
274
223
  MiddlewareAttachments_ extends
275
224
  ReadonlyArray<MiddlewareAttachment.MiddlewareAttachment>,
276
225
  >({
@@ -315,20 +264,6 @@ export const make = (): GroupSpec<"Convex", ""> =>
315
264
  middlewareAttachments: [],
316
265
  });
317
266
 
318
- export const makeAt = <const Name_ extends string>(
319
- name: Name_,
320
- ): GroupSpec<"Convex", Name_> => {
321
- validateConfectFunctionIdentifier(name);
322
-
323
- return makeProto({
324
- runtime: "Convex",
325
- name,
326
- functions: Record.empty(),
327
- groups: Record.empty(),
328
- middlewareAttachments: [],
329
- });
330
- };
331
-
332
267
  export const makeNode = (): GroupSpec<"Node", ""> =>
333
268
  makeProto({
334
269
  runtime: "Node",
@@ -338,20 +273,6 @@ export const makeNode = (): GroupSpec<"Node", ""> =>
338
273
  middlewareAttachments: [],
339
274
  });
340
275
 
341
- export const makeNodeAt = <const Name_ extends string>(
342
- name: Name_,
343
- ): GroupSpec<"Node", Name_> => {
344
- validateConfectFunctionIdentifier(name);
345
-
346
- return makeProto({
347
- runtime: "Node",
348
- name,
349
- functions: Record.empty(),
350
- groups: Record.empty(),
351
- middlewareAttachments: [],
352
- });
353
- };
354
-
355
276
  export function withName<const Name_ extends string, Group extends Any>(
356
277
  name: Name_,
357
278
  group: Group,
@@ -373,6 +294,22 @@ export function withName(name: string, group: Any): AnyWithProps {
373
294
  });
374
295
  }
375
296
 
297
+ export const addGroupsAt = (
298
+ group: AnyWithProps,
299
+ children: Record.ReadonlyRecord<string, AnyWithProps>,
300
+ ): AnyWithProps =>
301
+ makeProto({
302
+ runtime: group.runtime,
303
+ name: group.name,
304
+ functions: group.functions,
305
+ groups: Record.union(
306
+ group.groups,
307
+ Record.map(children, (child, name) => withName(name, child)),
308
+ (_existing, incoming) => incoming,
309
+ ),
310
+ middlewareAttachments: group.middlewareAttachments,
311
+ });
312
+
376
313
  export const validateMiddleware = (
377
314
  group: AnyWithProps,
378
315
  ): Result.Result<void, MiddlewareAttachment.ValidationError> =>
@@ -11,7 +11,6 @@ import type {
11
11
  VLiteral,
12
12
  VNull,
13
13
  VObject,
14
- VOptional,
15
14
  VRecord,
16
15
  VString,
17
16
  VUnion,
@@ -130,124 +129,134 @@ export type ValueToValidator<Vl> = [Vl] extends [never]
130
129
  ? never
131
130
  : IsAny<Vl> extends true
132
131
  ? VAny
132
+ : [Vl] extends [string | number | bigint | boolean | null | ArrayBuffer]
133
+ ? ConvertValueToValidator<Vl>
134
+ : IsRecursive<Vl> extends true
135
+ ? VAny
136
+ : [Vl] extends [ReadonlyValue]
137
+ ? ConvertValueToValidator<Vl>
138
+ : TypeError<"Provided value is not a valid Convex value", Vl>;
139
+
140
+ type OptionalValue<Vl, Optionality> = Optionality extends "optional"
141
+ ? Vl | undefined
142
+ : Vl;
143
+
144
+ type ConvertValueToValidator<
145
+ Vl,
146
+ Optionality extends "required" | "optional" = "required",
147
+ > = [Vl] extends [never]
148
+ ? never
149
+ : IsAny<Vl> extends true
150
+ ? VAny<any, Optionality>
133
151
  : [Vl] extends [null]
134
- ? VNull
152
+ ? VNull<OptionalValue<null, Optionality>, Optionality>
135
153
  : [Vl] extends [boolean]
136
154
  ? [boolean] extends [Vl]
137
- ? VBoolean
138
- : VLiteral<Vl>
155
+ ? VBoolean<OptionalValue<boolean, Optionality>, Optionality>
156
+ : VLiteral<OptionalValue<Vl, Optionality>, Optionality>
139
157
  : IsUnion<Vl> extends true
140
- ? IsRecursive<Vl> extends true
141
- ? VAny
142
- : [Vl] extends [ReadonlyValue]
143
- ? UnionValueToValidator<Vl>
144
- : TypeError<"Provided value is not a valid Convex value", Vl>
158
+ ? UnionValueToValidator<Vl, Optionality>
145
159
  : [Vl] extends [number]
146
160
  ? [number] extends [Vl]
147
- ? VFloat64
148
- : VLiteral<Vl>
161
+ ? VFloat64<OptionalValue<number, Optionality>, Optionality>
162
+ : VLiteral<OptionalValue<Vl, Optionality>, Optionality>
149
163
  : [Vl] extends [bigint]
150
164
  ? [bigint] extends [Vl]
151
- ? VInt64
152
- : VLiteral<Vl>
165
+ ? VInt64<OptionalValue<bigint, Optionality>, Optionality>
166
+ : VLiteral<OptionalValue<Vl, Optionality>, Optionality>
153
167
  : [Vl] extends [string]
154
- ? Vl extends {
155
- __tableName: infer TableName extends string;
156
- }
157
- ? VId<GenericId.GenericId<TableName>>
158
- : [string] extends [Vl]
159
- ? VString
160
- : VLiteral<Vl>
168
+ ? StringValueToValidator<Vl, Optionality>
161
169
  : [Vl] extends [ArrayBuffer]
162
- ? VBytes
170
+ ? VBytes<OptionalValue<ArrayBuffer, Optionality>, Optionality>
163
171
  : IsRecursive<Vl> extends true
164
- ? VAny
165
- : [Vl] extends [ReadonlyValue]
166
- ? Vl extends ReadonlyArray<ReadonlyValue>
167
- ? ArrayValueToValidator<Vl>
168
- : Vl extends ReadonlyRecordValue
169
- ? RecordValueToValidator<Vl>
170
- : TypeError<"Unexpected value", Vl>
171
- : TypeError<
172
- "Provided value is not a valid Convex value",
173
- Vl
174
- >;
175
-
176
- type ArrayValueToValidator<Vl extends ReadonlyArray<ReadonlyValue>> =
177
- Vl extends ReadonlyArray<infer El extends ReadonlyValue>
178
- ? ValueToValidator<El> extends infer Vd extends Validator<any, any, any>
179
- ? VArray<MutableValue<El[]>, Vd>
172
+ ? VAny<any, Optionality>
173
+ : Vl extends ReadonlyArray<infer El>
174
+ ? VArray<
175
+ OptionalValue<MutableValue<El[]>, Optionality>,
176
+ ConvertValueToValidator<El>,
177
+ Optionality
178
+ >
179
+ : Vl extends object
180
+ ? ObjectValueToValidator<Vl, Optionality>
181
+ : never;
182
+
183
+ type StringValueToValidator<
184
+ Vl,
185
+ Optionality extends "required" | "optional",
186
+ > = Vl extends { __tableName: infer TableName extends string }
187
+ ? VId<OptionalValue<GenericId.GenericId<TableName>, Optionality>, Optionality>
188
+ : [string] extends [Vl]
189
+ ? VString<OptionalValue<string, Optionality>, Optionality>
190
+ : VLiteral<OptionalValue<Vl, Optionality>, Optionality>;
191
+
192
+ type ObjectValueToValidator<Vl, Optionality extends "required" | "optional"> =
193
+ IsRecord<MutableValue<Vl>> extends true
194
+ ? ValueToValidator<Vl[keyof Vl]> extends infer VdValue extends Validator<
195
+ any,
196
+ "required",
197
+ any
198
+ >
199
+ ? VRecord<
200
+ OptionalValue<MutableValue<Vl>, Optionality>,
201
+ VString,
202
+ VdValue,
203
+ Optionality
204
+ >
180
205
  : never
181
- : never;
206
+ : ObjectFieldsToValidators<Vl> extends infer VdFields extends Record<
207
+ string,
208
+ any
209
+ >
210
+ ? VObject<
211
+ OptionalValue<MutableValue<Vl>, Optionality>,
212
+ VdFields,
213
+ Optionality
214
+ >
215
+ : never;
182
216
 
183
- type RecordValueToValidator<Vl> = Vl extends ReadonlyRecordValue
184
- ? {
185
- -readonly [K in keyof Vl]-?: IsAny<Vl[K]> extends true
186
- ? IsOptional<Vl, K> extends true
187
- ? VOptional<VAny>
188
- : VAny
189
- : UndefinedOrValueToValidator<Vl[K]>;
190
- } extends infer VdRecord extends Record<string, any>
191
- ? MutableValue<Vl> extends infer VlRecord extends Record<string, any>
192
- ? IsRecord<VlRecord> extends true
193
- ? VRecord<VlRecord, VString, VdRecord[keyof VdRecord]>
194
- : VObject<VlRecord, VdRecord>
195
- : never
196
- : never
197
- : never;
217
+ type ObjectFieldsToValidators<Vl> = {
218
+ -readonly [K in keyof Vl]-?: IsAny<Vl[K]> extends true
219
+ ? IsOptional<Vl, K> extends true
220
+ ? VAny<any, "optional">
221
+ : VAny
222
+ : undefined extends Vl[K]
223
+ ? OptionalValueToValidator<Vl[K]>
224
+ : [Vl[K]] extends [ReadonlyValue]
225
+ ? ConvertValueToValidator<Vl[K]>
226
+ : never;
227
+ };
198
228
 
199
229
  export type UndefinedOrValueToValidator<Vl extends ReadonlyValue | undefined> =
200
230
  undefined extends Vl
201
- ? [Vl] extends [(infer Val extends ReadonlyValue) | undefined]
202
- ? ValueToValidator<Val> extends infer Vd extends Validator<
203
- any,
204
- "required",
205
- any
206
- >
207
- ? VOptional<Vd>
208
- : never
209
- : never
231
+ ? OptionalValueToValidator<Vl>
210
232
  : [Vl] extends [ReadonlyValue]
211
233
  ? ValueToValidator<Vl>
212
234
  : never;
213
235
 
214
- type UnionValueToValidator<Vl extends ReadonlyValue> = [Vl] extends [
215
- ReadonlyValue,
216
- ]
217
- ? IsUnion<Vl> extends true
218
- ? UnionToTuple<Vl> extends infer VlTuple extends
219
- ReadonlyArray<ReadonlyValue>
220
- ? ValueTupleToValidatorTuple<VlTuple> extends infer VdTuple extends
221
- Validator<any, "required", any>[]
222
- ? VUnion<MutableValue<Vl>, VdTuple>
223
- : TypeError<"Failed to convert value tuple to validator tuple">
224
- : TypeError<"Failed to convert union to tuple">
225
- : TypeError<"Expected a union of values, but got a single value instead">
226
- : TypeError<"Provided value is not a valid Convex value">;
227
-
228
- type ValueTupleToValidatorTuple<VlTuple extends ReadonlyArray<ReadonlyValue>> =
236
+ type UnionValueToValidator<Vl, Optionality extends "required" | "optional"> =
237
+ IsRecursive<Vl> extends true
238
+ ? VAny<any, Optionality>
239
+ : VUnion<
240
+ OptionalValue<MutableValue<Vl>, Optionality>,
241
+ ValueTupleToValidatorTuple<UnionToTuple<Vl>>,
242
+ Optionality
243
+ >;
244
+
245
+ type ValueTupleToValidatorTuple<VlTuple extends readonly unknown[]> =
229
246
  VlTuple extends
230
- | [true, false, ...infer VlRest extends ReadonlyArray<ReadonlyValue>]
231
- | [false, true, ...infer VlRest extends ReadonlyArray<ReadonlyValue>]
232
- ? ValueTupleToValidatorTuple<VlRest> extends infer VdRest extends Validator<
233
- any,
234
- any,
235
- any
236
- >[]
237
- ? [VBoolean<boolean>, ...VdRest]
238
- : never
239
- : VlTuple extends [
240
- infer Vl extends ReadonlyValue,
241
- ...infer VlRest extends ReadonlyArray<ReadonlyValue>,
242
- ]
243
- ? ValueToValidator<Vl> extends infer Vd extends Validator<any, any, any>
244
- ? ValueTupleToValidatorTuple<VlRest> extends infer VdRest extends
245
- Validator<any, "required", any>[]
246
- ? [Vd, ...VdRest]
247
- : never
248
- : never
247
+ | [true, false, ...infer VlRest]
248
+ | [false, true, ...infer VlRest]
249
+ ? [VBoolean, ...ValueTupleToValidatorTuple<VlRest>]
250
+ : VlTuple extends [infer Vl, ...infer VlRest]
251
+ ? [ConvertValueToValidator<Vl>, ...ValueTupleToValidatorTuple<VlRest>]
249
252
  : [];
250
253
 
254
+ type OptionalValueToValidator<Vl> = [Vl] extends [
255
+ (infer Val extends ReadonlyValue) | undefined,
256
+ ]
257
+ ? ConvertValueToValidator<Val, "optional">
258
+ : never;
259
+
251
260
  export const compileSchema = <T, E>(
252
261
  schema: Schema.Codec<T, E>,
253
262
  ): ValueToValidator<(typeof schema)["Encoded"]> =>
package/src/Spec.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import * as Predicate from "effect/Predicate";
2
2
  import * as Record from "effect/Record";
3
- import * as Struct from "effect/Struct";
4
3
  import * as GroupSpec from "./GroupSpec";
5
4
 
6
5
  export const TypeId = "~@confect/core/Spec";
@@ -20,22 +19,6 @@ export interface Spec<
20
19
  Groups_ extends Record.ReadonlyRecord<string, GroupSpec.AnyWithProps> = {},
21
20
  > {
22
21
  readonly [TypeId]: Readonly<Groups_>;
23
-
24
- add<Group extends GroupSpec.AnyWithProps>(
25
- group: Group,
26
- ): Spec<
27
- Struct.Assign<Groups_, Record.ReadonlyRecord<GroupSpec.Name<Group>, Group>>
28
- >;
29
-
30
- addAt<const Name extends string, Group extends GroupSpec.AnyWithProps>(
31
- name: Name,
32
- group: Group,
33
- ): Spec<
34
- Struct.Assign<
35
- Groups_,
36
- Record.ReadonlyRecord<Name, GroupSpec.NamedAt<Group, Name>>
37
- >
38
- >;
39
22
  }
40
23
 
41
24
  export interface Any {
@@ -56,31 +39,10 @@ export const groups = <Spec_ extends AnyWithProps>(
56
39
  self: Spec_,
57
40
  ): Spec_[TypeId] => self[TypeId];
58
41
 
59
- const makeRecord = <
60
- Groups_ extends Record.ReadonlyRecord<string, GroupSpec.AnyWithProps>,
61
- >(
62
- record: Groups_,
63
- ): Spec<Groups_> => ({
64
- [TypeId]: record,
65
- add<Group extends GroupSpec.AnyWithProps>(group: Group) {
66
- return makeRecord(
67
- Struct.assign(
68
- record,
69
- Record.singleton<GroupSpec.Name<Group>, Group>(group.name, group),
70
- ),
71
- );
72
- },
73
- addAt<const Name extends string, Group extends GroupSpec.AnyWithProps>(
74
- name: Name,
75
- group: Group,
76
- ) {
77
- return makeRecord(
78
- Struct.assign(
79
- record,
80
- Record.singleton(name, GroupSpec.withName(name, group)),
81
- ),
82
- );
83
- },
42
+ export const fromGroups = (
43
+ record: Record.ReadonlyRecord<string, GroupSpec.AnyWithProps>,
44
+ ): AnyWithProps => ({
45
+ [TypeId]: Record.map(record, (group, name) =>
46
+ GroupSpec.withName(name, group),
47
+ ),
84
48
  });
85
-
86
- export const make = (): Spec => makeRecord({});
package/src/Types.ts CHANGED
@@ -86,27 +86,54 @@ export type IsRecursive<T> = true extends DetectCycle<T> ? true : false;
86
86
 
87
87
  type NonRecursiveLeaf = Brand.Brand<any> | GenericId<any> | ArrayBuffer;
88
88
 
89
- type DetectCycle<T, Cache extends any[] = []> =
89
+ type IsSame<A, B> =
90
+ (<G>() => G extends A ? 1 : 2) extends <G>() => G extends B ? 1 : 2
91
+ ? true
92
+ : false;
93
+
94
+ type IsObject<T> = T extends object ? true : false;
95
+
96
+ /**
97
+ * Whether `T` is one of the types already on the path, compared by identity.
98
+ * Assignability is not enough: a nested struct is assignable to an ancestor
99
+ * whenever the ancestor's extra fields are optional, and two structs whose
100
+ * fields are all optional can be assignable to each other in both directions.
101
+ * `DetectCycle` asks only once `T` is assignable to some type on the path,
102
+ * which keeps the identity comparisons off the common, non-recursive case.
103
+ */
104
+ type IsAncestor<T, Ancestors> = Ancestors extends unknown
105
+ ? IsSame<T, Ancestors>
106
+ : never;
107
+
108
+ type DetectCycle<T, Ancestors = never> =
90
109
  IsAny<T> extends true
91
110
  ? false
92
111
  : [T] extends [any]
93
112
  ? T extends NonRecursiveLeaf
94
113
  ? false
95
- : T extends Cache[number]
96
- ? true
97
- : T extends Array<infer U>
98
- ? DetectCycle<U, [...Cache, T]>
99
- : T extends Map<infer _U, infer V>
100
- ? DetectCycle<V, [...Cache, T]>
101
- : T extends Set<infer U>
102
- ? DetectCycle<U, [...Cache, T]>
103
- : T extends object
104
- ? true extends {
105
- [K in keyof T]: DetectCycle<T[K], [...Cache, T]>;
106
- }[keyof T]
114
+ : IsObject<T> extends false
115
+ ? false
116
+ : (
117
+ T extends Ancestors
118
+ ? true extends IsAncestor<T, Ancestors>
107
119
  ? true
108
120
  : false
109
121
  : false
122
+ ) extends true
123
+ ? true
124
+ : T extends Array<infer U>
125
+ ? DetectCycle<U, Ancestors | T>
126
+ : T extends Map<infer _U, infer V>
127
+ ? DetectCycle<V, Ancestors | T>
128
+ : T extends Set<infer U>
129
+ ? DetectCycle<U, Ancestors | T>
130
+ : T extends object
131
+ ? true extends {
132
+ [K in keyof T]: DetectCycle<T[K], Ancestors | T>;
133
+ }[keyof T]
134
+ ? true
135
+ : false
136
+ : false
110
137
  : never;
111
138
 
112
139
  //////////////////////////////////