viell-effect-schema-http 0.5.0 → 0.6.1
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/README.md +7 -6
- package/dist/http/Request.d.ts +33 -5
- package/dist/http/Request.d.ts.map +1 -1
- package/dist/http/Request.js +134 -90
- package/dist/http/Request.js.map +1 -1
- package/dist/http/internal/group.d.ts +10 -0
- package/dist/http/internal/group.d.ts.map +1 -0
- package/dist/http/internal/group.js +38 -0
- package/dist/http/internal/group.js.map +1 -0
- package/dist/http/internal/input.d.ts +7 -1
- package/dist/http/internal/input.d.ts.map +1 -1
- package/dist/http/internal/input.js +82 -2
- package/dist/http/internal/input.js.map +1 -1
- package/dist/http/internal/shape.d.ts +7 -1
- package/dist/http/internal/shape.d.ts.map +1 -1
- package/dist/http/internal/shape.js +40 -2
- package/dist/http/internal/shape.js.map +1 -1
- package/dist/http/internal/snapshot.d.ts +12 -0
- package/dist/http/internal/snapshot.d.ts.map +1 -0
- package/dist/http/internal/snapshot.js +179 -0
- package/dist/http/internal/snapshot.js.map +1 -0
- package/package.json +1 -1
- package/src/http/Request.ts +81 -19
- package/src/http/internal/group.ts +60 -0
- package/src/http/internal/input.ts +111 -2
- package/src/http/internal/shape.ts +70 -4
- package/src/http/internal/snapshot.ts +208 -0
package/src/http/Request.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
import type * as Body from "./Body.js";
|
|
9
9
|
import * as Fields from "./internal/fields.js";
|
|
10
10
|
import * as Inputs from "./internal/input.js";
|
|
11
|
+
import * as Groups from "./internal/group.js";
|
|
11
12
|
import * as Shapes from "./internal/shape.js";
|
|
12
13
|
import * as Transport from "./Transport.js";
|
|
13
14
|
import * as UrlEncoding from "./UrlEncoding.js";
|
|
@@ -94,6 +95,12 @@ export type QueryFields = Readonly<
|
|
|
94
95
|
* header or query group, a field a schema stacked with the union takes is not lost. Inside text given
|
|
95
96
|
* as text, a `Schema.Map`, a `Schema.Option` or a declaration with no struct behind it, the schema's
|
|
96
97
|
* own reading applies.
|
|
98
|
+
*
|
|
99
|
+
* A direct closed struct can recover an otherwise-refused nested field through constructor-only
|
|
100
|
+
* validation beside an opaque declaration. The original parent and output remain strictly
|
|
101
|
+
* validated, and complete preservation is required. Already successful calls keep their original
|
|
102
|
+
* path; new attempts can repeat effects or fail. Encoded-side recovery and unstable field evidence
|
|
103
|
+
* remain outside this bounded route.
|
|
97
104
|
*/
|
|
98
105
|
export type Input<S extends Schema.Top> = Inputs.Input<S>;
|
|
99
106
|
|
|
@@ -182,7 +189,9 @@ export type Shape = Shapes.Shape;
|
|
|
182
189
|
* @remarks
|
|
183
190
|
* This is a low-level extension type. A field a union would lose is not lost if another schema in
|
|
184
191
|
* the group took it, and `settled` is never an answer to that: a schema can allow a name it did not
|
|
185
|
-
* take.
|
|
192
|
+
* take. Optional `certificates` records actual claims per applied binding, allowing a union to
|
|
193
|
+
* replace its claim without removing another binding's evidence. Custom drafts with only `fields`
|
|
194
|
+
* and `settled` keep the earlier behavior and skip union recovery.
|
|
186
195
|
*/
|
|
187
196
|
export type Taken = Shapes.Taken;
|
|
188
197
|
|
|
@@ -821,8 +830,11 @@ type GroupInput<K extends "query" | "headers", A> =
|
|
|
821
830
|
* In a direct struct, schema preparation can supply an omitted optional field. A raw edit after
|
|
822
831
|
* that binding is an {@link InvalidRequest} when successful preparation writes an own enumerable
|
|
823
832
|
* data property with a non-`undefined` value under that name in a non-iterable record. This reads
|
|
824
|
-
* the preparation's result without repeating schema work.
|
|
825
|
-
*
|
|
833
|
+
* the preparation's result without repeating schema work. A transformed group also qualifies when
|
|
834
|
+
* its decoded and encoded endpoints are closed structs and the ordinary or null-prototype caller
|
|
835
|
+
* object has no own keys of any kind and none of either endpoint's names exists on its prototype
|
|
836
|
+
* chain. Only an edited, declared encoded string name is checked. Nonempty transformed groups,
|
|
837
|
+
* classes, unions and iterable output remain outside this proof.
|
|
826
838
|
*
|
|
827
839
|
* The input is an {@link Input} of the schema. Encoding produces ordered header lines. The schema
|
|
828
840
|
* states the shape of the group, whatever the order of the calls: once any schema is bound, every
|
|
@@ -841,6 +853,13 @@ type GroupInput<K extends "query" | "headers", A> =
|
|
|
841
853
|
* one of them takes stays with it, whatever a union beside it leaves out. Use {@link unbind} to
|
|
842
854
|
* start again.
|
|
843
855
|
*
|
|
856
|
+
* If a bare anyOf union would otherwise refuse uncovered root fields, it can retry the whole union
|
|
857
|
+
* with each member first, using the complete original input. Recovery needs actual other-binding
|
|
858
|
+
* claims for remaining losses and preserves every previously certified name. Parent metadata,
|
|
859
|
+
* transformed, open or iterable members, nested losses and joint backtracking are excluded. Existing
|
|
860
|
+
* successful reads do not retry. Recovery adds at most one whole-union read per other member; those
|
|
861
|
+
* attempts can repeat schema effects and can fail.
|
|
862
|
+
*
|
|
844
863
|
* A field the schema produces wins over an automatic `Accept`, `Content-Type` or `Content-Length`,
|
|
845
864
|
* whether the schema is bound before or after the body, and so does a default. An optional field
|
|
846
865
|
* left out with no default declares nothing, so the automatic value still applies.
|
|
@@ -855,6 +874,7 @@ export const headers = <
|
|
|
855
874
|
contract: S,
|
|
856
875
|
) => {
|
|
857
876
|
const read = Inputs.reader(contract, Schema.encodeEffect(contract));
|
|
877
|
+
const recover = Groups.recovery(contract);
|
|
858
878
|
const { shape, settle, taken, candidates, filled } = Shapes.bind(
|
|
859
879
|
contract,
|
|
860
880
|
foldHeader,
|
|
@@ -865,8 +885,9 @@ export const headers = <
|
|
|
865
885
|
I & GroupInput<"headers", Input<S>>,
|
|
866
886
|
E | Schema.SchemaError | InvalidRequest,
|
|
867
887
|
R | S["EncodingServices"] | S["DecodingServices"]
|
|
868
|
-
> =>
|
|
869
|
-
|
|
888
|
+
> => {
|
|
889
|
+
const token = {};
|
|
890
|
+
return construct(
|
|
870
891
|
{
|
|
871
892
|
...self.declaration,
|
|
872
893
|
headerShapes: [...self.declaration.headerShapes, shape],
|
|
@@ -881,24 +902,37 @@ export const headers = <
|
|
|
881
902
|
input.headers === undefined ? {} : input.headers
|
|
882
903
|
) as Input<S>;
|
|
883
904
|
const edited = candidates(value, draft.editedFields.get(shape));
|
|
884
|
-
|
|
885
|
-
|
|
905
|
+
let { output, lost: lostFromInput } = yield* read(value);
|
|
906
|
+
let conflicts = filled(edited, output);
|
|
886
907
|
const own = draft.takenHeaders && taken(value, output, lostFromInput);
|
|
887
|
-
|
|
908
|
+
let prepared = yield* self.prepare(
|
|
888
909
|
input,
|
|
889
910
|
own === undefined
|
|
890
911
|
? draft
|
|
891
912
|
: {
|
|
892
913
|
...draft,
|
|
893
|
-
takenHeaders: Shapes.taking(draft.takenHeaders, own),
|
|
914
|
+
takenHeaders: Shapes.taking(draft.takenHeaders, own, token),
|
|
894
915
|
},
|
|
895
916
|
);
|
|
896
|
-
if (lostFromInput.length > 0)
|
|
917
|
+
if (lostFromInput.length > 0) {
|
|
918
|
+
const recovered = yield* recover(
|
|
919
|
+
value,
|
|
920
|
+
{ output, lost: lostFromInput },
|
|
921
|
+
prepared.takenHeaders,
|
|
922
|
+
token,
|
|
923
|
+
taken,
|
|
924
|
+
);
|
|
925
|
+
if (recovered !== undefined) {
|
|
926
|
+
({ output, lost: lostFromInput } = recovered.reading);
|
|
927
|
+
conflicts = filled(edited, output);
|
|
928
|
+
prepared = { ...prepared, takenHeaders: recovered.taken };
|
|
929
|
+
}
|
|
897
930
|
yield* Inputs.refuseLost(
|
|
898
931
|
contract,
|
|
899
932
|
lostFromInput,
|
|
900
933
|
prepared.takenHeaders?.fields,
|
|
901
934
|
);
|
|
935
|
+
}
|
|
902
936
|
if (conflicts.length > 0)
|
|
903
937
|
return yield* preparationConflict("headers", conflicts);
|
|
904
938
|
const names = Shapes.written(output, foldHeader);
|
|
@@ -947,6 +981,7 @@ export const headers = <
|
|
|
947
981
|
};
|
|
948
982
|
}),
|
|
949
983
|
);
|
|
984
|
+
};
|
|
950
985
|
};
|
|
951
986
|
|
|
952
987
|
/**
|
|
@@ -973,8 +1008,11 @@ export const headers = <
|
|
|
973
1008
|
* In a direct struct, schema preparation can supply an omitted optional field. A raw edit after
|
|
974
1009
|
* that binding is an {@link InvalidRequest} when successful preparation writes an own enumerable
|
|
975
1010
|
* data property with a non-`undefined` value under that name in a non-iterable record. This reads
|
|
976
|
-
* the preparation's result without repeating schema work.
|
|
977
|
-
*
|
|
1011
|
+
* the preparation's result without repeating schema work. A transformed group also qualifies when
|
|
1012
|
+
* its decoded and encoded endpoints are closed structs and the ordinary or null-prototype caller
|
|
1013
|
+
* object has no own keys of any kind and none of either endpoint's names exists on its prototype
|
|
1014
|
+
* chain. Only an edited, declared encoded string name is checked. Nonempty transformed groups,
|
|
1015
|
+
* classes, unions and iterable output remain outside this proof.
|
|
978
1016
|
*
|
|
979
1017
|
* The input is an {@link Input} of the schema. The schema encodes to `UrlEncoding.QueryInput` and
|
|
980
1018
|
* states the shape of the group, whatever the order of the calls: once any schema is bound, every
|
|
@@ -998,6 +1036,13 @@ export const headers = <
|
|
|
998
1036
|
* Query schemas stack: a second one adds its fields, and together they state the shape. A field one
|
|
999
1037
|
* of them takes stays with it, whatever a union beside it leaves out. Use {@link unbind} to start
|
|
1000
1038
|
* again, and {@link mapInput} to supply a required field's value.
|
|
1039
|
+
*
|
|
1040
|
+
* If a bare anyOf union would otherwise refuse uncovered root fields, it can retry the whole union
|
|
1041
|
+
* with each member first, using the complete original input. Recovery needs actual other-binding
|
|
1042
|
+
* claims for remaining losses and preserves every previously certified name. Parent metadata,
|
|
1043
|
+
* transformed, open or iterable members, nested losses and joint backtracking are excluded. Existing
|
|
1044
|
+
* successful reads do not retry. Recovery adds at most one whole-union read per other member; those
|
|
1045
|
+
* attempts can repeat schema effects and can fail.
|
|
1001
1046
|
*/
|
|
1002
1047
|
export const query = <
|
|
1003
1048
|
S extends Schema.Top &
|
|
@@ -1006,6 +1051,7 @@ export const query = <
|
|
|
1006
1051
|
contract: S,
|
|
1007
1052
|
) => {
|
|
1008
1053
|
const read = Inputs.reader(contract, Schema.encodeEffect(contract));
|
|
1054
|
+
const recover = Groups.recovery(contract);
|
|
1009
1055
|
const { shape, settle, taken, candidates, filled } = Shapes.bind(
|
|
1010
1056
|
contract,
|
|
1011
1057
|
foldQuery,
|
|
@@ -1016,8 +1062,9 @@ export const query = <
|
|
|
1016
1062
|
I & GroupInput<"query", Input<S>>,
|
|
1017
1063
|
E | Schema.SchemaError | InvalidRequest,
|
|
1018
1064
|
R | S["EncodingServices"] | S["DecodingServices"]
|
|
1019
|
-
> =>
|
|
1020
|
-
|
|
1065
|
+
> => {
|
|
1066
|
+
const token = {};
|
|
1067
|
+
return construct(
|
|
1021
1068
|
{
|
|
1022
1069
|
...self.declaration,
|
|
1023
1070
|
queryShapes: [...self.declaration.queryShapes, shape],
|
|
@@ -1032,24 +1079,37 @@ export const query = <
|
|
|
1032
1079
|
input.query === undefined ? {} : input.query
|
|
1033
1080
|
) as Input<S>;
|
|
1034
1081
|
const edited = candidates(value, draft.editedFields.get(shape));
|
|
1035
|
-
|
|
1036
|
-
|
|
1082
|
+
let { output, lost: lostFromInput } = yield* read(value);
|
|
1083
|
+
let conflicts = filled(edited, output);
|
|
1037
1084
|
const own = draft.takenQuery && taken(value, output, lostFromInput);
|
|
1038
|
-
|
|
1085
|
+
let prepared = yield* self.prepare(
|
|
1039
1086
|
input,
|
|
1040
1087
|
own === undefined
|
|
1041
1088
|
? draft
|
|
1042
1089
|
: {
|
|
1043
1090
|
...draft,
|
|
1044
|
-
takenQuery: Shapes.taking(draft.takenQuery, own),
|
|
1091
|
+
takenQuery: Shapes.taking(draft.takenQuery, own, token),
|
|
1045
1092
|
},
|
|
1046
1093
|
);
|
|
1047
|
-
if (lostFromInput.length > 0)
|
|
1094
|
+
if (lostFromInput.length > 0) {
|
|
1095
|
+
const recovered = yield* recover(
|
|
1096
|
+
value,
|
|
1097
|
+
{ output, lost: lostFromInput },
|
|
1098
|
+
prepared.takenQuery,
|
|
1099
|
+
token,
|
|
1100
|
+
taken,
|
|
1101
|
+
);
|
|
1102
|
+
if (recovered !== undefined) {
|
|
1103
|
+
({ output, lost: lostFromInput } = recovered.reading);
|
|
1104
|
+
conflicts = filled(edited, output);
|
|
1105
|
+
prepared = { ...prepared, takenQuery: recovered.taken };
|
|
1106
|
+
}
|
|
1048
1107
|
yield* Inputs.refuseLost(
|
|
1049
1108
|
contract,
|
|
1050
1109
|
lostFromInput,
|
|
1051
1110
|
prepared.takenQuery?.fields,
|
|
1052
1111
|
);
|
|
1112
|
+
}
|
|
1053
1113
|
if (conflicts.length > 0)
|
|
1054
1114
|
return yield* preparationConflict("query", conflicts);
|
|
1055
1115
|
const names = Shapes.written(output, foldQuery);
|
|
@@ -1077,6 +1137,7 @@ export const query = <
|
|
|
1077
1137
|
);
|
|
1078
1138
|
}),
|
|
1079
1139
|
);
|
|
1140
|
+
};
|
|
1080
1141
|
};
|
|
1081
1142
|
|
|
1082
1143
|
/** A renderer: what to add to the path, from the encoded value and the path-value encoder. */
|
|
@@ -1526,6 +1587,7 @@ const takenRecord = (shapes: ReadonlyArray<Shape>): Taken | undefined =>
|
|
|
1526
1587
|
shapes.length > 1 && shapes.some(({ chooses }) => chooses)
|
|
1527
1588
|
? {
|
|
1528
1589
|
fields: new Set(),
|
|
1590
|
+
certificates: new Map(),
|
|
1529
1591
|
settled: new Set(
|
|
1530
1592
|
shapes.flatMap(({ chooses, names }) =>
|
|
1531
1593
|
chooses ? [] : Array.from(names),
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { Effect, type Schema } from "effect";
|
|
2
|
+
import type * as Excess from "./excess.js";
|
|
3
|
+
import * as Inputs from "./input.js";
|
|
4
|
+
import * as Shapes from "./shape.js";
|
|
5
|
+
|
|
6
|
+
/** Retry only an uncovered root loss, keeping the original obligations and all earlier claims. */
|
|
7
|
+
export const recovery = <S extends Schema.Top>(contract: S) => {
|
|
8
|
+
// Building every full permutation costs quadratic space; defer it until recovery is necessary.
|
|
9
|
+
let readers: ReturnType<typeof Inputs.reorderedReaders<S>> | undefined;
|
|
10
|
+
return Effect.fnUntraced(function* (
|
|
11
|
+
input: Inputs.Input<S>,
|
|
12
|
+
original: Inputs.Written<S["Encoded"]>,
|
|
13
|
+
taken: Shapes.Taken | undefined,
|
|
14
|
+
token: object,
|
|
15
|
+
claim: (
|
|
16
|
+
input: unknown,
|
|
17
|
+
output: object,
|
|
18
|
+
lost: ReadonlyArray<Excess.Path>,
|
|
19
|
+
) => ReadonlySet<PropertyKey>,
|
|
20
|
+
) {
|
|
21
|
+
if (
|
|
22
|
+
taken?.certificates === undefined ||
|
|
23
|
+
!taken.certificates.has(token) ||
|
|
24
|
+
typeof original.output !== "object" ||
|
|
25
|
+
original.output === null ||
|
|
26
|
+
Symbol.iterator in original.output ||
|
|
27
|
+
original.lost.some((path) => path.length !== 1) ||
|
|
28
|
+
original.lost.every(([name]) => taken.fields.has(name!))
|
|
29
|
+
)
|
|
30
|
+
return undefined;
|
|
31
|
+
const alternatives = (readers ??= Inputs.reorderedReaders(contract));
|
|
32
|
+
const elsewhere = Shapes.certifiedElsewhere(taken, token);
|
|
33
|
+
for (const read of alternatives) {
|
|
34
|
+
// Only typed schema failures permit the next attempt. Defects and interruption propagate.
|
|
35
|
+
const attempt = yield* Effect.result(read(input));
|
|
36
|
+
if (attempt._tag === "Failure") continue;
|
|
37
|
+
const candidate = attempt.success;
|
|
38
|
+
if (
|
|
39
|
+
typeof candidate.output !== "object" ||
|
|
40
|
+
candidate.output === null ||
|
|
41
|
+
Symbol.iterator in candidate.output ||
|
|
42
|
+
candidate.lost.some(
|
|
43
|
+
(path) => path.length !== 1 || !elsewhere.has(path[0]!),
|
|
44
|
+
)
|
|
45
|
+
)
|
|
46
|
+
continue;
|
|
47
|
+
const next = Shapes.retaking(
|
|
48
|
+
taken,
|
|
49
|
+
claim(input, candidate.output, candidate.lost),
|
|
50
|
+
token,
|
|
51
|
+
);
|
|
52
|
+
if (
|
|
53
|
+
next !== undefined &&
|
|
54
|
+
original.lost.every(([name]) => next.fields.has(name!))
|
|
55
|
+
)
|
|
56
|
+
return { reading: candidate, taken: next };
|
|
57
|
+
}
|
|
58
|
+
return undefined;
|
|
59
|
+
});
|
|
60
|
+
};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { Effect, Schema,
|
|
1
|
+
import { Effect, Schema, SchemaAST } from "effect";
|
|
2
2
|
import * as Excess from "./excess.js";
|
|
3
|
+
import { snapshotter } from "./snapshot.js";
|
|
3
4
|
|
|
4
5
|
/** What a schema's constructor takes. This is the one place that names the `~`-prefixed key. */
|
|
5
6
|
type ConstructorInput<S extends Schema.Top> = S["~type.make.in"];
|
|
@@ -178,6 +179,67 @@ export const reader = <S extends Schema.Top, A, R>(
|
|
|
178
179
|
}),
|
|
179
180
|
),
|
|
180
181
|
);
|
|
182
|
+
const ast = contract.ast;
|
|
183
|
+
const recoverable =
|
|
184
|
+
ast._tag === "Objects" &&
|
|
185
|
+
ast.encoding === undefined &&
|
|
186
|
+
ast.indexSignatures.length === 0 &&
|
|
187
|
+
!ast.propertySignatures.some(({ name }) => name === Symbol.iterator) &&
|
|
188
|
+
ast.propertySignatures.some(
|
|
189
|
+
({ type }) => type._tag === "Declaration" && type.encoding === undefined,
|
|
190
|
+
);
|
|
191
|
+
const snapshot = recoverable ? snapshotter([ast]) : undefined;
|
|
192
|
+
const recover = Effect.fnUntraced(function* (
|
|
193
|
+
input: unknown,
|
|
194
|
+
original: Written<A>,
|
|
195
|
+
) {
|
|
196
|
+
if (ast._tag !== "Objects") return original;
|
|
197
|
+
// Candidate callbacks receive the captured input too; keep their evidence independent of it.
|
|
198
|
+
const evidence = snapshot?.(input);
|
|
199
|
+
if (evidence === undefined) return original;
|
|
200
|
+
const given = Excess.normalised([ast], evidence.value);
|
|
201
|
+
const prototype: unknown = Object.getPrototypeOf(input);
|
|
202
|
+
const output = Object.create(prototype as object | null) as Record<
|
|
203
|
+
PropertyKey,
|
|
204
|
+
unknown
|
|
205
|
+
>;
|
|
206
|
+
for (const name of Reflect.ownKeys(input as object)) {
|
|
207
|
+
const property = Object.getOwnPropertyDescriptor(input, name)!;
|
|
208
|
+
Object.defineProperty(output, name, {
|
|
209
|
+
value: property.value,
|
|
210
|
+
enumerable: property.enumerable === true,
|
|
211
|
+
writable: true,
|
|
212
|
+
configurable: true,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
for (const { name, type } of ast.propertySignatures) {
|
|
216
|
+
const value = output[name];
|
|
217
|
+
if (value === undefined) continue;
|
|
218
|
+
const field = Schema.make<Schema.Top>(type);
|
|
219
|
+
const made = yield* Effect.mapError(
|
|
220
|
+
field.makeEffect(
|
|
221
|
+
value,
|
|
222
|
+
type._tag === "Declaration" && type.encoding === undefined
|
|
223
|
+
? undefined
|
|
224
|
+
: { parseOptions: Excess.strict },
|
|
225
|
+
),
|
|
226
|
+
(issue) => new Schema.SchemaError(issue),
|
|
227
|
+
);
|
|
228
|
+
Object.defineProperty(output, name, {
|
|
229
|
+
value: made,
|
|
230
|
+
enumerable: true,
|
|
231
|
+
writable: true,
|
|
232
|
+
configurable: true,
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
const value = yield* Effect.mapError(
|
|
236
|
+
contract.makeEffect(output, { parseOptions: Excess.strict }),
|
|
237
|
+
(issue) => new Schema.SchemaError(issue),
|
|
238
|
+
);
|
|
239
|
+
const candidate = yield* write(value, Excess.strict);
|
|
240
|
+
const lost = Excess.missing([ast], given, value, candidate);
|
|
241
|
+
return lost.length === 0 ? { output: candidate, lost } : original;
|
|
242
|
+
});
|
|
181
243
|
return (
|
|
182
244
|
input: Input<S>,
|
|
183
245
|
): Effect.Effect<
|
|
@@ -185,20 +247,67 @@ export const reader = <S extends Schema.Top, A, R>(
|
|
|
185
247
|
Schema.SchemaError,
|
|
186
248
|
R | S["DecodingServices"]
|
|
187
249
|
> => {
|
|
250
|
+
const captured = snapshot?.(input);
|
|
188
251
|
const given = Excess.normalised([contract.ast], input);
|
|
189
252
|
// A strict read cannot see a field outside a value's keys, so it may not fall back on `input`.
|
|
190
253
|
const seen = (
|
|
191
254
|
given === input ? input : Excess.exposed([contract.ast], input)
|
|
192
255
|
) as Input<S>;
|
|
193
|
-
|
|
256
|
+
const original = Effect.catch(
|
|
194
257
|
given === input
|
|
195
258
|
? kept(input)
|
|
196
259
|
: Effect.catch(kept(given as Input<S>), () => kept(seen)),
|
|
197
260
|
() => compared(input, given),
|
|
198
261
|
);
|
|
262
|
+
if (captured === undefined) return original;
|
|
263
|
+
return Effect.flatMap(original, (result) =>
|
|
264
|
+
result.lost.some((path) => path.length > 1) && captured.matches(input)
|
|
265
|
+
? Effect.catch(recover(captured.value, result), () =>
|
|
266
|
+
Effect.succeed(result),
|
|
267
|
+
)
|
|
268
|
+
: Effect.succeed(result),
|
|
269
|
+
);
|
|
199
270
|
};
|
|
200
271
|
};
|
|
201
272
|
|
|
273
|
+
/**
|
|
274
|
+
* Whole-union alternatives for a bare anyOf group. Preserve every member and the whole input:
|
|
275
|
+
* trying one member alone would move its decoder ahead of another member's constructor.
|
|
276
|
+
* Parent behavior is deliberately excluded rather than copied or silently discarded.
|
|
277
|
+
*/
|
|
278
|
+
export const reorderedReaders = <S extends Schema.Top>(contract: S) => {
|
|
279
|
+
const ast = contract.ast;
|
|
280
|
+
if (
|
|
281
|
+
ast._tag !== "Union" ||
|
|
282
|
+
(ast.options?.mode ?? "anyOf") !== "anyOf" ||
|
|
283
|
+
ast.encoding !== undefined ||
|
|
284
|
+
ast.context !== undefined ||
|
|
285
|
+
ast.checks !== undefined ||
|
|
286
|
+
ast.encodingChecks !== undefined ||
|
|
287
|
+
(ast.annotations !== undefined &&
|
|
288
|
+
Reflect.ownKeys(ast.annotations).length > 0) ||
|
|
289
|
+
!ast.types.every(
|
|
290
|
+
(member) =>
|
|
291
|
+
member._tag === "Objects" &&
|
|
292
|
+
member.encoding === undefined &&
|
|
293
|
+
member.indexSignatures.length === 0 &&
|
|
294
|
+
!member.propertySignatures.some(({ name }) => name === Symbol.iterator),
|
|
295
|
+
)
|
|
296
|
+
)
|
|
297
|
+
return [];
|
|
298
|
+
// The unchanged order already supplied `original`; repeating it adds no selection alternative.
|
|
299
|
+
return ast.types.slice(1).map((member, offset) => {
|
|
300
|
+
const index = offset + 1;
|
|
301
|
+
const reordered = Schema.make<S>(
|
|
302
|
+
new SchemaAST.Union(
|
|
303
|
+
[member, ...ast.types.filter((_, position) => position !== index)],
|
|
304
|
+
ast.options,
|
|
305
|
+
),
|
|
306
|
+
);
|
|
307
|
+
return reader(reordered, Schema.encodeEffect(reordered));
|
|
308
|
+
});
|
|
309
|
+
};
|
|
310
|
+
|
|
202
311
|
/**
|
|
203
312
|
* Refuse the fields a schema would lose, unless another schema in the group took them. Only a field
|
|
204
313
|
* of the group itself can be taken, so one further down a value stays lost.
|
|
@@ -20,6 +20,8 @@ export interface Taken {
|
|
|
20
20
|
readonly fields: ReadonlySet<PropertyKey>;
|
|
21
21
|
/** The compared names schemas with no union allow. Each settles a raw value under its names. */
|
|
22
22
|
readonly settled: ReadonlySet<string>;
|
|
23
|
+
/** Actual claims per applied binding; absent on legacy custom drafts, which cannot recover. */
|
|
24
|
+
readonly certificates?: ReadonlyMap<object, ReadonlySet<PropertyKey>>;
|
|
23
25
|
}
|
|
24
26
|
|
|
25
27
|
/** What a schema's encoded side states, in the schema's own spelling. */
|
|
@@ -253,11 +255,44 @@ export const others = (
|
|
|
253
255
|
export const taking = (
|
|
254
256
|
taken: Taken | undefined,
|
|
255
257
|
own: ReadonlySet<PropertyKey>,
|
|
258
|
+
token: object,
|
|
256
259
|
): Taken => ({
|
|
257
260
|
fields: new Set([...(taken?.fields ?? []), ...own]),
|
|
258
261
|
settled: taken?.settled ?? new Set(),
|
|
262
|
+
...(taken?.certificates === undefined
|
|
263
|
+
? {}
|
|
264
|
+
: { certificates: new Map([...taken.certificates, [token, own]]) }),
|
|
259
265
|
});
|
|
260
266
|
|
|
267
|
+
/** Replace one binding's claim only if no previously certified name disappears. */
|
|
268
|
+
export const retaking = (
|
|
269
|
+
taken: Taken,
|
|
270
|
+
own: ReadonlySet<PropertyKey>,
|
|
271
|
+
token: object,
|
|
272
|
+
): Taken | undefined => {
|
|
273
|
+
if (taken.certificates === undefined || !taken.certificates.has(token))
|
|
274
|
+
return undefined;
|
|
275
|
+
const certificates = new Map(taken.certificates);
|
|
276
|
+
certificates.set(token, own);
|
|
277
|
+
const fields = new Set(
|
|
278
|
+
Array.from(certificates.values()).flatMap((set) => [...set]),
|
|
279
|
+
);
|
|
280
|
+
return Array.from(taken.fields).every((name) => fields.has(name))
|
|
281
|
+
? { ...taken, fields, certificates }
|
|
282
|
+
: undefined;
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
/** Names an actual other binding took, never merely names it permits. */
|
|
286
|
+
export const certifiedElsewhere = (
|
|
287
|
+
taken: Taken,
|
|
288
|
+
token: object,
|
|
289
|
+
): ReadonlySet<PropertyKey> =>
|
|
290
|
+
new Set(
|
|
291
|
+
Array.from(taken.certificates ?? []).flatMap(([binding, fields]) =>
|
|
292
|
+
binding === token ? [] : [...fields],
|
|
293
|
+
),
|
|
294
|
+
);
|
|
295
|
+
|
|
261
296
|
/**
|
|
262
297
|
* What went into `settle` and is gone from what came out, in the spelling it went in with. A schema
|
|
263
298
|
* can write less than it was given, and that must not pass as a group that was built.
|
|
@@ -275,7 +310,7 @@ export const lost = (
|
|
|
275
310
|
};
|
|
276
311
|
|
|
277
312
|
/** Only stable absent/data properties prove the caller did not supply this field. */
|
|
278
|
-
const omitted = (input: object, name:
|
|
313
|
+
const omitted = (input: object, name: PropertyKey): boolean => {
|
|
279
314
|
const property = Object.getOwnPropertyDescriptor(input, name);
|
|
280
315
|
return property === undefined
|
|
281
316
|
? !(name in input)
|
|
@@ -315,15 +350,46 @@ export const bind = <S extends Schema.Constraint>(contract: S, fold: Fold) => {
|
|
|
315
350
|
typeof name === "string" ? [name] : [],
|
|
316
351
|
)
|
|
317
352
|
: [];
|
|
353
|
+
const end = Excess.encodedEnd(contract.ast);
|
|
354
|
+
const closedEndpoints =
|
|
355
|
+
contract.ast._tag === "Objects" &&
|
|
356
|
+
contract.ast.indexSignatures.length === 0 &&
|
|
357
|
+
end._tag === "Objects" &&
|
|
358
|
+
end.indexSignatures.length === 0
|
|
359
|
+
? [contract.ast, end]
|
|
360
|
+
: [];
|
|
361
|
+
const encodedNames =
|
|
362
|
+
end._tag === "Objects"
|
|
363
|
+
? end.propertySignatures.flatMap(({ name }) =>
|
|
364
|
+
typeof name === "string" ? [name] : [],
|
|
365
|
+
)
|
|
366
|
+
: [];
|
|
367
|
+
/** No caller field can explain generated output when both closed endpoints are absent. */
|
|
368
|
+
const empty = (input: object): boolean => {
|
|
369
|
+
const prototype: unknown = Object.getPrototypeOf(input);
|
|
370
|
+
return (
|
|
371
|
+
closedEndpoints.length > 0 &&
|
|
372
|
+
(prototype === Object.prototype || prototype === null) &&
|
|
373
|
+
Reflect.ownKeys(input).length === 0 &&
|
|
374
|
+
closedEndpoints.every((endpoint) =>
|
|
375
|
+
endpoint.propertySignatures.every(
|
|
376
|
+
({ name }) => omitted(input, name) && !(name in input),
|
|
377
|
+
),
|
|
378
|
+
)
|
|
379
|
+
);
|
|
380
|
+
};
|
|
318
381
|
/** Snapshot caller omission before preparation can run a default or encoder. */
|
|
319
382
|
const candidates = (
|
|
320
383
|
input: unknown,
|
|
321
384
|
edits: ReadonlySet<string> | undefined,
|
|
322
385
|
): ReadonlyArray<string> =>
|
|
323
386
|
edits !== undefined && typeof input === "object" && input !== null
|
|
324
|
-
?
|
|
325
|
-
|
|
326
|
-
|
|
387
|
+
? (contract.ast.encoding === undefined
|
|
388
|
+
? directNames
|
|
389
|
+
: empty(input)
|
|
390
|
+
? encodedNames
|
|
391
|
+
: []
|
|
392
|
+
).filter((name) => edits.has(fold(name)) && omitted(input, name))
|
|
327
393
|
: [];
|
|
328
394
|
/** Preparation supplied these omitted, edited fields in the record it actually writes. */
|
|
329
395
|
const filled = (
|