@ghostry/fabricator 0.0.15 → 0.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/Error/index.js +14 -9
- package/dist/esm/Fabricator/Constructor.js +44 -37
- package/dist/esm/Primitive/object/Fabricator.js +45 -43
- package/dist/esm/Primitive/object/Registry.js +41 -84
- package/dist/esm/Primitive/object/Stages.js +73 -0
- package/dist/esm/Primitive/object/compute/Fabricator.js +1 -7
- package/dist/esm/Primitive/object/compute/Registry.js +2 -4
- package/dist/esm/Primitive/recursive/Terminate.js +5 -13
- package/dist/esm/Unique/Capacity.js +2 -8
- package/dist/esm/Utility/ShallowMerge.js +1 -13
- package/dist/types/Error/index.d.ts +27 -18
- package/dist/types/Instance/Stack/Async.d.ts +9 -5
- package/dist/types/Primitive/object/Fabricator.d.ts +14 -18
- package/dist/types/Primitive/object/Registry.d.ts +6 -8
- package/dist/types/Primitive/object/Schema.d.ts +15 -7
- package/dist/types/Primitive/object/Stages.d.ts +68 -0
- package/dist/types/Primitive/object/Types.d.ts +106 -12
- package/dist/types/Primitive/object/compute/Fabricator.d.ts +6 -7
- package/dist/types/Primitive/object/compute/Types.d.ts +6 -7
- package/dist/types/Utility/ShallowMerge.d.ts +6 -2
- package/package.json +1 -2
package/dist/esm/Error/index.js
CHANGED
|
@@ -93,7 +93,7 @@ class Error_FabricatorError extends Error {
|
|
|
93
93
|
constructor(){
|
|
94
94
|
super();
|
|
95
95
|
this.name = "@ghostry/fabricator:DetachedComputeError";
|
|
96
|
-
this.message = "A `.refine()`-computed field can only be fabricated as
|
|
96
|
+
this.message = "A `.refine()`-computed field can only be built and fabricated as a field of the object that owns it, which supplies the fields its resolver reads.";
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
99
|
FabricatorError.DetachedComputeError = DetachedComputeError;
|
|
@@ -121,14 +121,19 @@ class Error_FabricatorError extends Error {
|
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
123
|
FabricatorError.UnknownFieldError = UnknownFieldError;
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
124
|
+
const PLACES = {
|
|
125
|
+
object: "T.object",
|
|
126
|
+
extend: ".extend",
|
|
127
|
+
rebase: ".rebase"
|
|
128
|
+
};
|
|
129
|
+
class ComputeOutsideRefineError extends FabricatorError {
|
|
130
|
+
constructor(field, operation){
|
|
131
|
+
super(), _define_property(this, "field", void 0), _define_property(this, "operation", void 0), this.field = field, this.operation = operation;
|
|
132
|
+
this.name = "@ghostry/fabricator:ComputeOutsideRefineError";
|
|
133
|
+
this.message = `Cannot place a computed field in \`${PLACES[operation]}(...)\`: "${field}". Add computed fields with \`.refine(({ compute }) => ...)\`.`;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
FabricatorError.ComputeOutsideRefineError = ComputeOutsideRefineError;
|
|
132
137
|
class InvalidOverrideValueError extends FabricatorError {
|
|
133
138
|
constructor(field, kind, value){
|
|
134
139
|
super(), _define_property(this, "field", void 0), _define_property(this, "kind", void 0), _define_property(this, "value", void 0), this.field = field, this.kind = kind, this.value = value;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { FabricatorError } from "../Error/index.js";
|
|
2
2
|
import { toEffectiveSource } from "../Instance/Stack/Visible.js";
|
|
3
3
|
import { Primitive } from "../Primitive/index.js";
|
|
4
|
+
import { stageOf } from "../Primitive/object/Stages.js";
|
|
4
5
|
import { isLayered, normalizeSalt, toRandomSource } from "../Random/index.js";
|
|
5
6
|
import { toSchema } from "../Schema/Core.js";
|
|
6
7
|
import { Adaptation, Fixed, Kind, Layer, Meta } from "../Types.js";
|
|
@@ -224,13 +225,7 @@ function Constructor(source, stack, ancestry, exhaustion = "throw") {
|
|
|
224
225
|
}, from);
|
|
225
226
|
}
|
|
226
227
|
case "object.compute":
|
|
227
|
-
|
|
228
|
-
const s = schema;
|
|
229
|
-
return Primitive.object.compute.Fabricator({
|
|
230
|
-
...common,
|
|
231
|
-
schema: s
|
|
232
|
-
});
|
|
233
|
-
}
|
|
228
|
+
throw new FabricatorError.DetachedComputeError();
|
|
234
229
|
case "object.omittable":
|
|
235
230
|
{
|
|
236
231
|
const s = schema;
|
|
@@ -258,44 +253,56 @@ function Constructor(source, stack, ancestry, exhaustion = "throw") {
|
|
|
258
253
|
case "object":
|
|
259
254
|
{
|
|
260
255
|
const s = schema;
|
|
261
|
-
const
|
|
262
|
-
const
|
|
263
|
-
for(const key
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
const built = make(fieldSchema, fieldPath, context);
|
|
272
|
-
const value = fieldSchema[Fixed];
|
|
273
|
-
if (Primitive.object.compute.isObjectComputeFabricator(built)) return {
|
|
274
|
-
...built,
|
|
275
|
-
[Kind]: built[Meta].source[Kind],
|
|
276
|
-
fabricate: ()=>value
|
|
277
|
-
};
|
|
278
|
-
return {
|
|
279
|
-
...built,
|
|
280
|
-
fabricate: ()=>value
|
|
281
|
-
};
|
|
282
|
-
}
|
|
283
|
-
return make(fieldSchema, fieldPath, context);
|
|
284
|
-
});
|
|
285
|
-
}
|
|
286
|
-
return fields;
|
|
287
|
-
};
|
|
288
|
-
const fields = build(s[Meta].definition);
|
|
289
|
-
const hidden = s[Meta].hidden ? build(s[Meta].hidden) : {};
|
|
256
|
+
const stages = s[Meta].stages.map((stage)=>{
|
|
257
|
+
const built = {};
|
|
258
|
+
for (const key of Object.keys(stage))built[key] = buildField(stage[key], [
|
|
259
|
+
...path,
|
|
260
|
+
key
|
|
261
|
+
], context);
|
|
262
|
+
return built;
|
|
263
|
+
});
|
|
264
|
+
const fields = {};
|
|
265
|
+
for (const key of Object.keys(s[Meta].definition))fields[key] = stages[stageOf(s[Meta].stages, key)][key];
|
|
290
266
|
return Primitive.object.Fabricator({
|
|
291
267
|
...common,
|
|
292
268
|
schema: Primitive.object.rehydrate(s)
|
|
293
|
-
}, fields,
|
|
269
|
+
}, fields, stages);
|
|
294
270
|
}
|
|
295
271
|
default:
|
|
296
272
|
throw new FabricatorError.UnknownKindError(String(kind), "construction");
|
|
297
273
|
}
|
|
298
274
|
}
|
|
275
|
+
function buildField(fieldSchema, fieldPath, context) {
|
|
276
|
+
fieldSchema = Fixed in fieldSchema ? {
|
|
277
|
+
...toSchema(fieldSchema),
|
|
278
|
+
[Fixed]: fieldSchema[Fixed]
|
|
279
|
+
} : toSchema(fieldSchema);
|
|
280
|
+
if ("object.compute" === fieldSchema[Kind]) {
|
|
281
|
+
const s = fieldSchema;
|
|
282
|
+
const built = Primitive.object.compute.Fabricator({
|
|
283
|
+
algorithm: context.algorithm,
|
|
284
|
+
trace: context.toTrace(fieldPath, "object.compute"),
|
|
285
|
+
exhaustion: context.exhaustion,
|
|
286
|
+
schema: s
|
|
287
|
+
});
|
|
288
|
+
if (!(Fixed in fieldSchema)) return built;
|
|
289
|
+
const value = fieldSchema[Fixed];
|
|
290
|
+
return {
|
|
291
|
+
...built,
|
|
292
|
+
[Kind]: s[Meta].source[Kind],
|
|
293
|
+
fabricate: ()=>value
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
if (Fixed in fieldSchema) {
|
|
297
|
+
const built = make(fieldSchema, fieldPath, context);
|
|
298
|
+
const value = fieldSchema[Fixed];
|
|
299
|
+
return {
|
|
300
|
+
...built,
|
|
301
|
+
fabricate: ()=>value
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
return make(fieldSchema, fieldPath, context);
|
|
305
|
+
}
|
|
299
306
|
function construct(schema, options = {}) {
|
|
300
307
|
var _options_path;
|
|
301
308
|
if ("string" == typeof options.kind && options.kind !== schema[Kind]) throw new FabricatorError.TraceKindMismatchError(schema[Kind], options.kind);
|
|
@@ -6,10 +6,11 @@ import { isPollutionKey } from "../../Utility/Core.js";
|
|
|
6
6
|
import { isObjectComputeFabricator } from "./compute/Fabricator.js";
|
|
7
7
|
import { isObjectOmittableFabricator } from "./omittable/Fabricator.js";
|
|
8
8
|
import { isObjectOptionalFabricator } from "./optional/Fabricator.js";
|
|
9
|
+
import { stageOf } from "./Stages.js";
|
|
9
10
|
function isObjectFabricator(candidate) {
|
|
10
11
|
return "object" === candidate[Kind];
|
|
11
12
|
}
|
|
12
|
-
function Fabricator(context, fields,
|
|
13
|
+
function Fabricator(context, fields, stages) {
|
|
13
14
|
const { schema, algorithm, trace } = context;
|
|
14
15
|
const meta = schema[Meta];
|
|
15
16
|
if (meta.produce) {
|
|
@@ -38,41 +39,56 @@ function Fabricator(context, fields, hidden = {}) {
|
|
|
38
39
|
schema
|
|
39
40
|
};
|
|
40
41
|
}
|
|
42
|
+
const entries = stages.flatMap((stage, at)=>Object.keys(stage).map((key)=>{
|
|
43
|
+
const field = stage[key];
|
|
44
|
+
return {
|
|
45
|
+
at,
|
|
46
|
+
key,
|
|
47
|
+
field,
|
|
48
|
+
visible: fields[key] === field,
|
|
49
|
+
reads: isObjectComputeFabricator(field) ? field[Meta].dependencies.map((dependency)=>[
|
|
50
|
+
dependency,
|
|
51
|
+
stageOf(stages, dependency, at)
|
|
52
|
+
]) : []
|
|
53
|
+
};
|
|
54
|
+
}));
|
|
41
55
|
const fabricate = (overrides, config)=>{
|
|
42
56
|
if (overrides && (null == config ? void 0 : config.validate) !== false) schema.override(overrides);
|
|
43
|
-
const
|
|
44
|
-
const deferred =
|
|
45
|
-
const
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
...Object.entries(fields).map(([k, v])=>[
|
|
52
|
-
k,
|
|
53
|
-
v,
|
|
54
|
-
true
|
|
55
|
-
])
|
|
56
|
-
];
|
|
57
|
-
for (const [k, v, visible] of entries){
|
|
58
|
-
if (isPollutionKey(k)) throw new FabricatorError.PrototypePollutionError(k);
|
|
59
|
-
if (visible && overrides && k in overrides) {
|
|
60
|
-
const value = overrides[k];
|
|
61
|
-
recordOverride(v, value);
|
|
57
|
+
const values = stages.map(()=>new Map());
|
|
58
|
+
const deferred = [];
|
|
59
|
+
for (const entry of entries){
|
|
60
|
+
const { at, key, field, visible } = entry;
|
|
61
|
+
if (isPollutionKey(key)) throw new FabricatorError.PrototypePollutionError(key);
|
|
62
|
+
if (visible && overrides && key in overrides) {
|
|
63
|
+
const value = overrides[key];
|
|
64
|
+
recordOverride(field, value);
|
|
62
65
|
if (value === Omitted) continue;
|
|
63
|
-
|
|
66
|
+
values[at].set(key, isObjectFabricator(field) ? field.fabricate(value, {
|
|
64
67
|
validate: false
|
|
65
|
-
}) : value;
|
|
68
|
+
}) : value);
|
|
66
69
|
continue;
|
|
67
70
|
}
|
|
68
|
-
if (isObjectOmittableFabricator(
|
|
69
|
-
const value =
|
|
70
|
-
if (value !== Omitted)
|
|
71
|
-
} else if (isObjectComputeFabricator(
|
|
72
|
-
else
|
|
71
|
+
if (isObjectOmittableFabricator(field) || isObjectOptionalFabricator(field)) {
|
|
72
|
+
const value = field.fabricate();
|
|
73
|
+
if (value !== Omitted) values[at].set(key, value);
|
|
74
|
+
} else if (isObjectComputeFabricator(field)) deferred.push(entry);
|
|
75
|
+
else values[at].set(key, field.fabricate());
|
|
76
|
+
}
|
|
77
|
+
for (const { at, key, field, reads } of deferred){
|
|
78
|
+
const input = {};
|
|
79
|
+
for (const [dependency, from] of reads){
|
|
80
|
+
const read = values[from];
|
|
81
|
+
if (null == read ? void 0 : read.has(dependency)) input[dependency] = read.get(dependency);
|
|
82
|
+
}
|
|
83
|
+
values[at].set(key, field.fabricate({
|
|
84
|
+
fabricated: input
|
|
85
|
+
}));
|
|
86
|
+
}
|
|
87
|
+
const fabricated = {};
|
|
88
|
+
for (const key of Object.keys(fields)){
|
|
89
|
+
const read = values[stageOf(stages, key)];
|
|
90
|
+
if (read.has(key)) fabricated[key] = read.get(key);
|
|
73
91
|
}
|
|
74
|
-
resolveComputed(deferred, fabricated);
|
|
75
|
-
for (const k of Object.keys(hidden))delete fabricated[k];
|
|
76
92
|
return fabricated;
|
|
77
93
|
};
|
|
78
94
|
return {
|
|
@@ -84,18 +100,4 @@ function Fabricator(context, fields, hidden = {}) {
|
|
|
84
100
|
schema
|
|
85
101
|
};
|
|
86
102
|
}
|
|
87
|
-
function resolveComputed(deferred, fabricated) {
|
|
88
|
-
const resolved = new Set();
|
|
89
|
-
const resolve = (key)=>{
|
|
90
|
-
if (resolved.has(key)) return;
|
|
91
|
-
const field = deferred.get(key);
|
|
92
|
-
if (!field) return;
|
|
93
|
-
for (const dependency of field[Meta].dependencies)resolve(dependency);
|
|
94
|
-
fabricated[key] = field.fabricate({
|
|
95
|
-
fabricated
|
|
96
|
-
});
|
|
97
|
-
resolved.add(key);
|
|
98
|
-
};
|
|
99
|
-
for (const key of deferred.keys())resolve(key);
|
|
100
|
-
}
|
|
101
103
|
export { Fabricator, isObjectFabricator };
|
|
@@ -3,7 +3,6 @@ import { FabricatorError } from "../../Error/index.js";
|
|
|
3
3
|
import { toSchema, toValueKind, violatesKind } from "../../Schema/Core.js";
|
|
4
4
|
import { Adaptation, Fixed, Kind, Meta, Omitted } from "../../Types.js";
|
|
5
5
|
import { isPlainObject } from "../../Utility/Core.js";
|
|
6
|
-
import { shallowMerge } from "../../Utility/ShallowMerge.js";
|
|
7
6
|
import { isNullableSchema } from "../nullable/Schema.js";
|
|
8
7
|
import { isNullishSchema } from "../nullish/Schema.js";
|
|
9
8
|
import { isUndefinableSchema } from "../undefinable/Schema.js";
|
|
@@ -12,58 +11,62 @@ import { isObjectComputeSchema } from "./compute/Schema.js";
|
|
|
12
11
|
import { isObjectOmittableSchema } from "./omittable/Schema.js";
|
|
13
12
|
import { isObjectOptionalSchema } from "./optional/Schema.js";
|
|
14
13
|
import { Schema, isObjectSchema } from "./Schema.js";
|
|
14
|
+
import { firstStageOf, prune, stageKeys, toDefinition, withEntries, withOriginals } from "./Stages.js";
|
|
15
15
|
function normalizeDefinition(definition) {
|
|
16
16
|
return Object.fromEntries(Object.entries(definition).map(([k, v])=>[
|
|
17
17
|
k,
|
|
18
18
|
toSchema(v)
|
|
19
19
|
]));
|
|
20
20
|
}
|
|
21
|
-
function
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
return
|
|
21
|
+
function normalizeFields(definition, operation) {
|
|
22
|
+
const normalized = normalizeDefinition(definition);
|
|
23
|
+
for (const [key, entry] of Object.entries(normalized))if (isObjectComputeSchema(entry)) throw new FabricatorError.ComputeOutsideRefineError(key, operation);
|
|
24
|
+
return normalized;
|
|
25
25
|
}
|
|
26
|
-
function
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
...
|
|
30
|
-
|
|
26
|
+
function appendKeys(order, keys) {
|
|
27
|
+
const known = new Set(order);
|
|
28
|
+
return [
|
|
29
|
+
...order,
|
|
30
|
+
...keys.filter((key)=>!known.has(key))
|
|
31
|
+
];
|
|
32
|
+
}
|
|
33
|
+
function make(allStages, order, adaptations) {
|
|
34
|
+
const stages = prune(allStages, order);
|
|
35
|
+
const definition = toDefinition(stages, order);
|
|
31
36
|
const schema = Schema({
|
|
32
37
|
[Kind]: "object",
|
|
33
|
-
[Meta]:
|
|
34
|
-
definition,
|
|
35
|
-
refinements,
|
|
36
|
-
hidden
|
|
37
|
-
} : {
|
|
38
|
+
[Meta]: {
|
|
38
39
|
definition,
|
|
39
|
-
|
|
40
|
+
stages
|
|
40
41
|
},
|
|
41
42
|
...adaptations ? {
|
|
42
43
|
[Adaptation]: adaptations
|
|
43
44
|
} : void 0,
|
|
44
45
|
extend: (extender)=>{
|
|
45
|
-
const extension =
|
|
46
|
+
const extension = normalizeFields(extender({
|
|
46
47
|
base: definition
|
|
47
|
-
}));
|
|
48
|
-
|
|
49
|
-
return make(merged, refinements, adaptations, unhide(hidden, extension));
|
|
48
|
+
}), "extend");
|
|
49
|
+
return make(withEntries(stages, extension), appendKeys(order, Object.keys(extension)), adaptations);
|
|
50
50
|
},
|
|
51
51
|
refine: (refiner)=>{
|
|
52
|
-
const compute = compute_0(
|
|
52
|
+
const compute = compute_0(order);
|
|
53
53
|
const refinement = normalizeDefinition(refiner({
|
|
54
54
|
base: schema,
|
|
55
55
|
compute
|
|
56
56
|
}));
|
|
57
|
-
return make(
|
|
58
|
-
...
|
|
57
|
+
return make([
|
|
58
|
+
...stages,
|
|
59
59
|
refinement
|
|
60
|
-
],
|
|
60
|
+
], appendKeys(order, Object.keys(refinement)), adaptations);
|
|
61
|
+
},
|
|
62
|
+
rebase: (fields)=>{
|
|
63
|
+
const originals = normalizeFields(fields, "rebase");
|
|
64
|
+
for (const key of Object.keys(originals))if (-1 === firstStageOf(stages, key)) throw new FabricatorError.UnknownFieldError(key, stageKeys(stages, order), "rebase");
|
|
65
|
+
return make(withOriginals(stages, originals), order, adaptations);
|
|
61
66
|
},
|
|
62
|
-
adapt: (adapter, produce)=>make(
|
|
67
|
+
adapt: (adapter, produce)=>make(stages, order, mergeAdaptations(adaptations, adapter, produce)),
|
|
63
68
|
override: (override)=>{
|
|
64
|
-
const overridden = {
|
|
65
|
-
...definition
|
|
66
|
-
};
|
|
69
|
+
const overridden = {};
|
|
67
70
|
for (const key of Object.keys(override)){
|
|
68
71
|
const fieldSchema = definition[key];
|
|
69
72
|
if (!fieldSchema) throw new FabricatorError.UnknownFieldError(key, Object.keys(definition), "override");
|
|
@@ -146,61 +149,17 @@ function make(definition, refinements, adaptations, hidden) {
|
|
|
146
149
|
[Fixed]: value
|
|
147
150
|
};
|
|
148
151
|
}
|
|
149
|
-
return make(overridden,
|
|
152
|
+
return make(withEntries(stages, overridden), order, adaptations);
|
|
150
153
|
}
|
|
151
154
|
});
|
|
152
155
|
return schema;
|
|
153
156
|
}
|
|
154
157
|
function rehydrate(schema) {
|
|
155
|
-
const rehydrated = make(schema[Meta].
|
|
158
|
+
const rehydrated = make(schema[Meta].stages, Object.keys(schema[Meta].definition), schema[Adaptation]);
|
|
156
159
|
return schema[Meta].produce ? rehydrated.as(schema[Meta].produce) : rehydrated;
|
|
157
160
|
}
|
|
158
|
-
function dependenciesOf(entry) {
|
|
159
|
-
if (Fixed in entry) return [];
|
|
160
|
-
if (!isObjectComputeSchema(entry)) return [];
|
|
161
|
-
return entry[Meta].dependencies;
|
|
162
|
-
}
|
|
163
|
-
function assertAcyclic(all) {
|
|
164
|
-
const visiting = [];
|
|
165
|
-
const visited = new Set();
|
|
166
|
-
const visit = (key)=>{
|
|
167
|
-
if (visited.has(key)) return;
|
|
168
|
-
const entry = all[key];
|
|
169
|
-
if (!entry) return;
|
|
170
|
-
if (visiting.includes(key)) {
|
|
171
|
-
const loop = visiting.slice(visiting.indexOf(key));
|
|
172
|
-
throw new FabricatorError.ComputeDependencyCycleError([
|
|
173
|
-
...loop,
|
|
174
|
-
key
|
|
175
|
-
]);
|
|
176
|
-
}
|
|
177
|
-
visiting.push(key);
|
|
178
|
-
for (const dependency of dependenciesOf(entry))visit(dependency);
|
|
179
|
-
visiting.pop();
|
|
180
|
-
visited.add(key);
|
|
181
|
-
};
|
|
182
|
-
for (const key of Object.keys(all))visit(key);
|
|
183
|
-
}
|
|
184
|
-
function requiredKeys(kept, all) {
|
|
185
|
-
const needed = new Set(kept);
|
|
186
|
-
const pending = [
|
|
187
|
-
...kept
|
|
188
|
-
];
|
|
189
|
-
while(pending.length > 0){
|
|
190
|
-
const entry = all[pending.pop()];
|
|
191
|
-
if (!entry) continue;
|
|
192
|
-
for (const dependency of dependenciesOf(entry))if (!needed.has(dependency)) {
|
|
193
|
-
needed.add(dependency);
|
|
194
|
-
pending.push(dependency);
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
return needed;
|
|
198
|
-
}
|
|
199
|
-
function selectEntries(definition, keys) {
|
|
200
|
-
return Object.fromEntries(Object.entries(definition).filter(([key])=>keys.has(key)));
|
|
201
|
-
}
|
|
202
161
|
function subset(source, keys, operation) {
|
|
203
|
-
const { definition,
|
|
162
|
+
const { definition, stages } = source[Meta];
|
|
204
163
|
const available = Object.keys(definition);
|
|
205
164
|
for (const key of keys)if (!available.includes(key)) throw new FabricatorError.UnknownFieldError(key, available, operation);
|
|
206
165
|
const named = new Set(keys);
|
|
@@ -212,15 +171,13 @@ function subset(source, keys, operation) {
|
|
|
212
171
|
return !named.has(key);
|
|
213
172
|
}
|
|
214
173
|
});
|
|
215
|
-
|
|
216
|
-
const all = {
|
|
217
|
-
...hidden,
|
|
218
|
-
...definition
|
|
219
|
-
};
|
|
220
|
-
const required = requiredKeys(kept, all);
|
|
221
|
-
const hiddenKeys = new Set(Object.keys(all).filter((key)=>required.has(key) && !keptSet.has(key)));
|
|
222
|
-
return make(selectEntries(definition, keptSet), refinements.map((refinement)=>selectEntries(refinement, keptSet)).filter((refinement)=>Object.keys(refinement).length > 0), void 0, hiddenKeys.size > 0 ? selectEntries(all, hiddenKeys) : void 0);
|
|
174
|
+
return make(stages, kept);
|
|
223
175
|
}
|
|
224
|
-
const Registry = (definition)=>
|
|
176
|
+
const Registry = (definition)=>{
|
|
177
|
+
const fields = normalizeFields(definition, "object");
|
|
178
|
+
return make([
|
|
179
|
+
fields
|
|
180
|
+
], Object.keys(fields));
|
|
181
|
+
};
|
|
225
182
|
export default Registry;
|
|
226
183
|
export { rehydrate, subset };
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Fixed, Kind, Meta } from "../../Types.js";
|
|
2
|
+
function stageOf(stages, key, before = stages.length) {
|
|
3
|
+
for(let i = Math.min(before, stages.length) - 1; i >= 0; i--)if (has(stages[i], key)) return i;
|
|
4
|
+
return -1;
|
|
5
|
+
}
|
|
6
|
+
function firstStageOf(stages, key) {
|
|
7
|
+
return stages.findIndex((stage)=>has(stage, key));
|
|
8
|
+
}
|
|
9
|
+
function stageKeys(stages, order) {
|
|
10
|
+
const keys = new Set(order);
|
|
11
|
+
for (const stage of stages)for (const key of Object.keys(stage))keys.add(key);
|
|
12
|
+
return [
|
|
13
|
+
...keys
|
|
14
|
+
];
|
|
15
|
+
}
|
|
16
|
+
function readsOf(entry) {
|
|
17
|
+
if (Fixed in entry) return [];
|
|
18
|
+
if ("object.compute" !== entry[Kind]) return [];
|
|
19
|
+
return entry[Meta].dependencies;
|
|
20
|
+
}
|
|
21
|
+
function toDefinition(stages, order) {
|
|
22
|
+
const definition = {};
|
|
23
|
+
for (const key of order)put(definition, key, stages[stageOf(stages, key)][key]);
|
|
24
|
+
return definition;
|
|
25
|
+
}
|
|
26
|
+
function withEntries(stages, entries) {
|
|
27
|
+
const next = stages.map((stage)=>({
|
|
28
|
+
...stage
|
|
29
|
+
}));
|
|
30
|
+
if (0 === next.length) next.push({});
|
|
31
|
+
for (const [key, entry] of Object.entries(entries)){
|
|
32
|
+
const at = stageOf(next, key);
|
|
33
|
+
put(next[-1 === at ? next.length - 1 : at], key, entry);
|
|
34
|
+
}
|
|
35
|
+
return next;
|
|
36
|
+
}
|
|
37
|
+
function withOriginals(stages, entries) {
|
|
38
|
+
const next = stages.map((stage)=>({
|
|
39
|
+
...stage
|
|
40
|
+
}));
|
|
41
|
+
for (const [key, entry] of Object.entries(entries))put(next[firstStageOf(next, key)], key, entry);
|
|
42
|
+
return next;
|
|
43
|
+
}
|
|
44
|
+
function prune(stages, order) {
|
|
45
|
+
const kept = stages.map(()=>new Set());
|
|
46
|
+
const pending = [];
|
|
47
|
+
const keep = (at, key)=>{
|
|
48
|
+
if (-1 === at || kept[at].has(key)) return;
|
|
49
|
+
kept[at].add(key);
|
|
50
|
+
pending.push([
|
|
51
|
+
at,
|
|
52
|
+
key
|
|
53
|
+
]);
|
|
54
|
+
};
|
|
55
|
+
for (const key of order)keep(stageOf(stages, key), key);
|
|
56
|
+
while(pending.length > 0){
|
|
57
|
+
const [at, key] = pending.pop();
|
|
58
|
+
for (const dependency of readsOf(stages[at][key]))keep(stageOf(stages, dependency, at), dependency);
|
|
59
|
+
}
|
|
60
|
+
return stages.map((stage, i)=>Object.fromEntries(Object.entries(stage).filter(([key])=>kept[i].has(key)))).filter((stage)=>Object.keys(stage).length > 0);
|
|
61
|
+
}
|
|
62
|
+
function has(target, key) {
|
|
63
|
+
return Object.prototype.hasOwnProperty.call(target, key);
|
|
64
|
+
}
|
|
65
|
+
function put(target, key, value) {
|
|
66
|
+
Object.defineProperty(target, key, {
|
|
67
|
+
value,
|
|
68
|
+
enumerable: true,
|
|
69
|
+
writable: true,
|
|
70
|
+
configurable: true
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
export { firstStageOf, prune, readsOf, stageKeys, stageOf, toDefinition, withEntries, withOriginals };
|
|
@@ -13,9 +13,8 @@ function Fabricator(context) {
|
|
|
13
13
|
fabricate: (params)=>{
|
|
14
14
|
if (!params) throw new FabricatorError.DetachedComputeError();
|
|
15
15
|
const meta = schema[Meta];
|
|
16
|
-
const fabricated = meta.narrowed ? narrow(params.fabricated, meta.dependencies) : params.fabricated;
|
|
17
16
|
const resolved = meta.resolve({
|
|
18
|
-
fabricated
|
|
17
|
+
fabricated: params.fabricated
|
|
19
18
|
});
|
|
20
19
|
const sourceKind = meta.source[Kind];
|
|
21
20
|
if (violatesKind(sourceKind, resolved)) throw new FabricatorError.ComputeResultMismatchError(sourceKind, resolved);
|
|
@@ -23,9 +22,4 @@ function Fabricator(context) {
|
|
|
23
22
|
}
|
|
24
23
|
};
|
|
25
24
|
}
|
|
26
|
-
function narrow(fabricated, dependencies) {
|
|
27
|
-
const narrowed = {};
|
|
28
|
-
for (const key of dependencies)if (key in fabricated) narrowed[key] = fabricated[key];
|
|
29
|
-
return narrowed;
|
|
30
|
-
}
|
|
31
25
|
export { Fabricator, isObjectComputeFabricator };
|
|
@@ -8,8 +8,7 @@ function make(base) {
|
|
|
8
8
|
[Meta]: {
|
|
9
9
|
source,
|
|
10
10
|
resolve,
|
|
11
|
-
dependencies: base
|
|
12
|
-
narrowed: false
|
|
11
|
+
dependencies: base
|
|
13
12
|
}
|
|
14
13
|
}),
|
|
15
14
|
from: (keys)=>{
|
|
@@ -20,8 +19,7 @@ function make(base) {
|
|
|
20
19
|
[Meta]: {
|
|
21
20
|
source,
|
|
22
21
|
resolve,
|
|
23
|
-
dependencies: keys
|
|
24
|
-
narrowed: true
|
|
22
|
+
dependencies: keys
|
|
25
23
|
}
|
|
26
24
|
})
|
|
27
25
|
};
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { FabricatorError } from "../../Error/index.js";
|
|
2
2
|
import { Adaptation, Kind, Meta, Omitted } from "../../Types.js";
|
|
3
3
|
import { never } from "../../Utility/Core.js";
|
|
4
|
+
import { toDefinition } from "../object/Stages.js";
|
|
4
5
|
import { toStatedSize } from "../record/Admits.js";
|
|
5
6
|
function terminate(body) {
|
|
6
7
|
return terminateAt(body, []);
|
|
@@ -74,16 +75,11 @@ function terminateAt(schema, path) {
|
|
|
74
75
|
]);
|
|
75
76
|
return next;
|
|
76
77
|
};
|
|
77
|
-
const
|
|
78
|
-
if (!s[Meta].hidden) return withMeta(s, {
|
|
79
|
-
...s[Meta],
|
|
80
|
-
definition
|
|
81
|
-
});
|
|
82
|
-
const hidden = terminateFields(s[Meta].hidden);
|
|
78
|
+
const stages = s[Meta].stages.map(terminateFields);
|
|
83
79
|
return withMeta(s, {
|
|
84
80
|
...s[Meta],
|
|
85
|
-
|
|
86
|
-
|
|
81
|
+
stages,
|
|
82
|
+
definition: toDefinition(stages, Object.keys(s[Meta].definition))
|
|
87
83
|
});
|
|
88
84
|
}
|
|
89
85
|
case "tuple":
|
|
@@ -185,11 +181,7 @@ function containsSelf(schema) {
|
|
|
185
181
|
case "object":
|
|
186
182
|
{
|
|
187
183
|
const s = schema;
|
|
188
|
-
|
|
189
|
-
...s[Meta].hidden,
|
|
190
|
-
...s[Meta].definition
|
|
191
|
-
};
|
|
192
|
-
return Object.keys(all).some((key)=>containsSelf(all[key]));
|
|
184
|
+
return s[Meta].stages.some((stage)=>Object.keys(stage).some((key)=>containsSelf(stage[key])));
|
|
193
185
|
}
|
|
194
186
|
case "tuple":
|
|
195
187
|
{
|
|
@@ -22,14 +22,8 @@ function walk(node, draws, clock, limits) {
|
|
|
22
22
|
const visit = (child, times = draws)=>walk(child, times, clock, limits);
|
|
23
23
|
switch(node[Kind]){
|
|
24
24
|
case "object":
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
...meta.definition,
|
|
28
|
-
...meta.hidden
|
|
29
|
-
};
|
|
30
|
-
for (const field of Object.values(fields))if (!(Fixed in field)) visit(field);
|
|
31
|
-
return;
|
|
32
|
-
}
|
|
25
|
+
for (const stage of meta.stages)for (const field of Object.values(stage))if (!(Fixed in field)) visit(field);
|
|
26
|
+
return;
|
|
33
27
|
case "tuple":
|
|
34
28
|
meta.items.forEach((item)=>visit(item));
|
|
35
29
|
return;
|
|
@@ -1,13 +1 @@
|
|
|
1
|
-
|
|
2
|
-
import { mergeableKeys } from "./Core.js";
|
|
3
|
-
function shallowMerge(a, b) {
|
|
4
|
-
const result = {};
|
|
5
|
-
if (Replace in b && true === b[Replace]) {
|
|
6
|
-
for (const key of mergeableKeys(b))result[key] = b[key];
|
|
7
|
-
return result;
|
|
8
|
-
}
|
|
9
|
-
for (const key of mergeableKeys(a))result[key] = a[key];
|
|
10
|
-
for (const key of mergeableKeys(b))result[key] = b[key];
|
|
11
|
-
return result;
|
|
12
|
-
}
|
|
13
|
-
export { shallowMerge };
|
|
1
|
+
export { };
|
|
@@ -224,9 +224,11 @@ export declare namespace FabricatorError {
|
|
|
224
224
|
key: string);
|
|
225
225
|
}
|
|
226
226
|
/**
|
|
227
|
-
* A `.refine()`-computed field
|
|
228
|
-
*
|
|
229
|
-
*
|
|
227
|
+
* A `.refine()`-computed field built or fabricated outside the object that
|
|
228
|
+
* owns it: placed in an array, tuple, or any other position, or a built
|
|
229
|
+
* field's `.fabricate()` called on its own. A computed field's value is
|
|
230
|
+
* derived from its sibling fields, so there is nothing to resolve against
|
|
231
|
+
* anywhere else.
|
|
230
232
|
*/
|
|
231
233
|
class DetachedComputeError extends FabricatorError {
|
|
232
234
|
constructor();
|
|
@@ -279,9 +281,9 @@ export declare namespace FabricatorError {
|
|
|
279
281
|
}
|
|
280
282
|
/**
|
|
281
283
|
* A key names a field the object schema does not define — most often a typo,
|
|
282
|
-
* which is why the known fields are listed. One failure reached from
|
|
283
|
-
* places: `.override()`, `T.pick`, `T.omit`, and a compute
|
|
284
|
-
* `.from([...])`, told apart by `operation`.
|
|
284
|
+
* which is why the known fields are listed. One failure reached from five
|
|
285
|
+
* places: `.override()`, `.rebase()`, `T.pick`, `T.omit`, and a compute
|
|
286
|
+
* field's `.from([...])`, told apart by `operation`.
|
|
285
287
|
*/
|
|
286
288
|
class UnknownFieldError extends FabricatorError {
|
|
287
289
|
/**
|
|
@@ -296,7 +298,7 @@ export declare namespace FabricatorError {
|
|
|
296
298
|
/**
|
|
297
299
|
* What named the unknown field, worded to read as the message's verb.
|
|
298
300
|
*/
|
|
299
|
-
readonly operation: "override" | "pick" | "omit" | "compute from";
|
|
301
|
+
readonly operation: "override" | "pick" | "omit" | "compute from" | "rebase";
|
|
300
302
|
constructor(
|
|
301
303
|
/**
|
|
302
304
|
* The name that was not found.
|
|
@@ -310,25 +312,32 @@ export declare namespace FabricatorError {
|
|
|
310
312
|
/**
|
|
311
313
|
* What named the unknown field, worded to read as the message's verb.
|
|
312
314
|
*/
|
|
313
|
-
operation: "override" | "pick" | "omit" | "compute from");
|
|
315
|
+
operation: "override" | "pick" | "omit" | "compute from" | "rebase");
|
|
314
316
|
}
|
|
315
317
|
/**
|
|
316
|
-
*
|
|
317
|
-
*
|
|
318
|
-
*
|
|
319
|
-
*
|
|
320
|
-
* never at fabrication.
|
|
318
|
+
* A `.refine()`-computed field placed in an object some other way: in
|
|
319
|
+
* `T.object(...)`, or passed to `.extend(...)` or `.rebase(...)`. A computed
|
|
320
|
+
* field reads the object as of the `.refine()` stage that created it, so it
|
|
321
|
+
* only exists inside that stage.
|
|
321
322
|
*/
|
|
322
|
-
class
|
|
323
|
+
class ComputeOutsideRefineError extends FabricatorError {
|
|
323
324
|
/**
|
|
324
|
-
* The
|
|
325
|
+
* The field the computed field was placed under.
|
|
325
326
|
*/
|
|
326
|
-
readonly
|
|
327
|
+
readonly field: string;
|
|
328
|
+
/**
|
|
329
|
+
* Where it was placed.
|
|
330
|
+
*/
|
|
331
|
+
readonly operation: "object" | "extend" | "rebase";
|
|
327
332
|
constructor(
|
|
328
333
|
/**
|
|
329
|
-
* The
|
|
334
|
+
* The field the computed field was placed under.
|
|
335
|
+
*/
|
|
336
|
+
field: string,
|
|
337
|
+
/**
|
|
338
|
+
* Where it was placed.
|
|
330
339
|
*/
|
|
331
|
-
|
|
340
|
+
operation: "object" | "extend" | "rebase");
|
|
332
341
|
}
|
|
333
342
|
/**
|
|
334
343
|
* An override value does not fit the field it is meant to replace. Raised for
|
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
import type { Stack } from "../Types.ts";
|
|
2
2
|
/**
|
|
3
3
|
* The asynchronous carrier, and the **only** module in this package importing
|
|
4
|
-
* anything from `node:`. Nothing imports it directly: `
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
*
|
|
4
|
+
* anything from `node:`. Nothing imports it directly: `src/index.ts` imports
|
|
5
|
+
* `#stack`, whose `node`/`bun` conditions (`package.json`) resolve here while
|
|
6
|
+
* `default` resolves to `./Sync.ts`. That is what keeps the package importable
|
|
7
|
+
* on a runtime with no `node:async_hooks` while every runtime that has one gets
|
|
8
|
+
* async-safe `wrap` with nothing to configure. Deno reaches this file both ways
|
|
9
|
+
* it can install the package: from npm through the `node` condition (built
|
|
10
|
+
* output, under `dist/`), and from JSR through `jsr.json`'s import map, which
|
|
11
|
+
* resolves `#stack` here when the package is published. A `deno` condition in
|
|
12
|
+
* `package.json` would serve both, but npm ships no `src/`.
|
|
9
13
|
*
|
|
10
14
|
* The store holds the whole open chain, not one `Frame`: `run` _replaces_ the
|
|
11
15
|
* store for the duration of `block`, so `enter` rebuilds the chain with the new
|
|
@@ -36,8 +36,8 @@ export type Fields = Record<string, BaseFabricator<unknown>>;
|
|
|
36
36
|
* built by `Constructor.ts` — which owns recursion into nested Schemas, so
|
|
37
37
|
* `object` never calls `construct()` and there's no import cycle with
|
|
38
38
|
* `Constructor.ts`. Schema-baked `.override()`-marked fields (`[Fixed]` in
|
|
39
|
-
* `Types.ts`) are already resolved into literal-valued
|
|
40
|
-
*
|
|
39
|
+
* `Types.ts`) are already resolved into literal-valued entries before they
|
|
40
|
+
* reach here. `fabricate`'s `overrides` (below) is a separate per-call
|
|
41
41
|
* mechanism; the two compose.
|
|
42
42
|
*
|
|
43
43
|
* `schema` is echoed as `.schema` unchanged — callers wanting a
|
|
@@ -45,20 +45,16 @@ export type Fields = Record<string, BaseFabricator<unknown>>;
|
|
|
45
45
|
* `Constructor.ts`'s `make` passes `object.rehydrate(...)` so a top-level built
|
|
46
46
|
* Fabricator's `.schema` still composes.
|
|
47
47
|
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
*
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* visible
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
* phases exactly like `fields`, so compute fields can read them, then deleted
|
|
60
|
-
* before returning. Per-call `overrides` only reach `fields` — `.override()`
|
|
61
|
-
* rejects a hidden key, and `{ validate: false }` must not let one through.
|
|
62
|
-
* `[Children]` stays `fields` alone, so enumeration never pins a hidden field.
|
|
48
|
+
* `stages` is `[Meta].stages` built (`Stages.ts`), and `fields` is each visible
|
|
49
|
+
* key's current version out of it, which is also `[Children]`, so enumeration
|
|
50
|
+
* never pins a hidden field or an earlier version. Two phases: every ordinary
|
|
51
|
+
* entry first, then the compute entries in stage order. A compute entry only
|
|
52
|
+
* reads earlier stages, so stage order is already dependency order. Each is
|
|
53
|
+
* handed exactly its `dependencies`, each at the version current before its
|
|
54
|
+
* stage, with an absent one left out. Hidden entries and earlier versions are
|
|
55
|
+
* fabricated like any other and never emitted. Per-call `overrides` reach only
|
|
56
|
+
* the visible current versions — `.override()` rejects a hidden key, and `{
|
|
57
|
+
* validate: false }` must not let one through — so a compute field reading an
|
|
58
|
+
* earlier version of an overridden key still reads that version.
|
|
63
59
|
*/
|
|
64
|
-
export declare function Fabricator<$Definition extends Definition>(context: FabricatorContext<Schema<$Definition>>, fields: Fields,
|
|
60
|
+
export declare function Fabricator<$Definition extends Definition>(context: FabricatorContext<Schema<$Definition>>, fields: Fields, stages: ReadonlyArray<Fields>): Fabricator<Schema<$Definition>>;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Adaptations } from "../../Adapter/Types.ts";
|
|
2
2
|
import { Adaptation, Meta } from "../../Types.ts";
|
|
3
3
|
import { Schema } from "./Schema.ts";
|
|
4
|
-
import type { Definition, Subsettable, Meta as ThisMeta } from "./Types.ts";
|
|
4
|
+
import type { Definition, PlainFields, Subsettable, Meta as ThisMeta } from "./Types.ts";
|
|
5
5
|
/**
|
|
6
6
|
* Rebuild a full `object` Schema — with `extend`/`refine` restored — from
|
|
7
7
|
* anything already reduced to this kind's `[Meta]` (a bare Schema, or a built
|
|
@@ -23,17 +23,15 @@ export declare function rehydrate<$Definition extends Definition, $Adaptations e
|
|
|
23
23
|
* Shared body of `T.pick`/`T.omit` (`pick/Registry.ts`, `omit/Registry.ts`),
|
|
24
24
|
* kept here because it rebuilds through `make`. A pure Schema → Schema
|
|
25
25
|
* transform, like `.override()`: the result is an ordinary `object` Schema over
|
|
26
|
-
* the kept fields, in source order.
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* entirely.
|
|
26
|
+
* the kept fields, in source order. `make`'s `prune` keeps every version a kept
|
|
27
|
+
* compute field reads, hidden — fabricated, so the resolver can read it, but
|
|
28
|
+
* never emitted — and drops everything else.
|
|
30
29
|
*
|
|
31
30
|
* The source's own `produce` (`.as(...)`) and whole-object `[Adaptation]` are
|
|
32
31
|
* not carried: both describe the source's full shape, not this one. Field
|
|
33
32
|
* entries are reused as they are, so a kept or hidden field's own adaptation
|
|
34
|
-
* and `[Fixed]` override come along.
|
|
35
|
-
* fields.
|
|
33
|
+
* and `[Fixed]` override come along.
|
|
36
34
|
*/
|
|
37
35
|
export declare function subset(source: Subsettable, keys: ReadonlyArray<string>, operation: "pick" | "omit"): Schema<any>;
|
|
38
|
-
declare const _default: <const $Definition extends Definition
|
|
36
|
+
declare const _default: <const $Definition extends Definition & PlainFields<$Definition>>(definition: $Definition) => Schema<$Definition>;
|
|
39
37
|
export default _default;
|
|
@@ -5,18 +5,26 @@ import { type SatisfiesThis } from "../../Schema/Satisfies.ts";
|
|
|
5
5
|
import type { AnySchema } from "../../Schema/Types.ts";
|
|
6
6
|
import type { ShallowMerge } from "../../Utility/ShallowMerge.ts";
|
|
7
7
|
import type { Computer } from "./compute/Schema.ts";
|
|
8
|
-
import type { Core, Definition, Extender, Fabricated, Override, Refinement } from "./Types.ts";
|
|
8
|
+
import type { Core, Definition, Extender, Extension, Fabricated, Override, Rebase, Rebased, Refinement } from "./Types.ts";
|
|
9
9
|
export type Refiner<$Definition extends Definition, $Refinement extends Refinement<$Definition>> = (params: {
|
|
10
10
|
base: Schema<$Definition>;
|
|
11
11
|
compute: Computer<$Definition>;
|
|
12
12
|
}) => $Refinement;
|
|
13
13
|
export interface Schema<$Definition extends Definition = Definition, $Adaptations extends Adaptations = {}> extends Core<$Definition, $Adaptations> {
|
|
14
|
-
extend: <const $Extension extends Definition
|
|
14
|
+
extend: <const $Extension extends Definition & Extension<$Definition, $Extension>>(extender: Extender<$Definition, $Extension>) => Schema<ShallowMerge<$Definition, $Extension>, $Adaptations>;
|
|
15
15
|
refine: <const $Refinement extends Refinement<$Definition>>(refiner: Refiner<$Definition, $Refinement>) => Schema<ShallowMerge<$Definition, $Refinement>, $Adaptations>;
|
|
16
16
|
override: (override: Override<$Definition>) => Schema<$Definition, $Adaptations>;
|
|
17
|
+
/**
|
|
18
|
+
* Replace each named field's original version — the one every `.refine()`
|
|
19
|
+
* since read, or replaced from — with the given schema: a reverse override.
|
|
20
|
+
* `.override()` and `.extend()` reach only a field's current version, so this
|
|
21
|
+
* is how a finished schema's computed field gets a different input. See
|
|
22
|
+
* `Registry.ts`'s `rebase`.
|
|
23
|
+
*/
|
|
24
|
+
rebase: <const $Rebase extends Definition & Rebase<$Definition, $Rebase>>(fields: $Rebase) => Schema<Rebased<$Definition, $Rebase>, $Adaptations>;
|
|
17
25
|
/**
|
|
18
26
|
* Layer an opaque production on this schema's existing `[Meta]` — carrying
|
|
19
|
-
* `definition`/`
|
|
27
|
+
* `definition`/`stages` forward, not discarding them, so
|
|
20
28
|
* `extend`/`refine`/`override` keep working (they drop the custom producer
|
|
21
29
|
* again, falling back to definition-based fabrication) and a later `.as(...)`
|
|
22
30
|
* (or future validation of `produce`) still has the definition to check
|
|
@@ -42,10 +50,10 @@ export interface Schema<$Definition extends Definition = Definition, $Adaptation
|
|
|
42
50
|
/**
|
|
43
51
|
* Unlike every other kind, `adapt` is _not_ added here but by `Registry.ts`'s
|
|
44
52
|
* `make` alongside `extend`/`refine`/`override`: those three rebuild from
|
|
45
|
-
* `
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
53
|
+
* `stages` rather than deriving from this object, so the adaptation map has to
|
|
54
|
+
* be threaded through `make` itself or chaining one of them would drop it (see
|
|
55
|
+
* `make`'s `adapt`). `as` and `satisfies` are the methods this factory
|
|
56
|
+
* attaches, so they are what the parameter omits.
|
|
49
57
|
*/
|
|
50
58
|
export declare function Schema<$Definition extends Definition, $Adaptations extends Adaptations = {}>(schema: Omit<Schema<$Definition, $Adaptations>, "as" | "satisfies">): Schema<$Definition, $Adaptations>;
|
|
51
59
|
export declare function isObjectSchema(candidate: AnySchema): candidate is Schema;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { Fixed, Kind, Meta } from "../../Types.ts";
|
|
2
|
+
import type { Definition } from "./Types.ts";
|
|
3
|
+
/**
|
|
4
|
+
* An `object`'s fields by `.refine()` stage (`Types.ts`'s `Meta`): stage 0
|
|
5
|
+
* holds the `T.object(...)` fields, and each `.refine()` appends one holding
|
|
6
|
+
* its compute fields. A key has one entry per version, in the stage that
|
|
7
|
+
* created that version.
|
|
8
|
+
*/
|
|
9
|
+
export type Stages = ReadonlyArray<Definition>;
|
|
10
|
+
/**
|
|
11
|
+
* A stage entry, or the Fabricator built from one: all {@link readsOf} looks at.
|
|
12
|
+
*/
|
|
13
|
+
type Entry = {
|
|
14
|
+
readonly [Kind]: string;
|
|
15
|
+
readonly [Meta]?: unknown;
|
|
16
|
+
readonly [Fixed]?: unknown;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* The index of the latest stage before `before` with an entry for `key`, or
|
|
20
|
+
* `-1`. With `before` left out, that is the key's current version. A compute
|
|
21
|
+
* field in stage `i` reads each dependency from `stageOf(stages, key, i)`.
|
|
22
|
+
*/
|
|
23
|
+
export declare function stageOf(stages: ReadonlyArray<object>, key: string, before?: number): number;
|
|
24
|
+
/**
|
|
25
|
+
* The index of the earliest stage with an entry for `key`, or `-1`: the version
|
|
26
|
+
* every later one replaced, which `.rebase()` replaces.
|
|
27
|
+
*/
|
|
28
|
+
export declare function firstStageOf(stages: ReadonlyArray<object>, key: string): number;
|
|
29
|
+
/**
|
|
30
|
+
* Every key with an entry in `stages`: each of `order` (the visible keys), then
|
|
31
|
+
* each hidden one in stage order.
|
|
32
|
+
*/
|
|
33
|
+
export declare function stageKeys(stages: Stages, order: ReadonlyArray<string>): ReadonlyArray<string>;
|
|
34
|
+
/**
|
|
35
|
+
* The fields an entry reads: a compute field's `dependencies`
|
|
36
|
+
* (`compute/Types.ts`), unless `.override()` fixed its value, in which case its
|
|
37
|
+
* resolver never runs. Every other entry reads nothing.
|
|
38
|
+
*/
|
|
39
|
+
export declare function readsOf(entry: Entry): ReadonlyArray<string>;
|
|
40
|
+
/**
|
|
41
|
+
* The visible view `[Meta].definition` holds: each key of `order`, in that
|
|
42
|
+
* order, at its current version.
|
|
43
|
+
*/
|
|
44
|
+
export declare function toDefinition(stages: Stages, order: ReadonlyArray<string>): Definition;
|
|
45
|
+
/**
|
|
46
|
+
* `entries` written over `stages` where each key stands: a key that already has
|
|
47
|
+
* an entry replaces its current version in place, so every compute field
|
|
48
|
+
* reading that version reads the new entry. Any other key goes into the last
|
|
49
|
+
* stage, which no compute field reads from: a compute field only reads earlier
|
|
50
|
+
* stages, and later ones see it as current.
|
|
51
|
+
*/
|
|
52
|
+
export declare function withEntries(stages: Stages, entries: Definition): Stages;
|
|
53
|
+
/**
|
|
54
|
+
* `entries` written over each key's earliest version where it stands
|
|
55
|
+
* (`.rebase()`), so every compute field reading that version, including one
|
|
56
|
+
* that replaced the key, reads the new entry. Later versions are untouched.
|
|
57
|
+
* Every key must already have an entry.
|
|
58
|
+
*/
|
|
59
|
+
export declare function withOriginals(stages: Stages, entries: Definition): Stages;
|
|
60
|
+
/**
|
|
61
|
+
* Keep only the entries something reads: the current version of each key in
|
|
62
|
+
* `order`, and, transitively, the version each kept compute field reads of its
|
|
63
|
+
* dependencies. Every other entry and every empty stage is dropped. Dropping
|
|
64
|
+
* never changes what a kept compute field reads: the version it reads is the
|
|
65
|
+
* latest before its stage, and that one is kept.
|
|
66
|
+
*/
|
|
67
|
+
export declare function prune(stages: Stages, order: ReadonlyArray<string>): Stages;
|
|
68
|
+
export {};
|
|
@@ -4,6 +4,7 @@ import type { AnySchema, Fabricated as FabricatedBy } from "../../Schema/Types.t
|
|
|
4
4
|
import { Kind, Meta, type Adaptation, type Omitted, type Produced, type Produces } from "../../Types.ts";
|
|
5
5
|
import { type Pretty } from "../../Utility/Types.ts";
|
|
6
6
|
import type { Schema as ComputeSchema } from "./compute/Schema.ts";
|
|
7
|
+
import type { Stages } from "./Stages.ts";
|
|
7
8
|
export type Definition<$Schema = AnySchema> = {
|
|
8
9
|
[_ in string]: $Schema;
|
|
9
10
|
};
|
|
@@ -17,7 +18,95 @@ export type Refinement<$Definition extends Definition> = Definition<ComputeSchem
|
|
|
17
18
|
export type Extender<$Definition extends Definition, $Extension extends Definition> = (params: {
|
|
18
19
|
base: $Definition;
|
|
19
20
|
}) => $Extension;
|
|
20
|
-
|
|
21
|
+
/**
|
|
22
|
+
* What `T.object(...)`/`.extend(...)` expect in place of a compute entry, so
|
|
23
|
+
* the type error reads as the fix. `Registry.ts` throws
|
|
24
|
+
* `ComputeOutsideRefineError` for one that gets past the type.
|
|
25
|
+
*/
|
|
26
|
+
type ComputeOutsideRefine = "Add computed fields with `.refine(({ compute }) => ...)`";
|
|
27
|
+
/**
|
|
28
|
+
* Checked structurally, like `Override`: a compute `Schema` would bring
|
|
29
|
+
* `refine`'s contravariance with it.
|
|
30
|
+
*/
|
|
31
|
+
type IsCompute<$Entry> = $Entry extends {
|
|
32
|
+
[Kind]: "object.compute";
|
|
33
|
+
} ? true : false;
|
|
34
|
+
/**
|
|
35
|
+
* `T.object(...)`'s fields, each compute entry swapped for
|
|
36
|
+
* {@link ComputeOutsideRefine}: a compute field reads the object as of the
|
|
37
|
+
* `.refine()` stage that minted it, so it only enters through that stage.
|
|
38
|
+
*/
|
|
39
|
+
export type PlainFields<$Definition extends Definition> = {
|
|
40
|
+
[$K in keyof $Definition]: IsCompute<$Definition[$K]> extends true ? ComputeOutsideRefine : $Definition[$K];
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* What a compute entry's resolver is handed, or `{}` for any other entry: not
|
|
44
|
+
* `never`, whose `keyof` is every key.
|
|
45
|
+
*/
|
|
46
|
+
type ReaderInput<$Entry> = $Entry extends {
|
|
47
|
+
[Kind]: "object.compute";
|
|
48
|
+
[Meta]: {
|
|
49
|
+
resolve: (params: {
|
|
50
|
+
fabricated: infer $Input;
|
|
51
|
+
}) => any;
|
|
52
|
+
};
|
|
53
|
+
} ? $Input : {};
|
|
54
|
+
/**
|
|
55
|
+
* Every key some compute entry of `$Definition` reads, other than the entry at
|
|
56
|
+
* that key itself: one that replaced its key reads the previous version, which
|
|
57
|
+
* `.extend()` never touches.
|
|
58
|
+
*/
|
|
59
|
+
type ReadKeys<$Definition extends Definition> = {
|
|
60
|
+
[$K in keyof $Definition]: Exclude<keyof ReaderInput<$Definition[$K]>, $K>;
|
|
61
|
+
}[keyof $Definition];
|
|
62
|
+
/**
|
|
63
|
+
* What `.extend(...)` may return: {@link PlainFields}, and a field some compute
|
|
64
|
+
* entry reads keeps a type that reader can take. `.extend()` replaces a field
|
|
65
|
+
* where it stands, so a reader would otherwise be handed a value its resolver
|
|
66
|
+
* was not typed for. A field nothing reads may change type freely.
|
|
67
|
+
*/
|
|
68
|
+
export type Extension<$Definition extends Definition, $Extension extends Definition> = {
|
|
69
|
+
[$K in keyof $Extension]: IsCompute<$Extension[$K]> extends true ? ComputeOutsideRefine : $K extends ReadKeys<$Definition> & keyof $Definition ? [FabricatedBy<$Extension[$K]>] extends [FabricatedBy<$Definition[$K]>] ? $Extension[$K] : `"${$K & string}" is read by a computed field, so it must keep its type` : $Extension[$K];
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Whether every compute entry of `$Definition` reading `$Key` — its own entry
|
|
73
|
+
* included, which reads the version `.rebase()` replaces — can take `$Value`
|
|
74
|
+
* for it. The type does not say which version each one reads, so one reading a
|
|
75
|
+
* later version, whose type a `.refine()` changed, constrains it too.
|
|
76
|
+
*/
|
|
77
|
+
type ReadersAccept<$Definition extends Definition, $Key extends PropertyKey, $Value> = false extends {
|
|
78
|
+
[$R in keyof $Definition]: $Key extends keyof ReaderInput<$Definition[$R]> ? [$Value] extends [ReaderInput<$Definition[$R]>[$Key]] ? true : false : true;
|
|
79
|
+
}[keyof $Definition] ? false : true;
|
|
80
|
+
/**
|
|
81
|
+
* Every key some compute entry of `$Definition` reads, its own key included.
|
|
82
|
+
* Unlike {@link ReadKeys}, this reaches a hidden key a visible compute entry
|
|
83
|
+
* reads, which `$Definition` itself no longer names.
|
|
84
|
+
*/
|
|
85
|
+
type InputKeys<$Definition extends Definition> = {
|
|
86
|
+
[$K in keyof $Definition]: keyof ReaderInput<$Definition[$K]>;
|
|
87
|
+
}[keyof $Definition];
|
|
88
|
+
/**
|
|
89
|
+
* What `.rebase(...)` may take: {@link PlainFields} naming only visible keys and
|
|
90
|
+
* keys a visible compute entry reads (hidden ones included), each of a type
|
|
91
|
+
* every compute entry reading that key can take.
|
|
92
|
+
*/
|
|
93
|
+
export type Rebase<$Definition extends Definition, $Rebase extends Definition> = {
|
|
94
|
+
[$K in keyof $Rebase]: IsCompute<$Rebase[$K]> extends true ? ComputeOutsideRefine : $K extends keyof $Definition | InputKeys<$Definition> ? ReadersAccept<$Definition, $K, FabricatedBy<$Rebase[$K]>> extends true ? $Rebase[$K] : `"${$K & string}" is read by a computed field, so it must keep its type` : `"${$K & string}" is not a field of this object`;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* The definition `.rebase(...)` produces. It replaces each named key's earliest
|
|
98
|
+
* version, so the visible entry changes only when that was the current one:
|
|
99
|
+
*
|
|
100
|
+
* - not a compute entry: no `.refine()` replaced it, so it was the only version,
|
|
101
|
+
* and the rebased entry takes its place;
|
|
102
|
+
* - a compute entry reading its own key: it replaced an earlier version it still
|
|
103
|
+
* reads, which is the one rebased, so it stays;
|
|
104
|
+
* - any other compute entry: whether an earlier version survived depends on what
|
|
105
|
+
* else read it, which the type cannot see, so it is either.
|
|
106
|
+
*/
|
|
107
|
+
export type Rebased<$Definition extends Definition, $Rebase extends Definition> = {
|
|
108
|
+
[$K in keyof $Definition]: $K extends keyof $Rebase ? IsCompute<$Definition[$K]> extends true ? $K extends keyof ReaderInput<$Definition[$K]> ? $Definition[$K] : $Definition[$K] | $Rebase[$K] : $Rebase[$K] : $Definition[$K];
|
|
109
|
+
};
|
|
21
110
|
/**
|
|
22
111
|
* Every `object.omittable`- or `object.optional`-kind field's key, so
|
|
23
112
|
* `Fabricated` can mark exactly those `?:` instead of required — matching
|
|
@@ -62,25 +151,30 @@ export type Override<$Definition extends Definition> = Pretty<{
|
|
|
62
151
|
} ? FabricatedBy<$Definition[$K]> | typeof Omitted : FabricatedBy<$Definition[$K]>;
|
|
63
152
|
}>;
|
|
64
153
|
/**
|
|
65
|
-
* `definition`/`
|
|
66
|
-
*
|
|
154
|
+
* `definition`/`stages` stay required regardless of `produce` — an opaque `as`
|
|
155
|
+
* production layers on top rather than replacing them, so
|
|
67
156
|
* `extend`/`refine`/`override` and `ToTypeBox`'s structural derivation keep
|
|
68
157
|
* working, and a prior definition survives `as` for future validation of
|
|
69
158
|
* `produce`.
|
|
70
159
|
*
|
|
71
|
-
* `
|
|
72
|
-
* of a
|
|
73
|
-
* `
|
|
74
|
-
*
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
*
|
|
160
|
+
* `stages` (`Stages.ts`) is every field entry by `.refine()` stage, one entry
|
|
161
|
+
* per version of a key: stage 0 holds the `T.object(...)` fields, and each
|
|
162
|
+
* `.refine()` appends a stage holding its compute fields. A compute field reads
|
|
163
|
+
* each dependency from the latest stage before its own. `definition` is the
|
|
164
|
+
* visible view derived from it in `Registry.ts`'s `make`: each visible key at
|
|
165
|
+
* its current version. A key `stages` has and `definition` does not is hidden,
|
|
166
|
+
* fabricated because a compute field reads it but never emitted: a dependency
|
|
167
|
+
* `T.pick`/`T.omit` dropped, or an earlier version of a key a later `.refine()`
|
|
168
|
+
* replaced. Everything that reads `definition` — the `Fabricated`/`Override`
|
|
169
|
+
* types, `.override()`'s key check, `[Children]` (and so enumeration), and
|
|
170
|
+
* every adapter — never sees one. Only `Constructor.ts`'s `make`,
|
|
171
|
+
* `Fabricator.ts`'s `fabricate`, `recursive/Terminate.ts`, and
|
|
172
|
+
* `Unique/Capacity.ts` read `stages`.
|
|
78
173
|
*/
|
|
79
174
|
export type Meta<$Definition extends Definition = Definition> = {
|
|
80
175
|
definition: $Definition;
|
|
81
|
-
|
|
176
|
+
stages: Stages;
|
|
82
177
|
produce?: Produce<Fabricated<$Definition>>;
|
|
83
|
-
hidden?: Definition;
|
|
84
178
|
};
|
|
85
179
|
/**
|
|
86
180
|
* What `T.pick`/`T.omit` accept: an `object` Schema or a built `object`
|
|
@@ -19,13 +19,12 @@ export declare function isObjectComputeFabricator(candidate: BaseFabricator<unkn
|
|
|
19
19
|
/**
|
|
20
20
|
* Turn an `object.compute` Schema into a live Fabricator. Unlike every other
|
|
21
21
|
* kind, there's nothing of `source` to build here — a computed field's value
|
|
22
|
-
* comes entirely from `resolve`, given the
|
|
23
|
-
*
|
|
24
|
-
*
|
|
22
|
+
* comes entirely from `resolve`, given the fields it reads. Only an `object`'s
|
|
23
|
+
* stage entries are built this way (`Constructor.ts`'s `buildField`); anywhere
|
|
24
|
+
* else is `DetachedComputeError` at build time, and so is `.fabricate()`
|
|
25
|
+
* without the fields.
|
|
25
26
|
*
|
|
26
|
-
*
|
|
27
|
-
*
|
|
28
|
-
* present, so an omitted dependency stays absent rather than becoming
|
|
29
|
-
* `undefined`. Narrowing here keeps `object/Fabricator.ts` unaware of it.
|
|
27
|
+
* `object/Fabricator.ts` hands over exactly `[Meta].dependencies`, each at the
|
|
28
|
+
* version this field reads, so nothing is narrowed here.
|
|
30
29
|
*/
|
|
31
30
|
export declare function Fabricator<$Fabricated extends PlainObject, $Source extends Source>(context: FabricatorContext<Schema<$Fabricated, $Source>>): Fabricator<$Fabricated, $Source>;
|
|
@@ -41,12 +41,12 @@ export type Resolved<$Source, $Bindings extends unknown[] = []> = FabricatedBy<D
|
|
|
41
41
|
* is handed.
|
|
42
42
|
*
|
|
43
43
|
* `dependencies` is always present, so every compute field has a known set of
|
|
44
|
-
* fields it reads: the keys named by `.from([...])`, or, without one, every
|
|
45
|
-
* of the `.refine()` stage's base — exactly what the resolver's
|
|
46
|
-
* read.
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* `
|
|
44
|
+
* fields it reads: the keys named by `.from([...])`, or, without one, every
|
|
45
|
+
* visible key of the `.refine()` stage's base — exactly what the resolver's
|
|
46
|
+
* type lets it read. The enclosing object hands the resolver exactly those
|
|
47
|
+
* fields, each at the version current before this field's stage
|
|
48
|
+
* (`object/Stages.ts`), and leaves out an omitted one rather than passing
|
|
49
|
+
* `undefined`. `T.pick`/`T.omit` keep exactly those versions fabricated.
|
|
50
50
|
*/
|
|
51
51
|
export type Meta<$Fabricated, $Source extends Source> = {
|
|
52
52
|
source: $Source;
|
|
@@ -54,7 +54,6 @@ export type Meta<$Fabricated, $Source extends Source> = {
|
|
|
54
54
|
fabricated: $Fabricated;
|
|
55
55
|
}) => Resolved<$Source>;
|
|
56
56
|
dependencies: ReadonlyArray<string>;
|
|
57
|
-
narrowed: boolean;
|
|
58
57
|
};
|
|
59
58
|
export interface Core<$Fabricated, $Source extends Source, $Adaptations extends Adaptations = {}> {
|
|
60
59
|
[Kind]: "object.compute";
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { Replace } from "../Types.ts";
|
|
2
|
-
import type {
|
|
2
|
+
import type { Pretty } from "./Types.ts";
|
|
3
|
+
/**
|
|
4
|
+
* The definition an `object` schema's `.extend()`/`.refine()` produces: each
|
|
5
|
+
* field of `$B` replaces the one of the same key in `$A` wholesale. Type only:
|
|
6
|
+
* at runtime that is `object/Stages.ts`.
|
|
7
|
+
*/
|
|
3
8
|
export type ShallowMerge<$A, $B> = $B extends {
|
|
4
9
|
[Replace]: true;
|
|
5
10
|
} ? Pretty<Omit<$B, typeof Replace>> : Pretty<Omit<$A, keyof $B> & Omit<$B, typeof Replace>>;
|
|
6
|
-
export declare function shallowMerge<$A extends PlainObject, $B extends PlainObject>(a: $A, b: $B): ShallowMerge<$A, $B>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ghostry/fabricator",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.16",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Fabricate typed data from composable schemas.",
|
|
6
6
|
"keywords": [
|
|
@@ -32,7 +32,6 @@
|
|
|
32
32
|
"imports": {
|
|
33
33
|
"#stack": {
|
|
34
34
|
"types": "./src/Instance/Stack/Async.ts",
|
|
35
|
-
"deno": "./src/Instance/Stack/Async.ts",
|
|
36
35
|
"node": "./dist/esm/Instance/Stack/Async.js",
|
|
37
36
|
"bun": "./dist/esm/Instance/Stack/Async.js",
|
|
38
37
|
"default": "./dist/esm/Instance/Stack/Sync.js"
|