viell-effect-schema-http 0.14.0 → 0.16.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/README.md +12 -30
- package/dist/http/Request.d.ts +17 -27
- package/dist/http/Request.d.ts.map +1 -1
- package/dist/http/Request.js +55 -68
- package/dist/http/Request.js.map +1 -1
- package/dist/http/UrlEncoding.d.ts +2 -1
- package/dist/http/UrlEncoding.d.ts.map +1 -1
- package/dist/http/UrlEncoding.js.map +1 -1
- package/dist/http/internal/encoded-input.d.ts.map +1 -1
- package/dist/http/internal/encoded-input.js +11 -0
- package/dist/http/internal/encoded-input.js.map +1 -1
- package/dist/http/internal/excess.d.ts.map +1 -1
- package/dist/http/internal/excess.js +86 -10
- package/dist/http/internal/excess.js.map +1 -1
- package/dist/http/internal/nested-input.d.ts +1 -1
- package/dist/http/internal/nested-input.d.ts.map +1 -1
- package/dist/http/internal/nested-input.js +48 -18
- package/dist/http/internal/nested-input.js.map +1 -1
- package/dist/http/internal/primitive.d.ts +4 -0
- package/dist/http/internal/primitive.d.ts.map +1 -0
- package/dist/http/internal/primitive.js +17 -0
- package/dist/http/internal/primitive.js.map +1 -0
- package/package.json +2 -2
- package/src/http/Request.ts +131 -141
- package/src/http/UrlEncoding.ts +2 -1
- package/src/http/internal/encoded-input.ts +10 -0
- package/src/http/internal/excess.ts +92 -12
- package/src/http/internal/nested-input.ts +66 -20
- package/src/http/internal/primitive.ts +20 -0
package/src/http/Request.ts
CHANGED
|
@@ -75,7 +75,8 @@ export type QueryFields = Readonly<
|
|
|
75
75
|
*
|
|
76
76
|
* Preparation tries the decoded side first, then the decoder as a whole, then the decoder field by
|
|
77
77
|
* field. If all fail, it reports the decoded side's error, a `SchemaError` before dispatch. Both
|
|
78
|
-
* routes validate
|
|
78
|
+
* routes validate the values they consume. Unsupported constructor-default paths below can still
|
|
79
|
+
* ignore a supplied alias. A weak schema also lets more through:
|
|
79
80
|
* `Schema.NumberFromString` turns `"many"` into `NaN`, and `Schema.FiniteFromString` refuses it.
|
|
80
81
|
*
|
|
81
82
|
* A value that fits the decoded side is read as decoded. Under a schema whose two sides share a
|
|
@@ -87,32 +88,21 @@ export type QueryFields = Readonly<
|
|
|
87
88
|
* encoded read uses the encoded name instead, including after the added whole-group validation.
|
|
88
89
|
* Conflicting values are not generally refused; give one side's name or validate a decoded mapping.
|
|
89
90
|
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
*
|
|
98
|
-
*
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
*
|
|
102
|
-
*
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
* routes. Successful initial whole construction also permits a nested proof through finite closed,
|
|
106
|
-
* untransformed objects, parent unions and dense array/tuple positions. Array indices/length must
|
|
107
|
-
* be stable own data with ordinary/null prototype and matching original strict-output length.
|
|
108
|
-
* Every declaring parent member must use the same field
|
|
109
|
-
* schema; stable own-data links must reach the same encoded-only field actually written by the
|
|
110
|
-
* original strict writer. Whole decoding can repeat checks/services, refuse valid mixed-side input
|
|
111
|
-
* or give shared-type fields their encoded meaning. Construction is not replayed. Distinct field
|
|
112
|
-
* declarations, sparse/accessor/inherited/subclass/custom-iterator/changing arrays, array-shaped
|
|
113
|
-
* parent union members, opaque/suspended/transformed parents and unsupported member/fieldwise paths
|
|
114
|
-
* remain outside this extension (issue #170). Use decoded names
|
|
115
|
-
* or a validated decoded mapping there. The Request guide describes the bounds and migration.
|
|
91
|
+
* Constructor-default validation is bounded: unsupported aliases can still be ignored in favor of
|
|
92
|
+
* defaults, including inherited-only cells, arbitrary iterators and opaque or ambiguous paths
|
|
93
|
+
* (issue #170). Stable sparse own cells and exact native own iterator data can qualify.
|
|
94
|
+
* Array normalization preserves positions and symbol identity but does not expand that proof.
|
|
95
|
+
* Supply one decoded shape, validated and mapped explicitly when needed, for unsupported inputs.
|
|
96
|
+
* Whole encoded reading can refuse mixed-side input or change a shared-type field's interpretation;
|
|
97
|
+
* schema checks and services can run more than once.
|
|
98
|
+
*
|
|
99
|
+
* A helper accepting `Input` must handle nested encoded alternatives too, or declare its own
|
|
100
|
+
* decoded-only input shape. Opaque values use their schema's own reading; supply decoded contents
|
|
101
|
+
* when constructor priority would otherwise lose an inner alias.
|
|
102
|
+
*
|
|
103
|
+
* See https://viell-dev.github.io/slop-effect-schema-http/schema-input for the complete current
|
|
104
|
+
* reading order, preservation/default boundaries and recovery.
|
|
105
|
+
* https://viell-dev.github.io/slop-effect-schema-http/schema-groups covers raw defaults and stacking.
|
|
116
106
|
*
|
|
117
107
|
* Under a union, a member that keeps every field wins over an earlier one that would strip one, on
|
|
118
108
|
* both sides and at any depth. A field no member can keep beside the others is a `SchemaError` that
|
|
@@ -845,6 +835,86 @@ type GroupInput<K extends "query" | "headers", A> =
|
|
|
845
835
|
? { readonly [P in K]?: A }
|
|
846
836
|
: { readonly [P in K]: A };
|
|
847
837
|
|
|
838
|
+
/** Read, recover and settle one binding before its adapter writes native header/query fields. */
|
|
839
|
+
const prepareGroup = <
|
|
840
|
+
S extends Schema.Top &
|
|
841
|
+
Schema.ConstraintCodec<unknown, object, unknown, unknown>,
|
|
842
|
+
>(
|
|
843
|
+
contract: S,
|
|
844
|
+
group: "headers" | "query",
|
|
845
|
+
fold: Fields.Fold,
|
|
846
|
+
) => {
|
|
847
|
+
const read = Inputs.reader(contract, Schema.encodeEffect(contract));
|
|
848
|
+
const recover = Groups.recovery(contract);
|
|
849
|
+
const { shape, settle, taken, candidates, filled } = Shapes.bind(
|
|
850
|
+
contract,
|
|
851
|
+
fold,
|
|
852
|
+
);
|
|
853
|
+
const takenKey = group === "headers" ? "takenHeaders" : "takenQuery";
|
|
854
|
+
const label = group === "headers" ? "header" : "query";
|
|
855
|
+
return {
|
|
856
|
+
shape,
|
|
857
|
+
// Delegate directly so extraction adds no Effect boundary between the original steps.
|
|
858
|
+
*evaluate<I, E, R>(
|
|
859
|
+
self: Request<I, E, R>,
|
|
860
|
+
input: I,
|
|
861
|
+
value: Input<S>,
|
|
862
|
+
draft: Draft,
|
|
863
|
+
token: object,
|
|
864
|
+
defaultsFrom: (
|
|
865
|
+
prepared: Draft,
|
|
866
|
+
names: ReadonlySet<string>,
|
|
867
|
+
elsewhere: ReadonlySet<PropertyKey>,
|
|
868
|
+
) => ReadonlyArray<readonly [string, unknown]>,
|
|
869
|
+
) {
|
|
870
|
+
const edited = candidates(value, draft.editedFields.get(shape));
|
|
871
|
+
let { output, lost: lostFromInput } = yield* read(value);
|
|
872
|
+
let conflicts = filled(edited, output);
|
|
873
|
+
const own = draft[takenKey] && taken(value, output, lostFromInput);
|
|
874
|
+
let prepared = yield* self.prepare(
|
|
875
|
+
input,
|
|
876
|
+
own === undefined
|
|
877
|
+
? draft
|
|
878
|
+
: {
|
|
879
|
+
...draft,
|
|
880
|
+
[takenKey]: Shapes.taking(draft[takenKey], own, token),
|
|
881
|
+
},
|
|
882
|
+
);
|
|
883
|
+
if (lostFromInput.length > 0) {
|
|
884
|
+
const recovered = yield* recover(
|
|
885
|
+
value,
|
|
886
|
+
{ output, lost: lostFromInput },
|
|
887
|
+
prepared[takenKey],
|
|
888
|
+
token,
|
|
889
|
+
taken,
|
|
890
|
+
);
|
|
891
|
+
if (recovered !== undefined) {
|
|
892
|
+
({ output, lost: lostFromInput } = recovered.reading);
|
|
893
|
+
conflicts = filled(edited, output);
|
|
894
|
+
prepared = { ...prepared, [takenKey]: recovered.taken };
|
|
895
|
+
}
|
|
896
|
+
yield* Inputs.refuseLost(
|
|
897
|
+
contract,
|
|
898
|
+
lostFromInput,
|
|
899
|
+
prepared[takenKey]?.fields,
|
|
900
|
+
);
|
|
901
|
+
}
|
|
902
|
+
if (conflicts.length > 0)
|
|
903
|
+
return yield* preparationConflict(group, conflicts);
|
|
904
|
+
const names = Shapes.written(output, fold);
|
|
905
|
+
const elsewhere = Shapes.others(shape, prepared[takenKey], fold);
|
|
906
|
+
const defaults = defaultsFrom(prepared, names, elsewhere);
|
|
907
|
+
const settled = yield* Effect.mapError(settle(output, defaults), () =>
|
|
908
|
+
misfit(label, defaults),
|
|
909
|
+
);
|
|
910
|
+
// A field the schema does not write back would be lost, as a union member can lose one.
|
|
911
|
+
const lost = Shapes.lost(output, defaults, settled, fold);
|
|
912
|
+
if (lost.length > 0) return yield* lostFields(label, lost);
|
|
913
|
+
return { prepared, settled, names, defaults };
|
|
914
|
+
},
|
|
915
|
+
};
|
|
916
|
+
};
|
|
917
|
+
|
|
848
918
|
/**
|
|
849
919
|
* Bind a schema to the call's `{ headers }` input.
|
|
850
920
|
*
|
|
@@ -904,12 +974,7 @@ export const headers = <
|
|
|
904
974
|
>(
|
|
905
975
|
contract: S,
|
|
906
976
|
) => {
|
|
907
|
-
const
|
|
908
|
-
const recover = Groups.recovery(contract);
|
|
909
|
-
const { shape, settle, taken, candidates, filled } = Shapes.bind(
|
|
910
|
-
contract,
|
|
911
|
-
foldHeader,
|
|
912
|
-
);
|
|
977
|
+
const { shape, evaluate } = prepareGroup(contract, "headers", foldHeader);
|
|
913
978
|
return <I, E, R>(
|
|
914
979
|
self: Request<I, E, R>,
|
|
915
980
|
): Request<
|
|
@@ -932,58 +997,20 @@ export const headers = <
|
|
|
932
997
|
const value = (
|
|
933
998
|
input.headers === undefined ? {} : input.headers
|
|
934
999
|
) as Input<S>;
|
|
935
|
-
const
|
|
936
|
-
|
|
937
|
-
let conflicts = filled(edited, output);
|
|
938
|
-
const own = draft.takenHeaders && taken(value, output, lostFromInput);
|
|
939
|
-
let prepared = yield* self.prepare(
|
|
1000
|
+
const { prepared, settled, names, defaults } = yield* evaluate(
|
|
1001
|
+
self,
|
|
940
1002
|
input,
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
{ output, lost: lostFromInput },
|
|
952
|
-
prepared.takenHeaders,
|
|
953
|
-
token,
|
|
954
|
-
taken,
|
|
955
|
-
);
|
|
956
|
-
if (recovered !== undefined) {
|
|
957
|
-
({ output, lost: lostFromInput } = recovered.reading);
|
|
958
|
-
conflicts = filled(edited, output);
|
|
959
|
-
prepared = { ...prepared, takenHeaders: recovered.taken };
|
|
960
|
-
}
|
|
961
|
-
yield* Inputs.refuseLost(
|
|
962
|
-
contract,
|
|
963
|
-
lostFromInput,
|
|
964
|
-
prepared.takenHeaders?.fields,
|
|
965
|
-
);
|
|
966
|
-
}
|
|
967
|
-
if (conflicts.length > 0)
|
|
968
|
-
return yield* preparationConflict("headers", conflicts);
|
|
969
|
-
const names = Shapes.written(output, foldHeader);
|
|
970
|
-
const elsewhere = Shapes.others(
|
|
971
|
-
shape,
|
|
972
|
-
prepared.takenHeaders,
|
|
973
|
-
foldHeader,
|
|
974
|
-
);
|
|
975
|
-
const defaults = prepared.headerLines.filter(
|
|
976
|
-
([name]) =>
|
|
977
|
-
!names.has(foldHeader(name)) &&
|
|
978
|
-
!elsewhere.has(foldHeader(name)) &&
|
|
979
|
-
Shapes.allows(shape, foldHeader(name)),
|
|
980
|
-
);
|
|
981
|
-
const settled = yield* Effect.mapError(settle(output, defaults), () =>
|
|
982
|
-
misfit("header", defaults),
|
|
1003
|
+
value,
|
|
1004
|
+
draft,
|
|
1005
|
+
token,
|
|
1006
|
+
(prepared, names, elsewhere) =>
|
|
1007
|
+
prepared.headerLines.filter(
|
|
1008
|
+
([name]) =>
|
|
1009
|
+
!names.has(foldHeader(name)) &&
|
|
1010
|
+
!elsewhere.has(foldHeader(name)) &&
|
|
1011
|
+
Shapes.allows(shape, foldHeader(name)),
|
|
1012
|
+
),
|
|
983
1013
|
);
|
|
984
|
-
// A field the schema does not write back would be lost, as a union member can lose one.
|
|
985
|
-
const lost = Shapes.lost(output, defaults, settled, foldHeader);
|
|
986
|
-
if (lost.length > 0) return yield* lostFields("header", lost);
|
|
987
1014
|
const lines = Transport.entries(settled);
|
|
988
1015
|
const replaced = new Set([
|
|
989
1016
|
...names,
|
|
@@ -1083,12 +1110,7 @@ export const query = <
|
|
|
1083
1110
|
>(
|
|
1084
1111
|
contract: S,
|
|
1085
1112
|
) => {
|
|
1086
|
-
const
|
|
1087
|
-
const recover = Groups.recovery(contract);
|
|
1088
|
-
const { shape, settle, taken, candidates, filled } = Shapes.bind(
|
|
1089
|
-
contract,
|
|
1090
|
-
foldQuery,
|
|
1091
|
-
);
|
|
1113
|
+
const { shape, evaluate } = prepareGroup(contract, "query", foldQuery);
|
|
1092
1114
|
return <I, E, R>(
|
|
1093
1115
|
self: Request<I, E, R>,
|
|
1094
1116
|
): Request<
|
|
@@ -1111,58 +1133,24 @@ export const query = <
|
|
|
1111
1133
|
const value = (
|
|
1112
1134
|
input.query === undefined ? {} : input.query
|
|
1113
1135
|
) as Input<S>;
|
|
1114
|
-
const
|
|
1115
|
-
|
|
1116
|
-
let conflicts = filled(edited, output);
|
|
1117
|
-
const own = draft.takenQuery && taken(value, output, lostFromInput);
|
|
1118
|
-
let prepared = yield* self.prepare(
|
|
1136
|
+
const { prepared, settled, names, defaults } = yield* evaluate(
|
|
1137
|
+
self,
|
|
1119
1138
|
input,
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
);
|
|
1135
|
-
if (recovered !== undefined) {
|
|
1136
|
-
({ output, lost: lostFromInput } = recovered.reading);
|
|
1137
|
-
conflicts = filled(edited, output);
|
|
1138
|
-
prepared = { ...prepared, takenQuery: recovered.taken };
|
|
1139
|
-
}
|
|
1140
|
-
yield* Inputs.refuseLost(
|
|
1141
|
-
contract,
|
|
1142
|
-
lostFromInput,
|
|
1143
|
-
prepared.takenQuery?.fields,
|
|
1144
|
-
);
|
|
1145
|
-
}
|
|
1146
|
-
if (conflicts.length > 0)
|
|
1147
|
-
return yield* preparationConflict("query", conflicts);
|
|
1148
|
-
const names = Shapes.written(output, foldQuery);
|
|
1149
|
-
const elsewhere = Shapes.others(shape, prepared.takenQuery, foldQuery);
|
|
1150
|
-
const defaults = prepared.request.urlParams.params
|
|
1151
|
-
.filter(
|
|
1152
|
-
(param) =>
|
|
1153
|
-
prepared.declaredQuery.has(param) &&
|
|
1154
|
-
!isEmptySegment(prepared, param) &&
|
|
1155
|
-
!names.has(param[0]) &&
|
|
1156
|
-
!elsewhere.has(param[0]) &&
|
|
1157
|
-
Shapes.allows(shape, param[0]),
|
|
1158
|
-
)
|
|
1159
|
-
.map((param) => [param[0], queryValue(prepared, param)] as const);
|
|
1160
|
-
const settled = yield* Effect.mapError(settle(output, defaults), () =>
|
|
1161
|
-
misfit("query", defaults),
|
|
1139
|
+
value,
|
|
1140
|
+
draft,
|
|
1141
|
+
token,
|
|
1142
|
+
(prepared, names, elsewhere) =>
|
|
1143
|
+
prepared.request.urlParams.params
|
|
1144
|
+
.filter(
|
|
1145
|
+
(param) =>
|
|
1146
|
+
prepared.declaredQuery.has(param) &&
|
|
1147
|
+
!isEmptySegment(prepared, param) &&
|
|
1148
|
+
!names.has(param[0]) &&
|
|
1149
|
+
!elsewhere.has(param[0]) &&
|
|
1150
|
+
Shapes.allows(shape, param[0]),
|
|
1151
|
+
)
|
|
1152
|
+
.map((param) => [param[0], queryValue(prepared, param)] as const),
|
|
1162
1153
|
);
|
|
1163
|
-
// A field the schema does not write back would be lost, as a union member can lose one.
|
|
1164
|
-
const lost = Shapes.lost(output, defaults, settled, foldQuery);
|
|
1165
|
-
if (lost.length > 0) return yield* lostFields("query", lost);
|
|
1166
1154
|
return replaceQuery(
|
|
1167
1155
|
prepared,
|
|
1168
1156
|
UrlEncoding.queryEntries(settled),
|
|
@@ -1667,6 +1655,8 @@ const initialDraft = (declaration: Declaration): Draft => {
|
|
|
1667
1655
|
* goes through the encoder. With no entries at all, the encoder still has its say.
|
|
1668
1656
|
*/
|
|
1669
1657
|
const renderQuery = (draft: Draft, encoding: UrlEncoding.Service): string => {
|
|
1658
|
+
const params = draft.request.urlParams.params;
|
|
1659
|
+
if (params.length === 0) return encoding.query([]);
|
|
1670
1660
|
const parts: Array<string> = [];
|
|
1671
1661
|
let run: Array<readonly [string, string | null]> = [];
|
|
1672
1662
|
const endRun = () => {
|
|
@@ -1674,7 +1664,7 @@ const renderQuery = (draft: Draft, encoding: UrlEncoding.Service): string => {
|
|
|
1674
1664
|
if (rendered !== "") parts.push(rendered);
|
|
1675
1665
|
run = [];
|
|
1676
1666
|
};
|
|
1677
|
-
for (const param of
|
|
1667
|
+
for (const param of params) {
|
|
1678
1668
|
const written = draft.queryText.get(param);
|
|
1679
1669
|
if (written !== undefined) {
|
|
1680
1670
|
endRun();
|
|
@@ -1684,7 +1674,7 @@ const renderQuery = (draft: Draft, encoding: UrlEncoding.Service): string => {
|
|
|
1684
1674
|
}
|
|
1685
1675
|
}
|
|
1686
1676
|
endRun();
|
|
1687
|
-
return parts.
|
|
1677
|
+
return parts.join("&");
|
|
1688
1678
|
};
|
|
1689
1679
|
|
|
1690
1680
|
const finalize = (
|
package/src/http/UrlEncoding.ts
CHANGED
|
@@ -80,7 +80,8 @@ export interface Service {
|
|
|
80
80
|
* stands between other entries, each run of them is rendered by a separate call, keeping the
|
|
81
81
|
* order of the query. A request with no entries gets one call with an empty array, and a query
|
|
82
82
|
* made only of written text gets no call, so add a fixed entry with `Request.setQuery` in a
|
|
83
|
-
* shared transform rather than here.
|
|
83
|
+
* shared transform rather than here. Returning `""` omits that run; it does not ask the renderer
|
|
84
|
+
* again with empty input.
|
|
84
85
|
*/
|
|
85
86
|
readonly query: (entries: Query) => string;
|
|
86
87
|
/**
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { SchemaAST } from "effect";
|
|
2
|
+
import { isNonObjectAlternative } from "./primitive.js";
|
|
2
3
|
|
|
3
4
|
interface Names {
|
|
4
5
|
readonly decoded: ReadonlySet<PropertyKey>;
|
|
@@ -34,6 +35,9 @@ const namesOf = (
|
|
|
34
35
|
[(ast.encoding ?? []).map(({ to }) => to), encodings],
|
|
35
36
|
] as const) {
|
|
36
37
|
for (const node of nodes) {
|
|
38
|
+
// This is only a union-member rule. Primitive encoding intermediates remain unsupported,
|
|
39
|
+
// and never enter the shared memo as an empty group view.
|
|
40
|
+
if (output === members && isNonObjectAlternative(node)) continue;
|
|
37
41
|
const names = namesOf(node, active, known);
|
|
38
42
|
if (names === undefined) {
|
|
39
43
|
active.delete(ast);
|
|
@@ -44,6 +48,12 @@ const namesOf = (
|
|
|
44
48
|
}
|
|
45
49
|
}
|
|
46
50
|
active.delete(ast);
|
|
51
|
+
// A union of primitives is still a primitive encoding intermediate. Requiring an object view
|
|
52
|
+
// also prevents the shared memo from turning that intermediate into an empty group view.
|
|
53
|
+
if (ast._tag === "Union" && members.length === 0) {
|
|
54
|
+
known.set(ast, undefined);
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
47
57
|
const decoded = new Set(
|
|
48
58
|
ast._tag === "Objects"
|
|
49
59
|
? ast.propertySignatures.map(({ name }) => keyOf(name))
|
|
@@ -97,10 +97,51 @@ const isPlain = (value: unknown): value is Record<PropertyKey, unknown> => {
|
|
|
97
97
|
return prototype === Object.prototype || prototype === null;
|
|
98
98
|
};
|
|
99
99
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
100
|
+
/** An actual array position, rather than a property whose spelling merely converts to a number. */
|
|
101
|
+
const arrayIndex = (key: PropertyKey): number | undefined => {
|
|
102
|
+
if (typeof key === "symbol") return undefined;
|
|
103
|
+
const index = Number(key);
|
|
104
|
+
return Number.isInteger(index) &&
|
|
105
|
+
index >= 0 &&
|
|
106
|
+
index < 0xffff_ffff &&
|
|
107
|
+
String(index) === String(key)
|
|
108
|
+
? index
|
|
109
|
+
: undefined;
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
const keysOf = (value: object, arrays = false): ReadonlyArray<PropertyKey> => {
|
|
113
|
+
const list = arrays && Array.isArray(value);
|
|
114
|
+
const length = list ? value.length : 0;
|
|
115
|
+
const own = Reflect.ownKeys(value);
|
|
116
|
+
const keys = new Set(
|
|
117
|
+
own.filter(
|
|
118
|
+
(key) =>
|
|
119
|
+
Object.prototype.propertyIsEnumerable.call(value, key) ||
|
|
120
|
+
(list && arrayIndex(key) !== undefined),
|
|
121
|
+
),
|
|
103
122
|
);
|
|
123
|
+
const positions = list
|
|
124
|
+
? own.filter((key) => {
|
|
125
|
+
const index = arrayIndex(key);
|
|
126
|
+
return index !== undefined && index < length;
|
|
127
|
+
}).length
|
|
128
|
+
: 0;
|
|
129
|
+
if (list && positions < length) {
|
|
130
|
+
// Array schemas also read hidden or inherited positions. Materialize those cells if another
|
|
131
|
+
// cell needs rewriting; copying an enumerable-key list would compact or drop them.
|
|
132
|
+
const visited = new Set<object>();
|
|
133
|
+
let prototype: object | null = Object.getPrototypeOf(value);
|
|
134
|
+
while (prototype !== null && !visited.has(prototype)) {
|
|
135
|
+
visited.add(prototype);
|
|
136
|
+
for (const key of Reflect.ownKeys(prototype)) {
|
|
137
|
+
const index = arrayIndex(key);
|
|
138
|
+
if (index !== undefined && index < length) keys.add(key);
|
|
139
|
+
}
|
|
140
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
return [...keys];
|
|
144
|
+
};
|
|
104
145
|
|
|
105
146
|
/** Whether a side only holds literals, and this value is none of them. */
|
|
106
147
|
const refuses = (side: Ast, value: unknown): boolean =>
|
|
@@ -181,7 +222,9 @@ const typesAt = (
|
|
|
181
222
|
): ReadonlyArray<Ast> =>
|
|
182
223
|
sides.flatMap((side) => {
|
|
183
224
|
if (side._tag === "Arrays") {
|
|
184
|
-
const
|
|
225
|
+
const index = arrayIndex(key);
|
|
226
|
+
if (index === undefined) return [];
|
|
227
|
+
const element = side.elements[index];
|
|
185
228
|
return element === undefined ? side.rest : [element];
|
|
186
229
|
}
|
|
187
230
|
return side._tag === "Objects"
|
|
@@ -238,6 +281,11 @@ const unlisted = (
|
|
|
238
281
|
const readAsStruct = (sides: ReadonlyArray<Ast>, open: boolean): boolean =>
|
|
239
282
|
!open && !sides.some(({ _tag }) => _tag === "Declaration");
|
|
240
283
|
|
|
284
|
+
/** Discover array layout only where no open or opaque reading can retain it untouched. */
|
|
285
|
+
const readAsArray = (sides: ReadonlyArray<Ast>): boolean =>
|
|
286
|
+
sides.some(({ _tag }) => _tag === "Arrays") &&
|
|
287
|
+
readAsStruct(sides, leaveKeysOpen(sides));
|
|
288
|
+
|
|
241
289
|
/** Bring a value's {@link unlisted} fields out as keys, at any depth, and take out what `drops` says. */
|
|
242
290
|
const rewriting = (drops: (item: unknown, stated: boolean) => boolean) => {
|
|
243
291
|
const rewrite = (types: ReadonlyArray<Ast>, value: unknown): unknown => {
|
|
@@ -247,8 +295,9 @@ const rewriting = (drops: (item: unknown, stated: boolean) => boolean) => {
|
|
|
247
295
|
const open = leaveKeysOpen(sides);
|
|
248
296
|
const plain = isPlain(value);
|
|
249
297
|
const asStruct = !plain && readAsStruct(sides, open);
|
|
298
|
+
const asArray = readAsArray(sides);
|
|
250
299
|
const isList = Array.isArray(value);
|
|
251
|
-
const keys = [...keysOf(value), ...hidden];
|
|
300
|
+
const keys = [...keysOf(value, asArray), ...hidden];
|
|
252
301
|
const entries = keys.flatMap((key) => {
|
|
253
302
|
const item = value[key];
|
|
254
303
|
const inner = typesAt(sides, key);
|
|
@@ -263,11 +312,39 @@ const rewriting = (drops: (item: unknown, stated: boolean) => boolean) => {
|
|
|
263
312
|
hidden.length === 0 &&
|
|
264
313
|
entries.length === keys.length &&
|
|
265
314
|
entries.every(([key, item]) => item === value[key]);
|
|
266
|
-
return
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
315
|
+
if (same) return value;
|
|
316
|
+
if (!isList) return Object.fromEntries(entries);
|
|
317
|
+
// Own-key inspection already reads this descriptor. Do not add a length getter read when an
|
|
318
|
+
// enumerable child changes through a mixed opaque view, which may never read array layout.
|
|
319
|
+
const length: unknown = asArray
|
|
320
|
+
? value.length
|
|
321
|
+
: Object.getOwnPropertyDescriptor(value, "length")?.value;
|
|
322
|
+
if (
|
|
323
|
+
typeof length !== "number" ||
|
|
324
|
+
!Number.isInteger(length) ||
|
|
325
|
+
length < 0 ||
|
|
326
|
+
length > 0xffff_ffff
|
|
327
|
+
)
|
|
328
|
+
return value;
|
|
329
|
+
// A proxy can report an own index beyond its length. A native array cannot retain both;
|
|
330
|
+
// leave that incoherent frame to ordinary reading instead of manufacturing a larger view.
|
|
331
|
+
if (
|
|
332
|
+
entries.some(([key]) => {
|
|
333
|
+
const index = arrayIndex(key);
|
|
334
|
+
return index !== undefined && index >= length;
|
|
335
|
+
})
|
|
336
|
+
)
|
|
337
|
+
return value;
|
|
338
|
+
const output: Array<unknown> = [];
|
|
339
|
+
output.length = length;
|
|
340
|
+
for (const [key, item] of entries)
|
|
341
|
+
Object.defineProperty(output, key, {
|
|
342
|
+
value: item,
|
|
343
|
+
enumerable: true,
|
|
344
|
+
writable: true,
|
|
345
|
+
configurable: true,
|
|
346
|
+
});
|
|
347
|
+
return output;
|
|
271
348
|
};
|
|
272
349
|
return rewrite;
|
|
273
350
|
};
|
|
@@ -328,8 +405,11 @@ export const missing = (
|
|
|
328
405
|
(!isObject(value) ||
|
|
329
406
|
!decoded.has(String(key)) ||
|
|
330
407
|
Equal.equals(at(output, key), given[key]));
|
|
331
|
-
return keysOf(given).flatMap((key) => {
|
|
332
|
-
const here = [
|
|
408
|
+
return keysOf(given, readAsArray(sides)).flatMap((key) => {
|
|
409
|
+
const here = [
|
|
410
|
+
...path,
|
|
411
|
+
Array.isArray(given) ? (arrayIndex(key) ?? key) : key,
|
|
412
|
+
];
|
|
333
413
|
return given[key] === undefined || has(value, key) || holdsAsWritten(key)
|
|
334
414
|
? missing(
|
|
335
415
|
typesAt(sides, key),
|