@confect/core 10.2.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/CHANGELOG.md +14 -0
- package/dist/GroupSpec.d.ts +5 -7
- package/dist/GroupSpec.d.ts.map +1 -1
- package/dist/GroupSpec.js +9 -45
- package/dist/GroupSpec.js.map +1 -1
- package/dist/SchemaToValidator.d.ts +16 -15
- package/dist/SchemaToValidator.d.ts.map +1 -1
- package/dist/SchemaToValidator.js.map +1 -1
- package/dist/Spec.d.ts +1 -4
- package/dist/Spec.d.ts.map +1 -1
- package/dist/Spec.js +4 -14
- package/dist/Spec.js.map +1 -1
- package/dist/SystemFields.js +1 -1
- package/dist/Types.d.ts +13 -2
- package/dist/Types.d.ts.map +1 -1
- package/dist/Types.js.map +1 -1
- package/package.json +6 -6
- package/src/GroupSpec.ts +20 -83
- package/src/SchemaToValidator.ts +105 -96
- package/src/Spec.ts +6 -44
- package/src/Types.ts +40 -13
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @confect/core
|
|
2
2
|
|
|
3
|
+
## 10.3.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 33eaf6a: Reduce the initialization cost of generated function references in projects with many groups.
|
|
8
|
+
|
|
9
|
+
Fix `confect/_generated/spec.ts` failing to typecheck with "Type instantiation is excessively deep and possibly infinite" (TS2589) in projects with many top-level or child groups, while keeping `refs` precisely typed.
|
|
10
|
+
|
|
11
|
+
Run `confect codegen` after upgrading to regenerate existing files before typechecking or deploying. User-authored group definitions using `GroupSpec.make()`, `GroupSpec.makeNode()`, `.addFunction()`, and `.middleware()` do not need to change.
|
|
12
|
+
|
|
13
|
+
- 52e4397: Stop reporting a nested struct as recursive when it is merely assignable to one of its ancestors. `IsRecursive` compared a type against the types above it by assignability, so a struct nested under a parent whose extra fields are all optional (for example `{ badge: string }` inside `{ badge: string; rows?: { badge: string }[] }`) was treated as a cycle: its validator type became `VAny`, and a table built from it had a `never` document type and rejected its own index fields. Types already on the path are now compared by identity.
|
|
14
|
+
- bc41806: Type a record of `Schema.Any` with a required value validator. `Schema.Record(Schema.String, Schema.Any)` compiled to `v.record(v.string(), v.any())` at runtime, but its type carried an optional `VAny` value, which `VRecord` does not accept, so a table with such a field could not be passed to `Table.tableDefinition`.
|
|
15
|
+
- 5ea7c07: Reduce TypeScript type-checking work for table schemas with nested objects, arrays, unions, and optional fields while preserving their inferred types.
|
|
16
|
+
|
|
3
17
|
## 10.2.0
|
|
4
18
|
|
|
5
19
|
No changes in this release.
|
package/dist/GroupSpec.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as MiddlewareAttachment from "./MiddlewareAttachment";
|
|
2
|
+
import * as Record from "effect/Record";
|
|
2
3
|
import * as Result from "effect/Result";
|
|
3
4
|
import type * as FunctionSpec from "./FunctionSpec";
|
|
4
5
|
import type * as MiddlewareSpec from "./MiddlewareSpec";
|
|
@@ -21,8 +22,6 @@ export interface GroupSpec<Runtime extends RuntimeAndFunctionType.Runtime, Name_
|
|
|
21
22
|
readonly "~Functions": Functions_;
|
|
22
23
|
readonly "~Groups": Groups_;
|
|
23
24
|
addFunction<Function extends FunctionSpec.AnyWithPropsWithRuntime<Runtime>>(function_: Function & MiddlewareSpec.ValidateAddedFunction<Function, MiddlewareSpecs_>): GroupSpec<Runtime, Name_, Functions_ | Function, Groups_, MiddlewareSpecs_>;
|
|
24
|
-
addGroup<Group extends AnyWithProps>(group: Group): GroupSpec<Runtime, Name_, Functions_, Groups_ | Group, MiddlewareSpecs_>;
|
|
25
|
-
addGroupAt<const AtName extends string, Group extends AnyWithProps>(name: AtName, group: Group): GroupSpec<Runtime, Name_, Functions_, Groups_ | NamedAt<Group, AtName>, MiddlewareSpecs_>;
|
|
26
25
|
middleware<MiddlewareSpec_ extends MiddlewareSpec.AnyMiddlewareSpec>(middlewareSpec: MiddlewareSpec_ & MiddlewareSpec.ValidateAttach<MiddlewareSpec_, Functions_, MiddlewareSpecs_>, ...options: MiddlewareAttachment.Args<NoInfer<MiddlewareSpec_>>): GroupSpec<Runtime, Name_, Functions_, Groups_, MiddlewareSpecs_ | MiddlewareSpec_>;
|
|
27
26
|
}
|
|
28
27
|
export interface Any {
|
|
@@ -30,8 +29,6 @@ export interface Any {
|
|
|
30
29
|
}
|
|
31
30
|
export interface AnyWithProps extends GroupSpec<RuntimeAndFunctionType.Runtime, string, FunctionSpec.AnyWithProps, AnyWithProps, MiddlewareSpec.AnyMiddlewareSpec> {
|
|
32
31
|
}
|
|
33
|
-
export interface AnyWithPropsWithRuntime<Runtime extends RuntimeAndFunctionType.Runtime> extends GroupSpec<Runtime, string, FunctionSpec.AnyWithPropsWithRuntime<Runtime>, AnyWithPropsWithRuntime<Runtime>, MiddlewareSpec.AnyMiddlewareSpec> {
|
|
34
|
-
}
|
|
35
32
|
export type Name<Group extends AnyWithProps> = Group["name"];
|
|
36
33
|
export type Functions<Group extends AnyWithProps> = Group["~Functions"];
|
|
37
34
|
export type Groups<Group extends AnyWithProps> = Group["~Groups"];
|
|
@@ -49,11 +46,12 @@ export type WithName<Group extends AnyWithProps, Name_ extends Name<Group>> = Ex
|
|
|
49
46
|
export type NamedAt<Group extends Any, Name_ extends string> = Omit<Group, "name"> & {
|
|
50
47
|
readonly name: Name_;
|
|
51
48
|
};
|
|
52
|
-
export type AddGroups<Group extends AnyWithProps, ExtraGroups extends AnyWithProps> = Group extends GroupSpec<infer Runtime,
|
|
49
|
+
export type AddGroups<Group extends AnyWithProps, ExtraGroups extends AnyWithProps> = Group extends GroupSpec<infer Runtime, string, infer Functions_, infer Groups_, infer MiddlewareSpecs_> ? GroupSpec<Runtime, Group["name"], Functions_, Exclude<Groups_, {
|
|
50
|
+
readonly name: Name<ExtraGroups>;
|
|
51
|
+
}> | ExtraGroups, MiddlewareSpecs_> : never;
|
|
53
52
|
export declare const make: () => GroupSpec<"Convex", "">;
|
|
54
|
-
export declare const makeAt: <const Name_ extends string>(name: Name_) => GroupSpec<"Convex", Name_>;
|
|
55
53
|
export declare const makeNode: () => GroupSpec<"Node", "">;
|
|
56
|
-
export declare const makeNodeAt: <const Name_ extends string>(name: Name_) => GroupSpec<"Node", Name_>;
|
|
57
54
|
export declare function withName<const Name_ extends string, Group extends Any>(name: Name_, group: Group): NamedAt<Group, Name_>;
|
|
55
|
+
export declare const addGroupsAt: (group: AnyWithProps, children: Record.ReadonlyRecord<string, AnyWithProps>) => AnyWithProps;
|
|
58
56
|
export declare const validateMiddleware: (group: AnyWithProps) => Result.Result<void, MiddlewareAttachment.ValidationError>;
|
|
59
57
|
//# sourceMappingURL=GroupSpec.d.ts.map
|
package/dist/GroupSpec.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GroupSpec.d.ts","sourceRoot":"","sources":["../src/GroupSpec.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,oBAAoB,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"GroupSpec.d.ts","sourceRoot":"","sources":["../src/GroupSpec.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,oBAAoB,MAAM,wBAAwB,CAAC;AAE/D,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,KAAK,YAAY,MAAM,gBAAgB,CAAC;AACpD,OAAO,KAAK,KAAK,cAAc,MAAM,kBAAkB,CAAC;AACxD,OAAO,KAAK,KAAK,sBAAsB,MAAM,0BAA0B,CAAC;AAGxE,eAAO,MAAM,MAAM,6BAA6B,CAAC;AACjD,MAAM,MAAM,MAAM,GAAG,OAAO,MAAM,CAAC;AAEnC,eAAO,MAAM,WAAW,MAAO,OAAO,KAAG,CAAC,IAAI,YACZ,CAAC;AAEnC,MAAM,WAAW,SAAS,CACxB,OAAO,SAAS,sBAAsB,CAAC,OAAO,EAC9C,KAAK,SAAS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
|
9
9
|
TypeId: () => TypeId,
|
|
10
|
+
addGroupsAt: () => addGroupsAt,
|
|
10
11
|
isGroupSpec: () => isGroupSpec,
|
|
11
12
|
make: () => make,
|
|
12
|
-
makeAt: () => makeAt,
|
|
13
13
|
makeNode: () => makeNode,
|
|
14
|
-
makeNodeAt: () => makeNodeAt,
|
|
15
14
|
validateMiddleware: () => validateMiddleware,
|
|
16
15
|
withName: () => withName
|
|
17
16
|
});
|
|
@@ -34,28 +33,6 @@ const Proto = {
|
|
|
34
33
|
middlewareAttachments: this_.middlewareAttachments
|
|
35
34
|
});
|
|
36
35
|
},
|
|
37
|
-
addGroup(group) {
|
|
38
|
-
const this_ = this;
|
|
39
|
-
const group_ = group;
|
|
40
|
-
return makeProto({
|
|
41
|
-
runtime: this_.runtime,
|
|
42
|
-
name: this_.name,
|
|
43
|
-
functions: this_.functions,
|
|
44
|
-
groups: Record.set(this_.groups, group_.name, group_),
|
|
45
|
-
middlewareAttachments: this_.middlewareAttachments
|
|
46
|
-
});
|
|
47
|
-
},
|
|
48
|
-
addGroupAt(name, group) {
|
|
49
|
-
const this_ = this;
|
|
50
|
-
const group_ = group;
|
|
51
|
-
return makeProto({
|
|
52
|
-
runtime: this_.runtime,
|
|
53
|
-
name: this_.name,
|
|
54
|
-
functions: this_.functions,
|
|
55
|
-
groups: Record.set(this_.groups, name, withName(name, group_)),
|
|
56
|
-
middlewareAttachments: this_.middlewareAttachments
|
|
57
|
-
});
|
|
58
|
-
},
|
|
59
36
|
middleware(middlewareSpec, ...options) {
|
|
60
37
|
const this_ = this;
|
|
61
38
|
if (!("options" in middlewareSpec) && this_.middlewareAttachments.some(({ spec }) => spec.key === middlewareSpec.key)) throw new Error(`Middleware "${middlewareSpec.key}" is already attached to this group`);
|
|
@@ -86,16 +63,6 @@ const make = () => makeProto({
|
|
|
86
63
|
groups: Record.empty(),
|
|
87
64
|
middlewareAttachments: []
|
|
88
65
|
});
|
|
89
|
-
const makeAt = (name) => {
|
|
90
|
-
validateConfectFunctionIdentifier(name);
|
|
91
|
-
return makeProto({
|
|
92
|
-
runtime: "Convex",
|
|
93
|
-
name,
|
|
94
|
-
functions: Record.empty(),
|
|
95
|
-
groups: Record.empty(),
|
|
96
|
-
middlewareAttachments: []
|
|
97
|
-
});
|
|
98
|
-
};
|
|
99
66
|
const makeNode = () => makeProto({
|
|
100
67
|
runtime: "Node",
|
|
101
68
|
name: "",
|
|
@@ -103,16 +70,6 @@ const makeNode = () => makeProto({
|
|
|
103
70
|
groups: Record.empty(),
|
|
104
71
|
middlewareAttachments: []
|
|
105
72
|
});
|
|
106
|
-
const makeNodeAt = (name) => {
|
|
107
|
-
validateConfectFunctionIdentifier(name);
|
|
108
|
-
return makeProto({
|
|
109
|
-
runtime: "Node",
|
|
110
|
-
name,
|
|
111
|
-
functions: Record.empty(),
|
|
112
|
-
groups: Record.empty(),
|
|
113
|
-
middlewareAttachments: []
|
|
114
|
-
});
|
|
115
|
-
};
|
|
116
73
|
function withName(name, group) {
|
|
117
74
|
validateConfectFunctionIdentifier(name);
|
|
118
75
|
const group_ = group;
|
|
@@ -125,12 +82,19 @@ function withName(name, group) {
|
|
|
125
82
|
middlewareAttachments: group_.middlewareAttachments
|
|
126
83
|
});
|
|
127
84
|
}
|
|
85
|
+
const addGroupsAt = (group, children) => makeProto({
|
|
86
|
+
runtime: group.runtime,
|
|
87
|
+
name: group.name,
|
|
88
|
+
functions: group.functions,
|
|
89
|
+
groups: Record.union(group.groups, Record.map(children, (child, name) => withName(name, child)), (_existing, incoming) => incoming),
|
|
90
|
+
middlewareAttachments: group.middlewareAttachments
|
|
91
|
+
});
|
|
128
92
|
const validateMiddleware = (group) => Result.gen(function* () {
|
|
129
93
|
yield* validateAll(group.middlewareAttachments, `group "${group.name}"`);
|
|
130
94
|
for (const function_ of Object.values(group.functions)) yield* validateAll([...group.middlewareAttachments, ...function_.middlewareAttachments], `function "${function_.name}"`);
|
|
131
95
|
for (const child of Object.values(group.groups)) yield* validateMiddleware(child);
|
|
132
96
|
});
|
|
133
97
|
//#endregion
|
|
134
|
-
export { GroupSpec_exports, TypeId, isGroupSpec, make,
|
|
98
|
+
export { GroupSpec_exports, TypeId, addGroupsAt, isGroupSpec, make, makeNode, validateMiddleware, withName };
|
|
135
99
|
|
|
136
100
|
//# sourceMappingURL=GroupSpec.js.map
|
package/dist/GroupSpec.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GroupSpec.js","names":["MiddlewareAttachment.validateAll"],"sources":["../src/GroupSpec.ts"],"sourcesContent":["import * as MiddlewareAttachment from \"./MiddlewareAttachment\";\nimport * as Predicate from \"effect/Predicate\";\nimport * as Record from \"effect/Record\";\nimport * as Result from \"effect/Result\";\nimport type * as FunctionSpec from \"./FunctionSpec\";\nimport type * as MiddlewareSpec from \"./MiddlewareSpec\";\nimport type * as RuntimeAndFunctionType from \"./RuntimeAndFunctionType\";\nimport { validateConfectFunctionIdentifier } from \"./Identifier\";\n\nexport const TypeId = \"~@confect/core/GroupSpec\";\nexport type TypeId = typeof TypeId;\n\nexport const isGroupSpec = (u: unknown): u is AnyWithProps =>\n Predicate.hasProperty(u, TypeId);\n\nexport interface GroupSpec<\n Runtime extends RuntimeAndFunctionType.Runtime,\n Name_ extends string,\n Functions_ extends FunctionSpec.AnyWithPropsWithRuntime<Runtime> = never,\n // Subgroups may be of any runtime, independent of this group's own runtime: a\n // group is only a namespace for its children, which are otherwise-independent\n // modules. Functions, by contrast, stay homogeneous (a Node group only accepts\n // Node actions)—`addFunction` keeps the `<Runtime>` bound below.\n Groups_ extends AnyWithProps = never,\n MiddlewareSpecs_ extends MiddlewareSpec.AnyMiddlewareSpec = never,\n> {\n readonly [TypeId]: TypeId;\n readonly runtime: Runtime;\n readonly name: Name_;\n readonly functions: {\n [\n FunctionName in FunctionSpec.Name<\n FunctionSpec.AnyWithPropsWithRuntime<Runtime>\n >\n ]: FunctionSpec.WithName<Functions_, FunctionName>;\n };\n readonly groups: {\n [GroupName in Name<Groups_>]: WithName<Groups_, GroupName>;\n };\n readonly middlewareSpecs: ReadonlyArray<MiddlewareSpecs_>;\n readonly middlewareAttachments: ReadonlyArray<\n MiddlewareAttachment.MiddlewareAttachment<MiddlewareSpecs_>\n >;\n readonly \"~Functions\": Functions_;\n readonly \"~Groups\": Groups_;\n\n addFunction<Function extends FunctionSpec.AnyWithPropsWithRuntime<Runtime>>(\n function_: Function &\n MiddlewareSpec.ValidateAddedFunction<Function, MiddlewareSpecs_>,\n ): GroupSpec<\n Runtime,\n Name_,\n Functions_ | Function,\n Groups_,\n MiddlewareSpecs_\n >;\n\n addGroup<Group extends AnyWithProps>(\n group: Group,\n ): GroupSpec<Runtime, Name_, Functions_, Groups_ | Group, MiddlewareSpecs_>;\n\n addGroupAt<const AtName extends string, Group extends AnyWithProps>(\n name: AtName,\n group: Group,\n ): GroupSpec<\n Runtime,\n Name_,\n Functions_,\n Groups_ | NamedAt<Group, AtName>,\n MiddlewareSpecs_\n >;\n\n middleware<MiddlewareSpec_ extends MiddlewareSpec.AnyMiddlewareSpec>(\n middlewareSpec: MiddlewareSpec_ &\n MiddlewareSpec.ValidateAttach<\n MiddlewareSpec_,\n Functions_,\n MiddlewareSpecs_\n >,\n ...options: MiddlewareAttachment.Args<NoInfer<MiddlewareSpec_>>\n ): GroupSpec<\n Runtime,\n Name_,\n Functions_,\n Groups_,\n MiddlewareSpecs_ | MiddlewareSpec_\n >;\n}\n\nexport interface Any {\n readonly [TypeId]: TypeId;\n}\n\nexport interface AnyWithProps extends GroupSpec<\n RuntimeAndFunctionType.Runtime,\n string,\n FunctionSpec.AnyWithProps,\n AnyWithProps,\n MiddlewareSpec.AnyMiddlewareSpec\n> {}\n\nexport interface AnyWithPropsWithRuntime<\n Runtime extends RuntimeAndFunctionType.Runtime,\n> extends GroupSpec<\n Runtime,\n string,\n FunctionSpec.AnyWithPropsWithRuntime<Runtime>,\n AnyWithPropsWithRuntime<Runtime>,\n MiddlewareSpec.AnyMiddlewareSpec\n> {}\n\nexport type Name<Group extends AnyWithProps> = Group[\"name\"];\n\nexport type Functions<Group extends AnyWithProps> = Group[\"~Functions\"];\n\nexport type Groups<Group extends AnyWithProps> = Group[\"~Groups\"];\n\nexport type MiddlewareSpecs<Group extends AnyWithProps> =\n Group[\"middlewareSpecs\"][number];\n\nexport type GroupNames<Group extends AnyWithProps> = [Groups<Group>] extends [\n never,\n]\n ? never\n : Name<Groups<Group>>;\n\nexport type WithName<\n Group extends AnyWithProps,\n Name_ extends Name<Group>,\n> = Extract<Group, { readonly name: Name_ }>;\n\n/**\n * Assigns a segment name to a leaf group created with {@link make} for typing\n * and refs.\n */\nexport type NamedAt<Group extends Any, Name_ extends string> = Omit<\n Group,\n \"name\"\n> & {\n readonly name: Name_;\n};\n\nexport type AddGroups<\n Group extends AnyWithProps,\n ExtraGroups extends AnyWithProps,\n> =\n Group extends GroupSpec<\n infer Runtime,\n infer Name_,\n infer Functions_,\n infer Groups_,\n infer MiddlewareSpecs_\n >\n ? GroupSpec<\n Runtime,\n Name_,\n Functions_,\n Groups_ | ExtraGroups,\n MiddlewareSpecs_\n >\n : never;\n\nconst Proto = {\n [TypeId]: TypeId,\n\n get middlewareSpecs() {\n return this.middlewareAttachments.map(({ spec }) => spec);\n },\n\n addFunction<Function extends FunctionSpec.AnyWithProps>(\n this: Any,\n function_: Function,\n ) {\n const this_ = this as AnyWithProps;\n\n const overlapping = function_.middlewareAttachments.find(\n ({ spec: functionMiddlewareSpec }) =>\n !(\"options\" in functionMiddlewareSpec) &&\n this_.middlewareAttachments.some(\n ({ spec: groupMiddlewareSpec }) =>\n groupMiddlewareSpec.key === functionMiddlewareSpec.key,\n ),\n );\n if (overlapping !== undefined) {\n throw new Error(\n `Middleware \"${overlapping.spec.key}\" is attached to both function \"${function_.name}\" and its group`,\n );\n }\n\n return makeProto({\n runtime: this_.runtime,\n name: this_.name,\n functions: Record.set(this_.functions, function_.name, function_),\n groups: this_.groups,\n middlewareAttachments: this_.middlewareAttachments,\n });\n },\n\n addGroup<Group extends Any>(this: Any, group: Group) {\n const this_ = this as AnyWithProps;\n const group_ = group as unknown as AnyWithProps;\n\n return makeProto({\n runtime: this_.runtime,\n name: this_.name,\n functions: this_.functions,\n groups: Record.set(this_.groups, group_.name, group_),\n middlewareAttachments: this_.middlewareAttachments,\n });\n },\n\n addGroupAt<Group extends Any>(this: Any, name: string, group: Group) {\n const this_ = this as AnyWithProps;\n const group_ = group as unknown as AnyWithProps;\n\n return makeProto({\n runtime: this_.runtime,\n name: this_.name,\n functions: this_.functions,\n groups: Record.set(this_.groups, name, withName(name, group_)),\n middlewareAttachments: this_.middlewareAttachments,\n });\n },\n\n middleware<MiddlewareSpec_ extends MiddlewareSpec.AnyMiddlewareSpec>(\n this: Any,\n middlewareSpec: MiddlewareSpec_,\n ...options: ReadonlyArray<unknown>\n ) {\n const this_ = this as AnyWithProps;\n\n if (\n !(\"options\" in middlewareSpec) &&\n this_.middlewareAttachments.some(\n ({ spec }) => spec.key === middlewareSpec.key,\n )\n ) {\n throw new Error(\n `Middleware \"${middlewareSpec.key}\" is already attached to this group`,\n );\n }\n\n for (const function_ of Object.values(this_.functions)) {\n if (\n !(\"options\" in middlewareSpec) &&\n function_.middlewareAttachments.some(\n ({ spec }) => spec.key === middlewareSpec.key,\n )\n ) {\n throw new Error(\n `Middleware \"${middlewareSpec.key}\" is attached to both function \"${function_.name}\" and its group`,\n );\n }\n }\n\n return makeProto({\n runtime: this_.runtime,\n name: this_.name,\n functions: this_.functions,\n groups: this_.groups,\n middlewareAttachments: [\n ...this_.middlewareAttachments,\n { spec: middlewareSpec, options: options[0] },\n ],\n });\n },\n} satisfies ThisType<AnyWithProps>;\n\nconst makeProto = <\n Runtime extends RuntimeAndFunctionType.Runtime,\n Name_ extends string,\n Functions_ extends FunctionSpec.AnyWithPropsWithRuntime<Runtime>,\n Groups_ extends AnyWithPropsWithRuntime<Runtime>,\n MiddlewareAttachments_ extends\n ReadonlyArray<MiddlewareAttachment.MiddlewareAttachment>,\n>({\n runtime,\n name,\n functions,\n groups,\n middlewareAttachments,\n}: {\n runtime: Runtime;\n name: Name_;\n functions: Record.ReadonlyRecord<string, Functions_>;\n groups: Record.ReadonlyRecord<string, Groups_>;\n middlewareAttachments: MiddlewareAttachments_;\n}): GroupSpec<\n Runtime,\n Name_,\n Functions_,\n Groups_,\n MiddlewareAttachments_[number][\"spec\"]\n> =>\n Object.assign(Object.create(Proto), {\n runtime,\n name,\n functions,\n groups,\n middlewareAttachments,\n }) as GroupSpec<\n Runtime,\n Name_,\n Functions_,\n Groups_,\n MiddlewareAttachments_[number][\"spec\"]\n >;\n\nexport const make = (): GroupSpec<\"Convex\", \"\"> =>\n makeProto({\n runtime: \"Convex\",\n name: \"\",\n functions: Record.empty(),\n groups: Record.empty(),\n middlewareAttachments: [],\n });\n\nexport const makeAt = <const Name_ extends string>(\n name: Name_,\n): GroupSpec<\"Convex\", Name_> => {\n validateConfectFunctionIdentifier(name);\n\n return makeProto({\n runtime: \"Convex\",\n name,\n functions: Record.empty(),\n groups: Record.empty(),\n middlewareAttachments: [],\n });\n};\n\nexport const makeNode = (): GroupSpec<\"Node\", \"\"> =>\n makeProto({\n runtime: \"Node\",\n name: \"\",\n functions: Record.empty(),\n groups: Record.empty(),\n middlewareAttachments: [],\n });\n\nexport const makeNodeAt = <const Name_ extends string>(\n name: Name_,\n): GroupSpec<\"Node\", Name_> => {\n validateConfectFunctionIdentifier(name);\n\n return makeProto({\n runtime: \"Node\",\n name,\n functions: Record.empty(),\n groups: Record.empty(),\n middlewareAttachments: [],\n });\n};\n\nexport function withName<const Name_ extends string, Group extends Any>(\n name: Name_,\n group: Group,\n): NamedAt<Group, Name_>;\nexport function withName(name: string, group: Any): AnyWithProps {\n validateConfectFunctionIdentifier(name);\n const group_ = group as AnyWithProps;\n\n if (group_.name === name) {\n return group_;\n }\n\n return makeProto({\n runtime: group_.runtime,\n name,\n functions: group_.functions,\n groups: group_.groups,\n middlewareAttachments: group_.middlewareAttachments,\n });\n}\n\nexport const validateMiddleware = (\n group: AnyWithProps,\n): Result.Result<void, MiddlewareAttachment.ValidationError> =>\n Result.gen(function* () {\n yield* MiddlewareAttachment.validateAll(\n group.middlewareAttachments,\n `group \"${group.name}\"`,\n );\n for (const function_ of Object.values(group.functions)) {\n yield* MiddlewareAttachment.validateAll(\n [...group.middlewareAttachments, ...function_.middlewareAttachments],\n `function \"${function_.name}\"`,\n );\n }\n for (const child of Object.values(group.groups))\n yield* validateMiddleware(child);\n });\n"],"mappings":";;;;;;;;;;;;;;;;;AASA,MAAa,SAAS;AAGtB,MAAa,eAAe,MAC1B,UAAU,YAAY,GAAG,MAAM;AAqJjC,MAAM,QAAQ;EACX,SAAS;CAEV,IAAI,kBAAkB;EACpB,OAAO,KAAK,sBAAsB,KAAK,EAAE,WAAW,IAAI;CAC1D;CAEA,YAEE,WACA;EACA,MAAM,QAAQ;EAEd,MAAM,cAAc,UAAU,sBAAsB,MACjD,EAAE,MAAM,6BACP,EAAE,aAAa,2BACf,MAAM,sBAAsB,MACzB,EAAE,MAAM,0BACP,oBAAoB,QAAQ,uBAAuB,GACvD,CACJ;EACA,IAAI,gBAAgB,KAAA,GAClB,MAAM,IAAI,MACR,eAAe,YAAY,KAAK,IAAI,kCAAkC,UAAU,KAAK,gBACvF;EAGF,OAAO,UAAU;GACf,SAAS,MAAM;GACf,MAAM,MAAM;GACZ,WAAW,OAAO,IAAI,MAAM,WAAW,UAAU,MAAM,SAAS;GAChE,QAAQ,MAAM;GACd,uBAAuB,MAAM;EAC/B,CAAC;CACH;CAEA,SAAuC,OAAc;EACnD,MAAM,QAAQ;EACd,MAAM,SAAS;EAEf,OAAO,UAAU;GACf,SAAS,MAAM;GACf,MAAM,MAAM;GACZ,WAAW,MAAM;GACjB,QAAQ,OAAO,IAAI,MAAM,QAAQ,OAAO,MAAM,MAAM;GACpD,uBAAuB,MAAM;EAC/B,CAAC;CACH;CAEA,WAAyC,MAAc,OAAc;EACnE,MAAM,QAAQ;EACd,MAA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ACN,WAAW,OAAO,MAAM;CACxB,QAAQ,OAAO,MAAM;CACrB,uBAAuB,CAAC;AAC1B,CAAC;AAEH,MAAa,cACX,SAC6B;CAC7B,kCAAkC,IAAI;CAEtC,OAAO,UAAU;EACf,SAAS;EACT;EACA,WAAW,OAAO,MAAM;EACxB,QAAQ,OAAO,MAAM;EACrB,uBAAuB,CAAC;CAC1B,CAAC;AACH;AAMA,SAAgB,SAAS,MAAc,OAA0B;CAC/D,kCAAkC,IAAI;CACtC,MAAM,SAAS;CAEf,IAAI,OAAO,SAAS,MAClB,OAAO;CAGT,OAAO,UAAU;EACf,SAAS,OAAO;EAChB;EACA,WAAW,OAAO;EAClB,QAAQ,OAAO;EACf,uBAAuB,OAAO;CAChC,CAAC;AACH;AAEA,MAAa,sBACX,UAEA,OAAO,IAAI,aAAa;CACtB,OAAOA,YACL,MAAM,uBACN,UAAU,MAAM,KAAK,EACvB;CACA,KAAK,MAAM,aAAa,OAAO,OAAO,MAAM,SAAS,GACnD,OAAOA,YACL,CAAC,GAAG,MAAM,uBAAuB,GAAG,UAAU,qBAAqB,GACnE,aAAa,UAAU,KAAK,EAC9B;CAEF,KAAK,MAAM,SAAS,OAAO,OAAO,MAAM,MAAM,GAC5C,OAAO,mBAAmB,KAAK;AACnC,CAAC"}
|
|
1
|
+
{"version":3,"file":"GroupSpec.js","names":["MiddlewareAttachment.validateAll"],"sources":["../src/GroupSpec.ts"],"sourcesContent":["import * as MiddlewareAttachment from \"./MiddlewareAttachment\";\nimport * as Predicate from \"effect/Predicate\";\nimport * as Record from \"effect/Record\";\nimport * as Result from \"effect/Result\";\nimport type * as FunctionSpec from \"./FunctionSpec\";\nimport type * as MiddlewareSpec from \"./MiddlewareSpec\";\nimport type * as RuntimeAndFunctionType from \"./RuntimeAndFunctionType\";\nimport { validateConfectFunctionIdentifier } from \"./Identifier\";\n\nexport const TypeId = \"~@confect/core/GroupSpec\";\nexport type TypeId = typeof TypeId;\n\nexport const isGroupSpec = (u: unknown): u is AnyWithProps =>\n Predicate.hasProperty(u, TypeId);\n\nexport interface GroupSpec<\n Runtime extends RuntimeAndFunctionType.Runtime,\n Name_ extends string,\n Functions_ extends FunctionSpec.AnyWithPropsWithRuntime<Runtime> = never,\n // Subgroups may be of any runtime, independent of this group's own runtime: a\n // group is only a namespace for its children, which are otherwise-independent\n // modules. Functions, by contrast, stay homogeneous (a Node group only accepts\n // Node actions)—`addFunction` keeps the `<Runtime>` bound below.\n Groups_ extends AnyWithProps = never,\n MiddlewareSpecs_ extends MiddlewareSpec.AnyMiddlewareSpec = never,\n> {\n readonly [TypeId]: TypeId;\n readonly runtime: Runtime;\n readonly name: Name_;\n readonly functions: {\n [\n FunctionName in FunctionSpec.Name<\n FunctionSpec.AnyWithPropsWithRuntime<Runtime>\n >\n ]: FunctionSpec.WithName<Functions_, FunctionName>;\n };\n readonly groups: {\n [GroupName in Name<Groups_>]: WithName<Groups_, GroupName>;\n };\n readonly middlewareSpecs: ReadonlyArray<MiddlewareSpecs_>;\n readonly middlewareAttachments: ReadonlyArray<\n MiddlewareAttachment.MiddlewareAttachment<MiddlewareSpecs_>\n >;\n readonly \"~Functions\": Functions_;\n readonly \"~Groups\": Groups_;\n\n addFunction<Function extends FunctionSpec.AnyWithPropsWithRuntime<Runtime>>(\n function_: Function &\n MiddlewareSpec.ValidateAddedFunction<Function, MiddlewareSpecs_>,\n ): GroupSpec<\n Runtime,\n Name_,\n Functions_ | Function,\n Groups_,\n MiddlewareSpecs_\n >;\n\n middleware<MiddlewareSpec_ extends MiddlewareSpec.AnyMiddlewareSpec>(\n middlewareSpec: MiddlewareSpec_ &\n MiddlewareSpec.ValidateAttach<\n MiddlewareSpec_,\n Functions_,\n MiddlewareSpecs_\n >,\n ...options: MiddlewareAttachment.Args<NoInfer<MiddlewareSpec_>>\n ): GroupSpec<\n Runtime,\n Name_,\n Functions_,\n Groups_,\n MiddlewareSpecs_ | MiddlewareSpec_\n >;\n}\n\nexport interface Any {\n readonly [TypeId]: TypeId;\n}\n\nexport interface AnyWithProps extends GroupSpec<\n RuntimeAndFunctionType.Runtime,\n string,\n FunctionSpec.AnyWithProps,\n AnyWithProps,\n MiddlewareSpec.AnyMiddlewareSpec\n> {}\n\nexport type Name<Group extends AnyWithProps> = Group[\"name\"];\n\nexport type Functions<Group extends AnyWithProps> = Group[\"~Functions\"];\n\nexport type Groups<Group extends AnyWithProps> = Group[\"~Groups\"];\n\nexport type MiddlewareSpecs<Group extends AnyWithProps> =\n Group[\"middlewareSpecs\"][number];\n\nexport type GroupNames<Group extends AnyWithProps> = [Groups<Group>] extends [\n never,\n]\n ? never\n : Name<Groups<Group>>;\n\nexport type WithName<\n Group extends AnyWithProps,\n Name_ extends Name<Group>,\n> = Extract<Group, { readonly name: Name_ }>;\n\n/**\n * Assigns a segment name to a leaf group created with {@link make} for typing\n * and refs.\n */\nexport type NamedAt<Group extends Any, Name_ extends string> = Omit<\n Group,\n \"name\"\n> & {\n readonly name: Name_;\n};\n\nexport type AddGroups<\n Group extends AnyWithProps,\n ExtraGroups extends AnyWithProps,\n> =\n Group extends GroupSpec<\n infer Runtime,\n string,\n infer Functions_,\n infer Groups_,\n infer MiddlewareSpecs_\n >\n ? GroupSpec<\n Runtime,\n Group[\"name\"],\n Functions_,\n Exclude<Groups_, { readonly name: Name<ExtraGroups> }> | ExtraGroups,\n MiddlewareSpecs_\n >\n : never;\n\nconst Proto = {\n [TypeId]: TypeId,\n\n get middlewareSpecs() {\n return this.middlewareAttachments.map(({ spec }) => spec);\n },\n\n addFunction<Function extends FunctionSpec.AnyWithProps>(\n this: Any,\n function_: Function,\n ) {\n const this_ = this as AnyWithProps;\n\n const overlapping = function_.middlewareAttachments.find(\n ({ spec: functionMiddlewareSpec }) =>\n !(\"options\" in functionMiddlewareSpec) &&\n this_.middlewareAttachments.some(\n ({ spec: groupMiddlewareSpec }) =>\n groupMiddlewareSpec.key === functionMiddlewareSpec.key,\n ),\n );\n if (overlapping !== undefined) {\n throw new Error(\n `Middleware \"${overlapping.spec.key}\" is attached to both function \"${function_.name}\" and its group`,\n );\n }\n\n return makeProto({\n runtime: this_.runtime,\n name: this_.name,\n functions: Record.set(this_.functions, function_.name, function_),\n groups: this_.groups,\n middlewareAttachments: this_.middlewareAttachments,\n });\n },\n\n middleware<MiddlewareSpec_ extends MiddlewareSpec.AnyMiddlewareSpec>(\n this: Any,\n middlewareSpec: MiddlewareSpec_,\n ...options: ReadonlyArray<unknown>\n ) {\n const this_ = this as AnyWithProps;\n\n if (\n !(\"options\" in middlewareSpec) &&\n this_.middlewareAttachments.some(\n ({ spec }) => spec.key === middlewareSpec.key,\n )\n ) {\n throw new Error(\n `Middleware \"${middlewareSpec.key}\" is already attached to this group`,\n );\n }\n\n for (const function_ of Object.values(this_.functions)) {\n if (\n !(\"options\" in middlewareSpec) &&\n function_.middlewareAttachments.some(\n ({ spec }) => spec.key === middlewareSpec.key,\n )\n ) {\n throw new Error(\n `Middleware \"${middlewareSpec.key}\" is attached to both function \"${function_.name}\" and its group`,\n );\n }\n }\n\n return makeProto({\n runtime: this_.runtime,\n name: this_.name,\n functions: this_.functions,\n groups: this_.groups,\n middlewareAttachments: [\n ...this_.middlewareAttachments,\n { spec: middlewareSpec, options: options[0] },\n ],\n });\n },\n} satisfies ThisType<AnyWithProps>;\n\nconst makeProto = <\n Runtime extends RuntimeAndFunctionType.Runtime,\n Name_ extends string,\n Functions_ extends FunctionSpec.AnyWithPropsWithRuntime<Runtime>,\n Groups_ extends AnyWithProps,\n MiddlewareAttachments_ extends\n ReadonlyArray<MiddlewareAttachment.MiddlewareAttachment>,\n>({\n runtime,\n name,\n functions,\n groups,\n middlewareAttachments,\n}: {\n runtime: Runtime;\n name: Name_;\n functions: Record.ReadonlyRecord<string, Functions_>;\n groups: Record.ReadonlyRecord<string, Groups_>;\n middlewareAttachments: MiddlewareAttachments_;\n}): GroupSpec<\n Runtime,\n Name_,\n Functions_,\n Groups_,\n MiddlewareAttachments_[number][\"spec\"]\n> =>\n Object.assign(Object.create(Proto), {\n runtime,\n name,\n functions,\n groups,\n middlewareAttachments,\n }) as GroupSpec<\n Runtime,\n Name_,\n Functions_,\n Groups_,\n MiddlewareAttachments_[number][\"spec\"]\n >;\n\nexport const make = (): GroupSpec<\"Convex\", \"\"> =>\n makeProto({\n runtime: \"Convex\",\n name: \"\",\n functions: Record.empty(),\n groups: Record.empty(),\n middlewareAttachments: [],\n });\n\nexport const makeNode = (): GroupSpec<\"Node\", \"\"> =>\n makeProto({\n runtime: \"Node\",\n name: \"\",\n functions: Record.empty(),\n groups: Record.empty(),\n middlewareAttachments: [],\n });\n\nexport function withName<const Name_ extends string, Group extends Any>(\n name: Name_,\n group: Group,\n): NamedAt<Group, Name_>;\nexport function withName(name: string, group: Any): AnyWithProps {\n validateConfectFunctionIdentifier(name);\n const group_ = group as AnyWithProps;\n\n if (group_.name === name) {\n return group_;\n }\n\n return makeProto({\n runtime: group_.runtime,\n name,\n functions: group_.functions,\n groups: group_.groups,\n middlewareAttachments: group_.middlewareAttachments,\n });\n}\n\nexport const addGroupsAt = (\n group: AnyWithProps,\n children: Record.ReadonlyRecord<string, AnyWithProps>,\n): AnyWithProps =>\n makeProto({\n runtime: group.runtime,\n name: group.name,\n functions: group.functions,\n groups: Record.union(\n group.groups,\n Record.map(children, (child, name) => withName(name, child)),\n (_existing, incoming) => incoming,\n ),\n middlewareAttachments: group.middlewareAttachments,\n });\n\nexport const validateMiddleware = (\n group: AnyWithProps,\n): Result.Result<void, MiddlewareAttachment.ValidationError> =>\n Result.gen(function* () {\n yield* MiddlewareAttachment.validateAll(\n group.middlewareAttachments,\n `group \"${group.name}\"`,\n );\n for (const function_ of Object.values(group.functions)) {\n yield* MiddlewareAttachment.validateAll(\n [...group.middlewareAttachments, ...function_.middlewareAttachments],\n `function \"${function_.name}\"`,\n );\n }\n for (const child of Object.values(group.groups))\n yield* validateMiddleware(child);\n });\n"],"mappings":";;;;;;;;;;;;;;;;AASA,MAAa,SAAS;AAGtB,MAAa,eAAe,MAC1B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,OAAO,KAAK,GAAG;CAClC;CACA;CACA;CACA;CACA;AACF,CAAC;AAQH,MAAa,aACX,UAAU;CACR,SAAS;CACT,MAAM;CACN,WAAW,OAAO,MAAM;CACxB,QAAQ,OAAO,MAAM;CACrB,uBAAuB,CAAC;AAC1B,CAAC;AAEH,MAAa,iBACX,UAAU;CACR,SAAS;CACT,MAAM;CACN,WAAW,OAAO,MAAM;CACxB,QAAQ,OAAO,MAAM;CACrB,uBAAuB,CAAC;AAC1B,CAAC;AAMH,SAAgB,SAAS,MAAc,OAA0B;CAC/D,kCAAkC,IAAI;CACtC,MAAM,SAAS;CAEf,IAAI,OAAO,SAAS,MAClB,OAAO;CAGT,OAAO,UAAU;EACf,SAAS,OAAO;EAChB;EACA,WAAW,OAAO;EAClB,QAAQ,OAAO;EACf,uBAAuB,OAAO;CAChC,CAAC;AACH;AAEA,MAAa,eACX,OACA,aAEA,UAAU;CACR,SAAS,MAAM;CACf,MAAM,MAAM;CACZ,WAAW,MAAM;CACjB,QAAQ,OAAO,MACb,MAAM,QACN,OAAO,IAAI,WAAW,OAAO,SAAS,SAAS,MAAM,KAAK,CAAC,IAC1D,WAAW,aAAa,QAC3B;CACA,uBAAuB,MAAM;AAC/B,CAAC;AAEH,MAAa,sBACX,UAEA,OAAO,IAAI,aAAa;CACtB,OAAOA,YACL,MAAM,uBACN,UAAU,MAAM,KAAK,EACvB;CACA,KAAK,MAAM,aAAa,OAAO,OAAO,MAAM,SAAS,GACnD,OAAOA,YACL,CAAC,GAAG,MAAM,uBAAuB,GAAG,UAAU,qBAAqB,GACnE,aAAa,UAAU,KAAK,EAC9B;CAEF,KAAK,MAAM,SAAS,OAAO,OAAO,MAAM,MAAM,GAC5C,OAAO,mBAAmB,KAAK;AACnC,CAAC"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { PropertyValidators, Validator, VAny, VArray, VBoolean, VBytes, VFloat64, VId, VInt64, VLiteral, VNull, VObject,
|
|
1
|
+
import type { PropertyValidators, Validator, VAny, VArray, VBoolean, VBytes, VFloat64, VId, VInt64, VLiteral, VNull, VObject, VRecord, VString, VUnion } from "convex/values";
|
|
2
2
|
import * as Effect from "effect/Effect";
|
|
3
3
|
import * as Schema from "effect/Schema";
|
|
4
4
|
import * as SchemaAST from "effect/SchemaAST";
|
|
@@ -19,21 +19,22 @@ export type ReadonlyRecordValue = {
|
|
|
19
19
|
type MutableValue<T> = T extends ReadonlyArray<infer El> ? MutableValue<El>[] : T extends ReadonlyRecordValue ? {
|
|
20
20
|
-readonly [K in keyof T]: MutableValue<Exclude<T[K], undefined>>;
|
|
21
21
|
} : T;
|
|
22
|
-
export type ValueToValidator<Vl> = [Vl] extends [never] ? never : IsAny<Vl> extends true ? VAny : [Vl] extends [
|
|
22
|
+
export type ValueToValidator<Vl> = [Vl] extends [never] ? never : IsAny<Vl> extends true ? VAny : [Vl] extends [string | number | bigint | boolean | null | ArrayBuffer] ? ConvertValueToValidator<Vl> : IsRecursive<Vl> extends true ? VAny : [Vl] extends [ReadonlyValue] ? ConvertValueToValidator<Vl> : TypeError<"Provided value is not a valid Convex value", Vl>;
|
|
23
|
+
type OptionalValue<Vl, Optionality> = Optionality extends "optional" ? Vl | undefined : Vl;
|
|
24
|
+
type ConvertValueToValidator<Vl, Optionality extends "required" | "optional" = "required"> = [Vl] extends [never] ? never : IsAny<Vl> extends true ? VAny<any, Optionality> : [Vl] extends [null] ? VNull<OptionalValue<null, Optionality>, Optionality> : [Vl] extends [boolean] ? [boolean] extends [Vl] ? VBoolean<OptionalValue<boolean, Optionality>, Optionality> : VLiteral<OptionalValue<Vl, Optionality>, Optionality> : IsUnion<Vl> extends true ? UnionValueToValidator<Vl, Optionality> : [Vl] extends [number] ? [number] extends [Vl] ? VFloat64<OptionalValue<number, Optionality>, Optionality> : VLiteral<OptionalValue<Vl, Optionality>, Optionality> : [Vl] extends [bigint] ? [bigint] extends [Vl] ? VInt64<OptionalValue<bigint, Optionality>, Optionality> : VLiteral<OptionalValue<Vl, Optionality>, Optionality> : [Vl] extends [string] ? StringValueToValidator<Vl, Optionality> : [Vl] extends [ArrayBuffer] ? VBytes<OptionalValue<ArrayBuffer, Optionality>, Optionality> : IsRecursive<Vl> extends true ? VAny<any, Optionality> : Vl extends ReadonlyArray<infer El> ? VArray<OptionalValue<MutableValue<El[]>, Optionality>, ConvertValueToValidator<El>, Optionality> : Vl extends object ? ObjectValueToValidator<Vl, Optionality> : never;
|
|
25
|
+
type StringValueToValidator<Vl, Optionality extends "required" | "optional"> = Vl extends {
|
|
23
26
|
__tableName: infer TableName extends string;
|
|
24
|
-
} ? VId<GenericId.GenericId<TableName
|
|
25
|
-
type
|
|
26
|
-
type
|
|
27
|
-
-readonly [K in keyof Vl]-?: IsAny<Vl[K]> extends true ? IsOptional<Vl, K> extends true ?
|
|
28
|
-
}
|
|
29
|
-
export type UndefinedOrValueToValidator<Vl extends ReadonlyValue | undefined> = undefined extends Vl ?
|
|
30
|
-
type UnionValueToValidator<Vl extends
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
...infer VlRest extends ReadonlyArray<ReadonlyValue>
|
|
36
|
-
] ? ValueToValidator<Vl> extends infer Vd extends Validator<any, any, any> ? ValueTupleToValidatorTuple<VlRest> extends infer VdRest extends Validator<any, "required", any>[] ? [Vd, ...VdRest] : never : never : [];
|
|
27
|
+
} ? VId<OptionalValue<GenericId.GenericId<TableName>, Optionality>, Optionality> : [string] extends [Vl] ? VString<OptionalValue<string, Optionality>, Optionality> : VLiteral<OptionalValue<Vl, Optionality>, Optionality>;
|
|
28
|
+
type ObjectValueToValidator<Vl, Optionality extends "required" | "optional"> = IsRecord<MutableValue<Vl>> extends true ? ValueToValidator<Vl[keyof Vl]> extends infer VdValue extends Validator<any, "required", any> ? VRecord<OptionalValue<MutableValue<Vl>, Optionality>, VString, VdValue, Optionality> : never : ObjectFieldsToValidators<Vl> extends infer VdFields extends Record<string, any> ? VObject<OptionalValue<MutableValue<Vl>, Optionality>, VdFields, Optionality> : never;
|
|
29
|
+
type ObjectFieldsToValidators<Vl> = {
|
|
30
|
+
-readonly [K in keyof Vl]-?: IsAny<Vl[K]> extends true ? IsOptional<Vl, K> extends true ? VAny<any, "optional"> : VAny : undefined extends Vl[K] ? OptionalValueToValidator<Vl[K]> : [Vl[K]] extends [ReadonlyValue] ? ConvertValueToValidator<Vl[K]> : never;
|
|
31
|
+
};
|
|
32
|
+
export type UndefinedOrValueToValidator<Vl extends ReadonlyValue | undefined> = undefined extends Vl ? OptionalValueToValidator<Vl> : [Vl] extends [ReadonlyValue] ? ValueToValidator<Vl> : never;
|
|
33
|
+
type UnionValueToValidator<Vl, Optionality extends "required" | "optional"> = IsRecursive<Vl> extends true ? VAny<any, Optionality> : VUnion<OptionalValue<MutableValue<Vl>, Optionality>, ValueTupleToValidatorTuple<UnionToTuple<Vl>>, Optionality>;
|
|
34
|
+
type ValueTupleToValidatorTuple<VlTuple extends readonly unknown[]> = VlTuple extends [true, false, ...infer VlRest] | [false, true, ...infer VlRest] ? [VBoolean, ...ValueTupleToValidatorTuple<VlRest>] : VlTuple extends [infer Vl, ...infer VlRest] ? [ConvertValueToValidator<Vl>, ...ValueTupleToValidatorTuple<VlRest>] : [];
|
|
35
|
+
type OptionalValueToValidator<Vl> = [Vl] extends [
|
|
36
|
+
(infer Val extends ReadonlyValue) | undefined
|
|
37
|
+
] ? ConvertValueToValidator<Val, "optional"> : never;
|
|
37
38
|
export declare const compileSchema: <T, E>(schema: Schema.Codec<T, E>) => ValueToValidator<(typeof schema)["Encoded"]>;
|
|
38
39
|
export declare const isRecursive: (ast: SchemaAST.AST) => boolean;
|
|
39
40
|
export declare const compileAst: (ast: SchemaAST.AST, isOptionalPropertyOfTypeLiteral?: boolean) => Effect.Effect<Validator<any, any, any>, UnsupportedSchemaTypeError | UnsupportedPropertySignatureKeyTypeError | IndexSignaturesAreNotSupportedError | MixedIndexAndPropertySignaturesAreNotSupportedError | OptionalTupleElementsAreNotSupportedError | EmptyTupleIsNotSupportedError | EmptyUnionIsNotSupportedError>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"SchemaToValidator.d.ts","sourceRoot":"","sources":["../src/SchemaToValidator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kBAAkB,EAClB,SAAS,EACT,IAAI,EACJ,MAAM,EACN,QAAQ,EACR,MAAM,EACN,QAAQ,EACR,GAAG,EACH,MAAM,EACN,QAAQ,EACR,KAAK,EACL,OAAO,EACP,
|
|
1
|
+
{"version":3,"file":"SchemaToValidator.d.ts","sourceRoot":"","sources":["../src/SchemaToValidator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kBAAkB,EAClB,SAAS,EACT,IAAI,EACJ,MAAM,EACN,QAAQ,EACR,MAAM,EACN,QAAQ,EACR,GAAG,EACH,MAAM,EACN,QAAQ,EACR,KAAK,EACL,OAAO,EACP,OAAO,EACP,OAAO,EACP,MAAM,EACP,MAAM,eAAe,CAAC;AAKvB,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AAKxC,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,SAAS,MAAM,kBAAkB,CAAC;AAG9C,OAAO,KAAK,SAAS,MAAM,aAAa,CAAC;AACzC,OAAO,KAAK,EACV,KAAK,EACL,UAAU,EACV,QAAQ,EACR,WAAW,EACX,OAAO,EACP,SAAS,EACT,YAAY,EACb,MAAM,SAAS,CAAC;AAIjB,eAAO,MAAM,iBAAiB,GAAI,YAAY,EAAE,WAAW,cAC7C,MAAM,CAAC,KAAK,CAAC,YAAY,EAAE,WAAW,CAAC,KAClD,kBAcF,CAAC;AAIF,eAAO,MAAM,oBAAoB,GAAI,YAAY,EAAE,WAAW,UACpD,MAAM,CAAC,KAAK,CAAC,YAAY,EAAE,WAAW,CAAC,KAC9C,SAAS,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,CACgC,CAAC;AAI3D;;GAEG;AACH,MAAM,MAAM,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sourcesContent":["import type {\n PropertyValidators,\n Validator,\n VAny,\n VArray,\n VBoolean,\n VBytes,\n VFloat64,\n VId,\n VInt64,\n VLiteral,\n VNull,\n VObject,\n VOptional,\n VRecord,\n VString,\n VUnion,\n} from \"convex/values\";\nimport { v } from \"convex/values\";\nimport { pipe } from \"effect/Function\";\nimport * as Array from \"effect/Array\";\nimport * as Data from \"effect/Data\";\nimport * as Effect from \"effect/Effect\";\nimport * as Match from \"effect/Match\";\nimport * as Number from \"effect/Number\";\nimport * as Option from \"effect/Option\";\nimport * as Predicate from \"effect/Predicate\";\nimport * as Schema from \"effect/Schema\";\nimport * as SchemaAST from \"effect/SchemaAST\";\nimport * as String from \"effect/String\";\n\nimport * as GenericId from \"./GenericId\";\nimport type {\n IsAny,\n IsOptional,\n IsRecord,\n IsRecursive,\n IsUnion,\n TypeError,\n UnionToTuple,\n} from \"./Types\";\n\n// Args\n\nexport const compileArgsSchema = <ConfectValue, ConvexValue>(\n argsSchema: Schema.Codec<ConfectValue, ConvexValue>,\n): PropertyValidators => {\n const ast = Schema.toEncoded(argsSchema).ast;\n\n return pipe(\n ast,\n Match.value,\n Match.tag(\"Objects\", (objectsAst) =>\n Array.isReadonlyArrayEmpty(objectsAst.indexSignatures)\n ? handlePropertySignatures(objectsAst)\n : Effect.fail(new IndexSignaturesAreNotSupportedError()),\n ),\n Match.orElse(() => Effect.fail(new TopLevelMustBeObjectError())),\n Effect.runSync,\n );\n};\n\n// Returns\n\nexport const compileReturnsSchema = <ConfectValue, ConvexValue>(\n schema: Schema.Codec<ConfectValue, ConvexValue>,\n): Validator<any, any, any> =>\n Effect.runSync(compileAst(Schema.toEncoded(schema).ast));\n\n// Table\n\n/**\n * Convert a table `Schema` to a table `Validator`.\n */\nexport type TableSchemaToTableValidator<\n TableSchema extends Schema.Codec<any, any>,\n> =\n ValueToValidator<TableSchema[\"Encoded\"]> extends infer Vd extends\n | VObject<any, any, any, any>\n | VUnion<any, any, any, any>\n ? Vd\n : never;\n\nexport const compileTableSchema = <TableSchema extends Schema.Codec<any, any>>(\n schema: TableSchema,\n): TableSchemaToTableValidator<TableSchema> => {\n const ast = Schema.toEncoded(schema).ast;\n\n return pipe(\n ast,\n Match.value,\n Match.tag(\"Objects\", ({ indexSignatures }) =>\n Array.isReadonlyArrayEmpty(indexSignatures)\n ? // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- The return type is derived from the input schema and cannot be recovered from the runtime AST.\n (compileAst(ast) as Effect.Effect<any>)\n : Effect.fail(new IndexSignaturesAreNotSupportedError()),\n ),\n Match.tag(\"Union\", (unionAst) => compileAst(unionAst)),\n Match.orElse(() => Effect.fail(new TopLevelMustBeObjectOrUnionError())),\n Effect.runSync,\n );\n};\n\n// Compiler\n\nexport type ReadonlyValue =\n | string\n | number\n | bigint\n | boolean\n | ArrayBuffer\n | ReadonlyArrayValue\n | ReadonlyRecordValue\n | null;\n\ntype ReadonlyArrayValue = readonly ReadonlyValue[];\n\nexport type ReadonlyRecordValue = {\n readonly [key: string]: ReadonlyValue | undefined;\n};\n\ntype MutableValue<T> =\n T extends ReadonlyArray<infer El>\n ? MutableValue<El>[]\n : T extends ReadonlyRecordValue\n ? { -readonly [K in keyof T]: MutableValue<Exclude<T[K], undefined>> }\n : T;\n\nexport type ValueToValidator<Vl> = [Vl] extends [never]\n ? never\n : IsAny<Vl> extends true\n ? VAny\n : [Vl] extends [null]\n ? VNull\n : [Vl] extends [boolean]\n ? [boolean] extends [Vl]\n ? VBoolean\n : VLiteral<Vl>\n : IsUnion<Vl> extends true\n ? IsRecursive<Vl> extends true\n ? VAny\n : [Vl] extends [ReadonlyValue]\n ? UnionValueToValidator<Vl>\n : TypeError<\"Provided value is not a valid Convex value\", Vl>\n : [Vl] extends [number]\n ? [number] extends [Vl]\n ? VFloat64\n : VLiteral<Vl>\n : [Vl] extends [bigint]\n ? [bigint] extends [Vl]\n ? VInt64\n : VLiteral<Vl>\n : [Vl] extends [string]\n ? Vl extends {\n __tableName: infer TableName extends string;\n }\n ? VId<GenericId.GenericId<TableName>>\n : [string] extends [Vl]\n ? VString\n : VLiteral<Vl>\n : [Vl] extends [ArrayBuffer]\n ? VBytes\n : IsRecursive<Vl> extends true\n ? VAny\n : [Vl] extends [ReadonlyValue]\n ? Vl extends ReadonlyArray<ReadonlyValue>\n ? ArrayValueToValidator<Vl>\n : Vl extends ReadonlyRecordValue\n ? RecordValueToValidator<Vl>\n : TypeError<\"Unexpected value\", Vl>\n : TypeError<\n \"Provided value is not a valid Convex value\",\n Vl\n >;\n\ntype ArrayValueToValidator<Vl extends ReadonlyArray<ReadonlyValue>> =\n Vl extends ReadonlyArray<infer El extends ReadonlyValue>\n ? ValueToValidator<El> extends infer Vd extends Validator<any, any, any>\n ? VArray<MutableValue<El[]>, Vd>\n : never\n : never;\n\ntype RecordValueToValidator<Vl> = Vl extends ReadonlyRecordValue\n ? {\n -readonly [K in keyof Vl]-?: IsAny<Vl[K]> extends true\n ? IsOptional<Vl, K> extends true\n ? VOptional<VAny>\n : VAny\n : UndefinedOrValueToValidator<Vl[K]>;\n } extends infer VdRecord extends Record<string, any>\n ? MutableValue<Vl> extends infer VlRecord extends Record<string, any>\n ? IsRecord<VlRecord> extends true\n ? VRecord<VlRecord, VString, VdRecord[keyof VdRecord]>\n : VObject<VlRecord, VdRecord>\n : never\n : never\n : never;\n\nexport type UndefinedOrValueToValidator<Vl extends ReadonlyValue | undefined> =\n undefined extends Vl\n ? [Vl] extends [(infer Val extends ReadonlyValue) | undefined]\n ? ValueToValidator<Val> extends infer Vd extends Validator<\n any,\n \"required\",\n any\n >\n ? VOptional<Vd>\n : never\n : never\n : [Vl] extends [ReadonlyValue]\n ? ValueToValidator<Vl>\n : never;\n\ntype UnionValueToValidator<Vl extends ReadonlyValue> = [Vl] extends [\n ReadonlyValue,\n]\n ? IsUnion<Vl> extends true\n ? UnionToTuple<Vl> extends infer VlTuple extends\n ReadonlyArray<ReadonlyValue>\n ? ValueTupleToValidatorTuple<VlTuple> extends infer VdTuple extends\n Validator<any, \"required\", any>[]\n ? VUnion<MutableValue<Vl>, VdTuple>\n : TypeError<\"Failed to convert value tuple to validator tuple\">\n : TypeError<\"Failed to convert union to tuple\">\n : TypeError<\"Expected a union of values, but got a single value instead\">\n : TypeError<\"Provided value is not a valid Convex value\">;\n\ntype ValueTupleToValidatorTuple<VlTuple extends ReadonlyArray<ReadonlyValue>> =\n VlTuple extends\n | [true, false, ...infer VlRest extends ReadonlyArray<ReadonlyValue>]\n | [false, true, ...infer VlRest extends ReadonlyArray<ReadonlyValue>]\n ? ValueTupleToValidatorTuple<VlRest> extends infer VdRest extends Validator<\n any,\n any,\n any\n >[]\n ? [VBoolean<boolean>, ...VdRest]\n : never\n : VlTuple extends [\n infer Vl extends ReadonlyValue,\n ...infer VlRest extends ReadonlyArray<ReadonlyValue>,\n ]\n ? ValueToValidator<Vl> extends infer Vd extends Validator<any, any, any>\n ? ValueTupleToValidatorTuple<VlRest> extends infer VdRest extends\n Validator<any, \"required\", any>[]\n ? [Vd, ...VdRest]\n : never\n : never\n : [];\n\nexport const compileSchema = <T, E>(\n schema: Schema.Codec<T, E>,\n): ValueToValidator<(typeof schema)[\"Encoded\"]> =>\n Effect.runSync(compileAst(schema.ast)) as any;\n\nexport const isRecursive = (ast: SchemaAST.AST): boolean =>\n pipe(\n ast,\n Match.value,\n Match.tag(\n \"Literal\",\n \"Null\",\n \"Boolean\",\n \"String\",\n \"Number\",\n \"BigInt\",\n \"Unknown\",\n \"Any\",\n \"Declaration\",\n \"UniqueSymbol\",\n \"Symbol\",\n \"Undefined\",\n \"Void\",\n \"Never\",\n \"Enum\",\n \"TemplateLiteral\",\n \"ObjectKeyword\",\n () => false,\n ),\n Match.tag(\"Union\", ({ types }) =>\n Array.some(types, (type) => isRecursive(type)),\n ),\n Match.tag(\"Objects\", ({ propertySignatures }) =>\n Array.some(propertySignatures, ({ type }) => isRecursive(type)),\n ),\n Match.tag(\n \"Arrays\",\n ({ elements, rest }) =>\n Array.some(elements, (element) => isRecursive(element)) ||\n Array.some(rest, (element) => isRecursive(element)),\n ),\n Match.tag(\"Suspend\", () => true),\n Match.exhaustive,\n );\n\nexport const compileAst = (\n ast: SchemaAST.AST,\n isOptionalPropertyOfTypeLiteral = false,\n): Effect.Effect<\n Validator<any, any, any>,\n | UnsupportedSchemaTypeError\n | UnsupportedPropertySignatureKeyTypeError\n | IndexSignaturesAreNotSupportedError\n | MixedIndexAndPropertySignaturesAreNotSupportedError\n | OptionalTupleElementsAreNotSupportedError\n | EmptyTupleIsNotSupportedError\n | EmptyUnionIsNotSupportedError\n> =>\n isRecursive(ast)\n ? Effect.succeed(v.any())\n : pipe(\n ast,\n Match.value,\n Match.tag(\"Literal\", ({ literal }) =>\n pipe(\n literal,\n Match.value,\n Match.whenOr(\n Match.string,\n Match.number,\n Match.bigint,\n Match.boolean,\n (l) => v.literal(l),\n ),\n Match.exhaustive,\n Effect.succeed,\n ),\n ),\n Match.tag(\"Null\", () => Effect.succeed(v.null())),\n Match.tag(\"Boolean\", () => Effect.succeed(v.boolean())),\n Match.tag(\"String\", (stringAst) =>\n GenericId.tableName(stringAst).pipe(\n Option.match({\n onNone: () => Effect.succeed(v.string()),\n onSome: (tableName) => Effect.succeed(v.id(tableName)),\n }),\n ),\n ),\n Match.tag(\"Number\", () => Effect.succeed(v.float64())),\n Match.tag(\"BigInt\", () => Effect.succeed(v.int64())),\n Match.tag(\"Union\", (unionAst) =>\n handleUnion(unionAst, isOptionalPropertyOfTypeLiteral),\n ),\n Match.tag(\"Objects\", (objectsAst) => handleObjects(objectsAst)),\n Match.tag(\"Arrays\", (arraysAst) => handleArrays(arraysAst)),\n Match.tag(\"Unknown\", \"Any\", () => Effect.succeed(v.any())),\n Match.tag(\"Declaration\", (declaration) =>\n Effect.mapBoth(\n // oxlint-disable-next-line effecttsgo/any-unknown-in-error-context -- Codec inputs are context-free, but Effect's SchemaAST.DeclarationRun erases their requirements to `any`.\n declaration.run(declaration.typeParameters)(\n new ArrayBuffer(0),\n declaration,\n {},\n ) as Effect.Effect<ArrayBuffer, unknown, never>,\n {\n onSuccess: () => v.bytes(),\n onFailure: () =>\n new UnsupportedSchemaTypeError({\n schemaType: declaration._tag,\n }),\n },\n ),\n ),\n Match.tag(\"Suspend\", () => Effect.succeed(v.any())),\n Match.tag(\n \"UniqueSymbol\",\n \"Symbol\",\n \"Undefined\",\n \"Void\",\n \"Never\",\n \"Enum\",\n \"TemplateLiteral\",\n \"ObjectKeyword\",\n () =>\n Effect.fail(\n new UnsupportedSchemaTypeError({\n schemaType: ast._tag,\n }),\n ),\n ),\n Match.exhaustive,\n );\n\nconst handleUnion = (\n { types }: SchemaAST.Union,\n isOptionalPropertyOfTypeLiteral: boolean,\n) => handleUnionTypes(types, isOptionalPropertyOfTypeLiteral);\n\nconst handleUnionTypes = Effect.fnUntraced(function* (\n types: SchemaAST.Union[\"types\"],\n isOptionalPropertyOfTypeLiteral: boolean,\n) {\n const members = isOptionalPropertyOfTypeLiteral\n ? Array.filter(types, Predicate.not(SchemaAST.isUndefined))\n : types;\n\n const [firstValidator, secondValidator, ...restValidators] =\n yield* Effect.all(Array.map(members, (type) => compileAst(type)));\n\n if (firstValidator === undefined) {\n return yield* new EmptyUnionIsNotSupportedError();\n } else if (secondValidator === undefined) {\n return firstValidator;\n } else {\n return v.union(firstValidator, secondValidator, ...restValidators);\n }\n});\n\nconst handleObjects = (objectsAst: SchemaAST.Objects) =>\n pipe(\n objectsAst.indexSignatures,\n Array.head,\n Option.match({\n onNone: () => Effect.map(handlePropertySignatures(objectsAst), v.object),\n onSome: ({ parameter, type }) =>\n pipe(\n objectsAst.propertySignatures,\n Array.head,\n Option.match({\n onNone: () =>\n Effect.map(\n Effect.all({\n parameter_: compileAst(parameter),\n type_: compileAst(type),\n }),\n ({ parameter_, type_ }) => v.record(parameter_, type_),\n ),\n onSome: () =>\n Effect.fail(\n new MixedIndexAndPropertySignaturesAreNotSupportedError(),\n ),\n }),\n ),\n }),\n );\n\nconst handleArrays = ({ elements, rest }: SchemaAST.Arrays) =>\n handleArrayElements(elements, rest);\n\nconst handleArrayElements = Effect.fnUntraced(function* (\n elements: SchemaAST.Arrays[\"elements\"],\n rest: SchemaAST.Arrays[\"rest\"],\n) {\n const [f, s, ...r] = elements;\n\n const elementToValidator = (element: SchemaAST.AST) =>\n SchemaAST.isOptional(element)\n ? Effect.fail(new OptionalTupleElementsAreNotSupportedError())\n : compileAst(element);\n\n const arrayItemsValidator = yield* f === undefined\n ? pipe(\n rest,\n Array.head,\n Option.match({\n onNone: () => Effect.fail(new EmptyTupleIsNotSupportedError()),\n onSome: (type) => compileAst(type),\n }),\n )\n : s === undefined\n ? elementToValidator(f)\n : Effect.gen(function* () {\n const firstValidator = yield* elementToValidator(f);\n const secondValidator = yield* elementToValidator(s);\n const restValidators = yield* Effect.forEach(r, elementToValidator);\n\n return v.union(firstValidator, secondValidator, ...restValidators);\n });\n\n return v.array(arrayItemsValidator);\n});\n\nconst handlePropertySignatures = (objectsAst: SchemaAST.Objects) =>\n pipe(\n objectsAst.propertySignatures,\n Effect.forEach(({ type, name }) => {\n const isOptional = SchemaAST.isOptional(type);\n if (String.isString(name)) {\n // Somehow, somewhere, keys of type number are being coerced to strings…\n return Option.match(Number.parse(name), {\n onNone: () =>\n Effect.gen(function* () {\n const validator = yield* compileAst(type, isOptional);\n\n return {\n propertyName: name,\n validator: isOptional ? v.optional(validator) : validator,\n };\n }),\n onSome: (number) =>\n Effect.fail(\n new UnsupportedPropertySignatureKeyTypeError({\n propertyKey: number,\n }),\n ),\n });\n } else {\n return Effect.fail(\n new UnsupportedPropertySignatureKeyTypeError({ propertyKey: name }),\n );\n }\n }),\n Effect.andThen((propertyNamesWithValidators) =>\n pipe(\n propertyNamesWithValidators,\n Array.reduce(\n {} as Record<string, Validator<any, any, any>>,\n (acc, { propertyName, validator }) => ({\n [propertyName]: validator,\n ...acc,\n }),\n ),\n Effect.succeed,\n ),\n ),\n );\n\n// Errors\n\nexport class TopLevelMustBeObjectError extends Data.TaggedError(\n \"TopLevelMustBeObjectError\",\n) {\n /* v8 ignore start */\n override get message() {\n return \"Top level schema must be an object\";\n }\n /* v8 ignore stop */\n}\n\nexport class TopLevelMustBeObjectOrUnionError extends Data.TaggedError(\n \"TopLevelMustBeObjectOrUnionError\",\n) {\n /* v8 ignore start */\n override get message() {\n return \"Top level schema must be an object or a union\";\n }\n /* v8 ignore stop */\n}\n\nexport class UnsupportedPropertySignatureKeyTypeError extends Data.TaggedError(\n \"UnsupportedPropertySignatureKeyTypeError\",\n)<{\n readonly propertyKey: number | symbol;\n}> {\n /* v8 ignore start */\n override get message() {\n return `Unsupported property signature '${this.propertyKey.toString()}'. Property is of type '${typeof this.propertyKey}' but only 'string' properties are supported.`;\n }\n /* v8 ignore stop */\n}\n\nexport class EmptyTupleIsNotSupportedError extends Data.TaggedError(\n \"EmptyTupleIsNotSupportedError\",\n) {\n /* v8 ignore start */\n override get message() {\n return \"Tuple must have at least one element\";\n }\n /* v8 ignore stop */\n}\n\nexport class EmptyUnionIsNotSupportedError extends Data.TaggedError(\n \"EmptyUnionIsNotSupportedError\",\n) {\n /* v8 ignore start */\n override get message() {\n return \"Union must have at least one member that compiles to a validator\";\n }\n /* v8 ignore stop */\n}\n\nexport class UnsupportedSchemaTypeError extends Data.TaggedError(\n \"UnsupportedSchemaTypeError\",\n)<{\n readonly schemaType: SchemaAST.AST[\"_tag\"];\n}> {\n /* v8 ignore start */\n override get message() {\n return `Unsupported schema type '${this.schemaType}'`;\n }\n /* v8 ignore stop */\n}\n\nexport class IndexSignaturesAreNotSupportedError extends Data.TaggedError(\n \"IndexSignaturesAreNotSupportedError\",\n) {\n /* v8 ignore start */\n override get message() {\n return \"Index signatures are not supported\";\n }\n /* v8 ignore stop */\n}\n\nexport class MixedIndexAndPropertySignaturesAreNotSupportedError extends Data.TaggedError(\n \"MixedIndexAndPropertySignaturesAreNotSupportedError\",\n) {\n /* v8 ignore start */\n override get message() {\n return \"Mixed index and property signatures are not supported\";\n }\n /* v8 ignore stop */\n}\n\nexport class OptionalTupleElementsAreNotSupportedError extends Data.TaggedError(\n \"OptionalTupleElementsAreNotSupportedError\",\n) {\n /* v8 ignore start */\n override get message() {\n return \"Optional tuple elements are not supported\";\n }\n /* v8 ignore stop */\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4CA,MAAa,qBACX,eACuB;CACvB,MAAM,MAAM,OAAO,UAAU,UAAU,CAAC,CAAC;CAEzC,OAAO,KACL,KACA,MAAM,OACN,MAAM,IAAI,YAAY,eACpB,MAAM,qBAAqB,WAAW,eAAe,IACjD,yBAAyB,UAAU,IACnC,OAAO,KAAK,IAAI,oCAAoC,CAAC,CAC3D,GACA,MAAM,aAAa,OAAO,KAAK,IAAI,0BAA0B,CAAC,CAAC,GAC/D,OAAO,OACT;AACF;AAIA,MAAa,wBACX,WAEA,OAAO,QAAQ,WAAW,OAAO,UAAU,MAAM,CAAC,CAAC,GAAG,CAAC;AAgBzD,MAAa,sBACX,WAC6C;CAC7C,MAAM,MAAM,OAAO,UAAU,MAAM,CAAC,CAAC;CAErC,OAAO,KACL,KACA,MAAM,OACN,MAAM,IAAI,YAAY,EAAE,sBACtB,MAAM,qBAAqB,eAAe,IAErC,WAAW,GAAG,IACf,OAAO,KAAK,IAAI,oCAAoC,CAAC,CAC3D,GACA,MAAM,IAAI,UAAU,aAAa,WAAW,QAAQ,CAAC,GACrD,MAAM,aAAa,OAAO,KAAK,IAAI,iCAAiC,CAAC,CAAC,GACtE,OAAO,OACT;AACF;AAqJA,MAAa,iBACX,WAEA,OAAO,QAAQ,WAAW,OAAO,GAAG,CAAC;AAEvC,MAAa,eAAe,QAC1B,KACE,KACA,MAAM,OACN,MAAM,IACJ,WACA,QACA,WACA,UACA,UACA,UACA,WACA,OACA,eACA,gBACA,UACA,aACA,QACA,SACA,QACA,mBACA,uBACM,KACR,GACA,MAAM,IAAI,UAAU,EAAE,YACpB,MAAM,KAAK,QAAQ,SAAS,YAAY,IA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emaToValidator.js","names":["GenericId.tableName"],"sources":["../src/SchemaToValidator.ts"],"sourcesContent":["import type {\n PropertyValidators,\n Validator,\n VAny,\n VArray,\n VBoolean,\n VBytes,\n VFloat64,\n VId,\n VInt64,\n VLiteral,\n VNull,\n VObject,\n VRecord,\n VString,\n VUnion,\n} from \"convex/values\";\nimport { v } from \"convex/values\";\nimport { pipe } from \"effect/Function\";\nimport * as Array from \"effect/Array\";\nimport * as Data from \"effect/Data\";\nimport * as Effect from \"effect/Effect\";\nimport * as Match from \"effect/Match\";\nimport * as Number from \"effect/Number\";\nimport * as Option from \"effect/Option\";\nimport * as Predicate from \"effect/Predicate\";\nimport * as Schema from \"effect/Schema\";\nimport * as SchemaAST from \"effect/SchemaAST\";\nimport * as String from \"effect/String\";\n\nimport * as GenericId from \"./GenericId\";\nimport type {\n IsAny,\n IsOptional,\n IsRecord,\n IsRecursive,\n IsUnion,\n TypeError,\n UnionToTuple,\n} from \"./Types\";\n\n// Args\n\nexport const compileArgsSchema = <ConfectValue, ConvexValue>(\n argsSchema: Schema.Codec<ConfectValue, ConvexValue>,\n): PropertyValidators => {\n const ast = Schema.toEncoded(argsSchema).ast;\n\n return pipe(\n ast,\n Match.value,\n Match.tag(\"Objects\", (objectsAst) =>\n Array.isReadonlyArrayEmpty(objectsAst.indexSignatures)\n ? handlePropertySignatures(objectsAst)\n : Effect.fail(new IndexSignaturesAreNotSupportedError()),\n ),\n Match.orElse(() => Effect.fail(new TopLevelMustBeObjectError())),\n Effect.runSync,\n );\n};\n\n// Returns\n\nexport const compileReturnsSchema = <ConfectValue, ConvexValue>(\n schema: Schema.Codec<ConfectValue, ConvexValue>,\n): Validator<any, any, any> =>\n Effect.runSync(compileAst(Schema.toEncoded(schema).ast));\n\n// Table\n\n/**\n * Convert a table `Schema` to a table `Validator`.\n */\nexport type TableSchemaToTableValidator<\n TableSchema extends Schema.Codec<any, any>,\n> =\n ValueToValidator<TableSchema[\"Encoded\"]> extends infer Vd extends\n | VObject<any, any, any, any>\n | VUnion<any, any, any, any>\n ? Vd\n : never;\n\nexport const compileTableSchema = <TableSchema extends Schema.Codec<any, any>>(\n schema: TableSchema,\n): TableSchemaToTableValidator<TableSchema> => {\n const ast = Schema.toEncoded(schema).ast;\n\n return pipe(\n ast,\n Match.value,\n Match.tag(\"Objects\", ({ indexSignatures }) =>\n Array.isReadonlyArrayEmpty(indexSignatures)\n ? // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- The return type is derived from the input schema and cannot be recovered from the runtime AST.\n (compileAst(ast) as Effect.Effect<any>)\n : Effect.fail(new IndexSignaturesAreNotSupportedError()),\n ),\n Match.tag(\"Union\", (unionAst) => compileAst(unionAst)),\n Match.orElse(() => Effect.fail(new TopLevelMustBeObjectOrUnionError())),\n Effect.runSync,\n );\n};\n\n// Compiler\n\nexport type ReadonlyValue =\n | string\n | number\n | bigint\n | boolean\n | ArrayBuffer\n | ReadonlyArrayValue\n | ReadonlyRecordValue\n | null;\n\ntype ReadonlyArrayValue = readonly ReadonlyValue[];\n\nexport type ReadonlyRecordValue = {\n readonly [key: string]: ReadonlyValue | undefined;\n};\n\ntype MutableValue<T> =\n T extends ReadonlyArray<infer El>\n ? MutableValue<El>[]\n : T extends ReadonlyRecordValue\n ? { -readonly [K in keyof T]: MutableValue<Exclude<T[K], undefined>> }\n : T;\n\nexport type ValueToValidator<Vl> = [Vl] extends [never]\n ? never\n : IsAny<Vl> extends true\n ? VAny\n : [Vl] extends [string | number | bigint | boolean | null | ArrayBuffer]\n ? ConvertValueToValidator<Vl>\n : IsRecursive<Vl> extends true\n ? VAny\n : [Vl] extends [ReadonlyValue]\n ? ConvertValueToValidator<Vl>\n : TypeError<\"Provided value is not a valid Convex value\", Vl>;\n\ntype OptionalValue<Vl, Optionality> = Optionality extends \"optional\"\n ? Vl | undefined\n : Vl;\n\ntype ConvertValueToValidator<\n Vl,\n Optionality extends \"required\" | \"optional\" = \"required\",\n> = [Vl] extends [never]\n ? never\n : IsAny<Vl> extends true\n ? VAny<any, Optionality>\n : [Vl] extends [null]\n ? VNull<OptionalValue<null, Optionality>, Optionality>\n : [Vl] extends [boolean]\n ? [boolean] extends [Vl]\n ? VBoolean<OptionalValue<boolean, Optionality>, Optionality>\n : VLiteral<OptionalValue<Vl, Optionality>, Optionality>\n : IsUnion<Vl> extends true\n ? UnionValueToValidator<Vl, Optionality>\n : [Vl] extends [number]\n ? [number] extends [Vl]\n ? VFloat64<OptionalValue<number, Optionality>, Optionality>\n : VLiteral<OptionalValue<Vl, Optionality>, Optionality>\n : [Vl] extends [bigint]\n ? [bigint] extends [Vl]\n ? VInt64<OptionalValue<bigint, Optionality>, Optionality>\n : VLiteral<OptionalValue<Vl, Optionality>, Optionality>\n : [Vl] extends [string]\n ? StringValueToValidator<Vl, Optionality>\n : [Vl] extends [ArrayBuffer]\n ? VBytes<OptionalValue<ArrayBuffer, Optionality>, Optionality>\n : IsRecursive<Vl> extends true\n ? VAny<any, Optionality>\n : Vl extends ReadonlyArray<infer El>\n ? VArray<\n OptionalValue<MutableValue<El[]>, Optionality>,\n ConvertValueToValidator<El>,\n Optionality\n >\n : Vl extends object\n ? ObjectValueToValidator<Vl, Optionality>\n : never;\n\ntype StringValueToValidator<\n Vl,\n Optionality extends \"required\" | \"optional\",\n> = Vl extends { __tableName: infer TableName extends string }\n ? VId<OptionalValue<GenericId.GenericId<TableName>, Optionality>, Optionality>\n : [string] extends [Vl]\n ? VString<OptionalValue<string, Optionality>, Optionality>\n : VLiteral<OptionalValue<Vl, Optionality>, Optionality>;\n\ntype ObjectValueToValidator<Vl, Optionality extends \"required\" | \"optional\"> =\n IsRecord<MutableValue<Vl>> extends true\n ? ValueToValidator<Vl[keyof Vl]> extends infer VdValue extends Validator<\n any,\n \"required\",\n any\n >\n ? VRecord<\n OptionalValue<MutableValue<Vl>, Optionality>,\n VString,\n VdValue,\n Optionality\n >\n : never\n : ObjectFieldsToValidators<Vl> extends infer VdFields extends Record<\n string,\n any\n >\n ? VObject<\n OptionalValue<MutableValue<Vl>, Optionality>,\n VdFields,\n Optionality\n >\n : never;\n\ntype ObjectFieldsToValidators<Vl> = {\n -readonly [K in keyof Vl]-?: IsAny<Vl[K]> extends true\n ? IsOptional<Vl, K> extends true\n ? VAny<any, \"optional\">\n : VAny\n : undefined extends Vl[K]\n ? OptionalValueToValidator<Vl[K]>\n : [Vl[K]] extends [ReadonlyValue]\n ? ConvertValueToValidator<Vl[K]>\n : never;\n};\n\nexport type UndefinedOrValueToValidator<Vl extends ReadonlyValue | undefined> =\n undefined extends Vl\n ? OptionalValueToValidator<Vl>\n : [Vl] extends [ReadonlyValue]\n ? ValueToValidator<Vl>\n : never;\n\ntype UnionValueToValidator<Vl, Optionality extends \"required\" | \"optional\"> =\n IsRecursive<Vl> extends true\n ? VAny<any, Optionality>\n : VUnion<\n OptionalValue<MutableValue<Vl>, Optionality>,\n ValueTupleToValidatorTuple<UnionToTuple<Vl>>,\n Optionality\n >;\n\ntype ValueTupleToValidatorTuple<VlTuple extends readonly unknown[]> =\n VlTuple extends\n | [true, false, ...infer VlRest]\n | [false, true, ...infer VlRest]\n ? [VBoolean, ...ValueTupleToValidatorTuple<VlRest>]\n : VlTuple extends [infer Vl, ...infer VlRest]\n ? [ConvertValueToValidator<Vl>, ...ValueTupleToValidatorTuple<VlRest>]\n : [];\n\ntype OptionalValueToValidator<Vl> = [Vl] extends [\n (infer Val extends ReadonlyValue) | undefined,\n]\n ? ConvertValueToValidator<Val, \"optional\">\n : never;\n\nexport const compileSchema = <T, E>(\n schema: Schema.Codec<T, E>,\n): ValueToValidator<(typeof schema)[\"Encoded\"]> =>\n Effect.runSync(compileAst(schema.ast)) as any;\n\nexport const isRecursive = (ast: SchemaAST.AST): boolean =>\n pipe(\n ast,\n Match.value,\n Match.tag(\n \"Literal\",\n \"Null\",\n \"Boolean\",\n \"String\",\n \"Number\",\n \"BigInt\",\n \"Unknown\",\n \"Any\",\n \"Declaration\",\n \"UniqueSymbol\",\n \"Symbol\",\n \"Undefined\",\n \"Void\",\n \"Never\",\n \"Enum\",\n \"TemplateLiteral\",\n \"ObjectKeyword\",\n () => false,\n ),\n Match.tag(\"Union\", ({ types }) =>\n Array.some(types, (type) => isRecursive(type)),\n ),\n Match.tag(\"Objects\", ({ propertySignatures }) =>\n Array.some(propertySignatures, ({ type }) => isRecursive(type)),\n ),\n Match.tag(\n \"Arrays\",\n ({ elements, rest }) =>\n Array.some(elements, (element) => isRecursive(element)) ||\n Array.some(rest, (element) => isRecursive(element)),\n ),\n Match.tag(\"Suspend\", () => true),\n Match.exhaustive,\n );\n\nexport const compileAst = (\n ast: SchemaAST.AST,\n isOptionalPropertyOfTypeLiteral = false,\n): Effect.Effect<\n Validator<any, any, any>,\n | UnsupportedSchemaTypeError\n | UnsupportedPropertySignatureKeyTypeError\n | IndexSignaturesAreNotSupportedError\n | MixedIndexAndPropertySignaturesAreNotSupportedError\n | OptionalTupleElementsAreNotSupportedError\n | EmptyTupleIsNotSupportedError\n | EmptyUnionIsNotSupportedError\n> =>\n isRecursive(ast)\n ? Effect.succeed(v.any())\n : pipe(\n ast,\n Match.value,\n Match.tag(\"Literal\", ({ literal }) =>\n pipe(\n literal,\n Match.value,\n Match.whenOr(\n Match.string,\n Match.number,\n Match.bigint,\n Match.boolean,\n (l) => v.literal(l),\n ),\n Match.exhaustive,\n Effect.succeed,\n ),\n ),\n Match.tag(\"Null\", () => Effect.succeed(v.null())),\n Match.tag(\"Boolean\", () => Effect.succeed(v.boolean())),\n Match.tag(\"String\", (stringAst) =>\n GenericId.tableName(stringAst).pipe(\n Option.match({\n onNone: () => Effect.succeed(v.string()),\n onSome: (tableName) => Effect.succeed(v.id(tableName)),\n }),\n ),\n ),\n Match.tag(\"Number\", () => Effect.succeed(v.float64())),\n Match.tag(\"BigInt\", () => Effect.succeed(v.int64())),\n Match.tag(\"Union\", (unionAst) =>\n handleUnion(unionAst, isOptionalPropertyOfTypeLiteral),\n ),\n Match.tag(\"Objects\", (objectsAst) => handleObjects(objectsAst)),\n Match.tag(\"Arrays\", (arraysAst) => handleArrays(arraysAst)),\n Match.tag(\"Unknown\", \"Any\", () => Effect.succeed(v.any())),\n Match.tag(\"Declaration\", (declaration) =>\n Effect.mapBoth(\n // oxlint-disable-next-line effecttsgo/any-unknown-in-error-context -- Codec inputs are context-free, but Effect's SchemaAST.DeclarationRun erases their requirements to `any`.\n declaration.run(declaration.typeParameters)(\n new ArrayBuffer(0),\n declaration,\n {},\n ) as Effect.Effect<ArrayBuffer, unknown, never>,\n {\n onSuccess: () => v.bytes(),\n onFailure: () =>\n new UnsupportedSchemaTypeError({\n schemaType: declaration._tag,\n }),\n },\n ),\n ),\n Match.tag(\"Suspend\", () => Effect.succeed(v.any())),\n Match.tag(\n \"UniqueSymbol\",\n \"Symbol\",\n \"Undefined\",\n \"Void\",\n \"Never\",\n \"Enum\",\n \"TemplateLiteral\",\n \"ObjectKeyword\",\n () =>\n Effect.fail(\n new UnsupportedSchemaTypeError({\n schemaType: ast._tag,\n }),\n ),\n ),\n Match.exhaustive,\n );\n\nconst handleUnion = (\n { types }: SchemaAST.Union,\n isOptionalPropertyOfTypeLiteral: boolean,\n) => handleUnionTypes(types, isOptionalPropertyOfTypeLiteral);\n\nconst handleUnionTypes = Effect.fnUntraced(function* (\n types: SchemaAST.Union[\"types\"],\n isOptionalPropertyOfTypeLiteral: boolean,\n) {\n const members = isOptionalPropertyOfTypeLiteral\n ? Array.filter(types, Predicate.not(SchemaAST.isUndefined))\n : types;\n\n const [firstValidator, secondValidator, ...restValidators] =\n yield* Effect.all(Array.map(members, (type) => compileAst(type)));\n\n if (firstValidator === undefined) {\n return yield* new EmptyUnionIsNotSupportedError();\n } else if (secondValidator === undefined) {\n return firstValidator;\n } else {\n return v.union(firstValidator, secondValidator, ...restValidators);\n }\n});\n\nconst handleObjects = (objectsAst: SchemaAST.Objects) =>\n pipe(\n objectsAst.indexSignatures,\n Array.head,\n Option.match({\n onNone: () => Effect.map(handlePropertySignatures(objectsAst), v.object),\n onSome: ({ parameter, type }) =>\n pipe(\n objectsAst.propertySignatures,\n Array.head,\n Option.match({\n onNone: () =>\n Effect.map(\n Effect.all({\n parameter_: compileAst(parameter),\n type_: compileAst(type),\n }),\n ({ parameter_, type_ }) => v.record(parameter_, type_),\n ),\n onSome: () =>\n Effect.fail(\n new MixedIndexAndPropertySignaturesAreNotSupportedError(),\n ),\n }),\n ),\n }),\n );\n\nconst handleArrays = ({ elements, rest }: SchemaAST.Arrays) =>\n handleArrayElements(elements, rest);\n\nconst handleArrayElements = Effect.fnUntraced(function* (\n elements: SchemaAST.Arrays[\"elements\"],\n rest: SchemaAST.Arrays[\"rest\"],\n) {\n const [f, s, ...r] = elements;\n\n const elementToValidator = (element: SchemaAST.AST) =>\n SchemaAST.isOptional(element)\n ? Effect.fail(new OptionalTupleElementsAreNotSupportedError())\n : compileAst(element);\n\n const arrayItemsValidator = yield* f === undefined\n ? pipe(\n rest,\n Array.head,\n Option.match({\n onNone: () => Effect.fail(new EmptyTupleIsNotSupportedError()),\n onSome: (type) => compileAst(type),\n }),\n )\n : s === undefined\n ? elementToValidator(f)\n : Effect.gen(function* () {\n const firstValidator = yield* elementToValidator(f);\n const secondValidator = yield* elementToValidator(s);\n const restValidators = yield* Effect.forEach(r, elementToValidator);\n\n return v.union(firstValidator, secondValidator, ...restValidators);\n });\n\n return v.array(arrayItemsValidator);\n});\n\nconst handlePropertySignatures = (objectsAst: SchemaAST.Objects) =>\n pipe(\n objectsAst.propertySignatures,\n Effect.forEach(({ type, name }) => {\n const isOptional = SchemaAST.isOptional(type);\n if (String.isString(name)) {\n // Somehow, somewhere, keys of type number are being coerced to strings…\n return Option.match(Number.parse(name), {\n onNone: () =>\n Effect.gen(function* () {\n const validator = yield* compileAst(type, isOptional);\n\n return {\n propertyName: name,\n validator: isOptional ? v.optional(validator) : validator,\n };\n }),\n onSome: (number) =>\n Effect.fail(\n new UnsupportedPropertySignatureKeyTypeError({\n propertyKey: number,\n }),\n ),\n });\n } else {\n return Effect.fail(\n new UnsupportedPropertySignatureKeyTypeError({ propertyKey: name }),\n );\n }\n }),\n Effect.andThen((propertyNamesWithValidators) =>\n pipe(\n propertyNamesWithValidators,\n Array.reduce(\n {} as Record<string, Validator<any, any, any>>,\n (acc, { propertyName, validator }) => ({\n [propertyName]: validator,\n ...acc,\n }),\n ),\n Effect.succeed,\n ),\n ),\n );\n\n// Errors\n\nexport class TopLevelMustBeObjectError extends Data.TaggedError(\n \"TopLevelMustBeObjectError\",\n) {\n /* v8 ignore start */\n override get message() {\n return \"Top level schema must be an object\";\n }\n /* v8 ignore stop */\n}\n\nexport class TopLevelMustBeObjectOrUnionError extends Data.TaggedError(\n \"TopLevelMustBeObjectOrUnionError\",\n) {\n /* v8 ignore start */\n override get message() {\n return \"Top level schema must be an object or a union\";\n }\n /* v8 ignore stop */\n}\n\nexport class UnsupportedPropertySignatureKeyTypeError extends Data.TaggedError(\n \"UnsupportedPropertySignatureKeyTypeError\",\n)<{\n readonly propertyKey: number | symbol;\n}> {\n /* v8 ignore start */\n override get message() {\n return `Unsupported property signature '${this.propertyKey.toString()}'. Property is of type '${typeof this.propertyKey}' but only 'string' properties are supported.`;\n }\n /* v8 ignore stop */\n}\n\nexport class EmptyTupleIsNotSupportedError extends Data.TaggedError(\n \"EmptyTupleIsNotSupportedError\",\n) {\n /* v8 ignore start */\n override get message() {\n return \"Tuple must have at least one element\";\n }\n /* v8 ignore stop */\n}\n\nexport class EmptyUnionIsNotSupportedError extends Data.TaggedError(\n \"EmptyUnionIsNotSupportedError\",\n) {\n /* v8 ignore start */\n override get message() {\n return \"Union must have at least one member that compiles to a validator\";\n }\n /* v8 ignore stop */\n}\n\nexport class UnsupportedSchemaTypeError extends Data.TaggedError(\n \"UnsupportedSchemaTypeError\",\n)<{\n readonly schemaType: SchemaAST.AST[\"_tag\"];\n}> {\n /* v8 ignore start */\n override get message() {\n return `Unsupported schema type '${this.schemaType}'`;\n }\n /* v8 ignore stop */\n}\n\nexport class IndexSignaturesAreNotSupportedError extends Data.TaggedError(\n \"IndexSignaturesAreNotSupportedError\",\n) {\n /* v8 ignore start */\n override get message() {\n return \"Index signatures are not supported\";\n }\n /* v8 ignore stop */\n}\n\nexport class MixedIndexAndPropertySignaturesAreNotSupportedError extends Data.TaggedError(\n \"MixedIndexAndPropertySignaturesAreNotSupportedError\",\n) {\n /* v8 ignore start */\n override get message() {\n return \"Mixed index and property signatures are not supported\";\n }\n /* v8 ignore stop */\n}\n\nexport class OptionalTupleElementsAreNotSupportedError extends Data.TaggedError(\n \"OptionalTupleElementsAreNotSupportedError\",\n) {\n /* v8 ignore start */\n override get message() {\n return \"Optional tuple elements are not supported\";\n }\n /* v8 ignore stop */\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,MAAa,qBACX,eACuB;CACvB,MAAM,MAAM,OAAO,UAAU,UAAU,CAAC,CAAC;CAEzC,OAAO,KACL,KACA,MAAM,OACN,MAAM,IAAI,YAAY,eACpB,MAAM,qBAAqB,WAAW,eAAe,IACjD,yBAAyB,UAAU,IACnC,OAAO,KAAK,IAAI,oCAAoC,CAAC,CAC3D,GACA,MAAM,aAAa,OAAO,KAAK,IAAI,0BAA0B,CAAC,CAAC,GAC/D,OAAO,OACT;AACF;AAIA,MAAa,wBACX,WAEA,OAAO,QAAQ,WAAW,OAAO,UAAU,MAAM,CAAC,CAAC,GAAG,CAAC;AAgBzD,MAAa,sBACX,WAC6C;CAC7C,MAAM,MAAM,OAAO,UAAU,MAAM,CAAC,CAAC;CAErC,OAAO,KACL,KACA,MAAM,OACN,MAAM,IAAI,YAAY,EAAE,sBACtB,MAAM,qBAAqB,eAAe,IAErC,WAAW,GAAG,IACf,OAAO,KAAK,IAAI,oCAAoC,CAAC,CAC3D,GACA,MAAM,IAAI,UAAU,aAAa,WAAW,QAAQ,CAAC,GACrD,MAAM,aAAa,OAAO,KAAK,IAAI,iCAAiC,CAAC,CAAC,GACtE,OAAO,OACT;AACF;AA+JA,MAAa,iBACX,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AAa,UAAU;EACrB,OAAO;CACT;AAEF;AAEA,IAAa,sDAAb,cAAyE,KAAK,YAC5E,qDACF,CAAC,CAAC;;CAEA,IAAa,UAAU;EACrB,OAAO;CACT;AAEF;AAEA,IAAa,4CAAb,cAA+D,KAAK,YAClE,2CACF,CAAC,CAAC;;CAEA,IAAa,UAAU;EACrB,OAAO;CACT;AAEF"}
|
package/dist/Spec.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import * as Record from "effect/Record";
|
|
2
|
-
import * as Struct from "effect/Struct";
|
|
3
2
|
import * as GroupSpec from "./GroupSpec";
|
|
4
3
|
export declare const TypeId = "~@confect/core/Spec";
|
|
5
4
|
export type TypeId = typeof TypeId;
|
|
@@ -13,8 +12,6 @@ export declare const isSpec: (u: unknown) => u is AnyWithProps;
|
|
|
13
12
|
*/
|
|
14
13
|
export interface Spec<Groups_ extends Record.ReadonlyRecord<string, GroupSpec.AnyWithProps> = {}> {
|
|
15
14
|
readonly [TypeId]: Readonly<Groups_>;
|
|
16
|
-
add<Group extends GroupSpec.AnyWithProps>(group: Group): Spec<Struct.Assign<Groups_, Record.ReadonlyRecord<GroupSpec.Name<Group>, Group>>>;
|
|
17
|
-
addAt<const Name extends string, Group extends GroupSpec.AnyWithProps>(name: Name, group: Group): Spec<Struct.Assign<Groups_, Record.ReadonlyRecord<Name, GroupSpec.NamedAt<Group, Name>>>>;
|
|
18
15
|
}
|
|
19
16
|
export interface Any {
|
|
20
17
|
readonly [TypeId]: unknown;
|
|
@@ -27,5 +24,5 @@ export type Groups<Spec_ extends AnyWithProps> = Spec_[TypeId][keyof Spec_[TypeI
|
|
|
27
24
|
* The group record stored by a spec, with its precise member types.
|
|
28
25
|
*/
|
|
29
26
|
export declare const groups: <Spec_ extends AnyWithProps>(self: Spec_) => Spec_[TypeId];
|
|
30
|
-
export declare const
|
|
27
|
+
export declare const fromGroups: (record: Record.ReadonlyRecord<string, GroupSpec.AnyWithProps>) => AnyWithProps;
|
|
31
28
|
//# sourceMappingURL=Spec.d.ts.map
|
package/dist/Spec.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Spec.d.ts","sourceRoot":"","sources":["../src/Spec.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"Spec.d.ts","sourceRoot":"","sources":["../src/Spec.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,SAAS,MAAM,aAAa,CAAC;AAEzC,eAAO,MAAM,MAAM,wBAAwB,CAAC;AAC5C,MAAM,MAAM,MAAM,GAAG,OAAO,MAAM,CAAC;AAEnC,eAAO,MAAM,MAAM,MAAO,OAAO,KAAG,CAAC,IAAI,YACP,CAAC;AAEnC;;;;;;GAMG;AACH,MAAM,WAAW,IAAI,CACnB,OAAO,SAAS,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,SAAS,CAAC,YAAY,CAAC,GAAG,EAAE;IAE1E,QAAQ,CAAC,CAAC,MAAM,CAAC,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAC;CACtC;AAED,MAAM,WAAW,GAAG;IAClB,QAAQ,CAAC,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC;CAC5B;AAED,MAAM,WAAW,YAAa,SAAQ,GAAG;IACvC,QAAQ,CAAC,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,SAAS,CAAC,YAAY,CAAC,CAAC;CAC1E;AAED,MAAM,MAAM,MAAM,CAAC,KAAK,SAAS,YAAY,IAC3C,KAAK,CAAC,MAAM,CAAC,CAAC,MAAM,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC;AAErC;;GAEG;AACH,eAAO,MAAM,MAAM,GAAI,KAAK,SAAS,YAAY,QACzC,KAAK,KACV,KAAK,CAAC,MAAM,CAAiB,CAAC;AAEjC,eAAO,MAAM,UAAU,WACb,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,SAAS,CAAC,YAAY,CAAC,KAC5D,YAID,CAAC"}
|
package/dist/Spec.js
CHANGED
|
@@ -2,13 +2,12 @@ import { __exportAll } from "./_virtual/_rolldown/runtime.js";
|
|
|
2
2
|
import { withName } from "./GroupSpec.js";
|
|
3
3
|
import * as Predicate from "effect/Predicate";
|
|
4
4
|
import * as Record from "effect/Record";
|
|
5
|
-
import * as Struct from "effect/Struct";
|
|
6
5
|
//#region src/Spec.ts
|
|
7
6
|
var Spec_exports = /* @__PURE__ */ __exportAll({
|
|
8
7
|
TypeId: () => TypeId,
|
|
8
|
+
fromGroups: () => fromGroups,
|
|
9
9
|
groups: () => groups,
|
|
10
|
-
isSpec: () => isSpec
|
|
11
|
-
make: () => make
|
|
10
|
+
isSpec: () => isSpec
|
|
12
11
|
});
|
|
13
12
|
const TypeId = "~@confect/core/Spec";
|
|
14
13
|
const isSpec = (u) => Predicate.hasProperty(u, TypeId);
|
|
@@ -16,17 +15,8 @@ const isSpec = (u) => Predicate.hasProperty(u, TypeId);
|
|
|
16
15
|
* The group record stored by a spec, with its precise member types.
|
|
17
16
|
*/
|
|
18
17
|
const groups = (self) => self[TypeId];
|
|
19
|
-
const
|
|
20
|
-
[TypeId]: record,
|
|
21
|
-
add(group) {
|
|
22
|
-
return makeRecord(Struct.assign(record, Record.singleton(group.name, group)));
|
|
23
|
-
},
|
|
24
|
-
addAt(name, group) {
|
|
25
|
-
return makeRecord(Struct.assign(record, Record.singleton(name, withName(name, group))));
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
const make = () => makeRecord({});
|
|
18
|
+
const fromGroups = (record) => ({ [TypeId]: Record.map(record, (group, name) => withName(name, group)) });
|
|
29
19
|
//#endregion
|
|
30
|
-
export { Spec_exports, TypeId, groups, isSpec
|
|
20
|
+
export { Spec_exports, TypeId, fromGroups, groups, isSpec };
|
|
31
21
|
|
|
32
22
|
//# sourceMappingURL=Spec.js.map
|
package/dist/Spec.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Spec.js","names":["GroupSpec.withName"],"sources":["../src/Spec.ts"],"sourcesContent":["import * as Predicate from \"effect/Predicate\";\nimport * as Record from \"effect/Record\";\nimport * as
|
|
1
|
+
{"version":3,"file":"Spec.js","names":["GroupSpec.withName"],"sources":["../src/Spec.ts"],"sourcesContent":["import * as Predicate from \"effect/Predicate\";\nimport * as Record from \"effect/Record\";\nimport * as GroupSpec from \"./GroupSpec\";\n\nexport const TypeId = \"~@confect/core/Spec\";\nexport type TypeId = typeof TypeId;\n\nexport const isSpec = (u: unknown): u is AnyWithProps =>\n Predicate.hasProperty(u, TypeId);\n\n/**\n * A Confect spec: a flat container of function groups. Groups may be of any\n * runtime—a group built with `GroupSpec.makeNode()` (a Node action group) sits\n * alongside `GroupSpec.make()` groups in the same namespace. The runtime of a\n * group lives on the group itself (`GroupSpec.runtime`) and on each function's\n * `RuntimeAndFunctionType`; the spec does not carry a runtime of its own.\n */\nexport interface Spec<\n Groups_ extends Record.ReadonlyRecord<string, GroupSpec.AnyWithProps> = {},\n> {\n readonly [TypeId]: Readonly<Groups_>;\n}\n\nexport interface Any {\n readonly [TypeId]: unknown;\n}\n\nexport interface AnyWithProps extends Any {\n readonly [TypeId]: Record.ReadonlyRecord<string, GroupSpec.AnyWithProps>;\n}\n\nexport type Groups<Spec_ extends AnyWithProps> =\n Spec_[TypeId][keyof Spec_[TypeId]];\n\n/**\n * The group record stored by a spec, with its precise member types.\n */\nexport const groups = <Spec_ extends AnyWithProps>(\n self: Spec_,\n): Spec_[TypeId] => self[TypeId];\n\nexport const fromGroups = (\n record: Record.ReadonlyRecord<string, GroupSpec.AnyWithProps>,\n): AnyWithProps => ({\n [TypeId]: Record.map(record, (group, name) =>\n GroupSpec.withName(name, group),\n ),\n});\n"],"mappings":";;;;;;;;;;;AAIA,MAAa,SAAS;AAGtB,MAAa,UAAU,MACrB,UAAU,YAAY,GAAG,MAAM;;;;AA6BjC,MAAa,UACX,SACkB,KAAK;AAEzB,MAAa,cACX,YACkB,GACjB,SAAS,OAAO,IAAI,SAAS,OAAO,SACnCA,SAAmB,MAAM,KAAK,CAChC,EACF"}
|
package/dist/SystemFields.js
CHANGED
|
@@ -7,10 +7,10 @@ import * as SchemaAST from "effect/SchemaAST";
|
|
|
7
7
|
import * as Record from "effect/Record";
|
|
8
8
|
import * as Effect from "effect/Effect";
|
|
9
9
|
import { pipe } from "effect/Function";
|
|
10
|
-
import * as Struct from "effect/Struct";
|
|
11
10
|
import * as Array from "effect/Array";
|
|
12
11
|
import * as SchemaGetter from "effect/SchemaGetter";
|
|
13
12
|
import * as SchemaTransformation from "effect/SchemaTransformation";
|
|
13
|
+
import * as Struct from "effect/Struct";
|
|
14
14
|
//#region src/SystemFields.ts
|
|
15
15
|
var SystemFields_exports = /* @__PURE__ */ __exportAll({
|
|
16
16
|
SystemFields: () => SystemFields,
|
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
|
|
26
|
-
|
|
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;
|
package/dist/Types.d.ts.map
CHANGED
|
@@ -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,CAA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,IAAI,CAAC,CAAC,SAAS,OAAO,GAAG,CAAC,CAAC,EAAE,CAAC,KAAK,IAAI,GAAG,KAAK,CAAC,SAAS,CAC5E,CAAC,EAAE,MAAM,YAAY,KAClB,IAAI,GACL,YAAY,GACZ,KAAK,CAAC;AAEV,KAAK,OAAO,CAAC,CAAC,EAAE,IAAI,IAAI,CAAC,SAAS,IAAI,GAAG,CAAC,GAAG,IAAI,CAAC;AAMlD,MAAM,MAAM,oBAAoB,CAC9B,eAAe,SAAS,gBAAgB,EACxC,KAAK,SAAS,qBAAqB,CAAC,eAAe,CAAC,EACpD,CAAC,SAAS,MAAM,EAAE,IAChB,CAAC,SAAS,SAAS,CAAC,GAAG,MAAM,IAAI,EAAE,GAAG,CAAC,GACvC,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,
|
|
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.
|
|
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.
|
|
11
|
-
"@types/node": "26.6.
|
|
10
|
+
"@effect/vitest": "4.0.4",
|
|
11
|
+
"@types/node": "26.6.5",
|
|
12
12
|
"confect-bench-harness": "0.0.0",
|
|
13
|
-
"effect": "4.0.
|
|
14
|
-
"tsdown": "0.23.
|
|
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
|
-
|
|
124
|
+
string,
|
|
150
125
|
infer Functions_,
|
|
151
126
|
infer Groups_,
|
|
152
127
|
infer MiddlewareSpecs_
|
|
153
128
|
>
|
|
154
129
|
? GroupSpec<
|
|
155
130
|
Runtime,
|
|
156
|
-
|
|
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
|
|
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> =>
|
package/src/SchemaToValidator.ts
CHANGED
|
@@ -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
|
-
?
|
|
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
|
|
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
|
-
:
|
|
166
|
-
?
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
:
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
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
|
-
:
|
|
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
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
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
|
-
?
|
|
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
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
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
|
|
231
|
-
| [false, true, ...infer VlRest
|
|
232
|
-
? ValueTupleToValidatorTuple<VlRest>
|
|
233
|
-
|
|
234
|
-
|
|
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
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
)
|
|
64
|
-
|
|
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
|
|
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
|
|
96
|
-
?
|
|
97
|
-
:
|
|
98
|
-
|
|
99
|
-
|
|
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
|
//////////////////////////////////
|