viell-effect-schema-http 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -6
- package/dist/concurrency/Interaction.d.ts +7 -1
- package/dist/concurrency/Interaction.d.ts.map +1 -1
- package/dist/concurrency/Interaction.js.map +1 -1
- package/dist/http/Body.d.ts +2 -0
- package/dist/http/Body.d.ts.map +1 -1
- package/dist/http/Body.js.map +1 -1
- package/dist/http/Operation.d.ts +6 -0
- package/dist/http/Operation.d.ts.map +1 -1
- package/dist/http/Operation.js.map +1 -1
- package/dist/http/Policy.d.ts +25 -0
- package/dist/http/Policy.d.ts.map +1 -1
- package/dist/http/Policy.js +25 -0
- package/dist/http/Policy.js.map +1 -1
- package/dist/http/Request.d.ts +58 -15
- package/dist/http/Request.d.ts.map +1 -1
- package/dist/http/Request.js +54 -19
- package/dist/http/Request.js.map +1 -1
- package/dist/http/Response.d.ts +11 -1
- package/dist/http/Response.d.ts.map +1 -1
- package/dist/http/Response.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/excess.d.ts +44 -0
- package/dist/http/internal/excess.d.ts.map +1 -0
- package/dist/http/internal/excess.js +231 -0
- package/dist/http/internal/excess.js.map +1 -0
- package/dist/http/internal/input.d.ts +36 -5
- package/dist/http/internal/input.d.ts.map +1 -1
- package/dist/http/internal/input.js +72 -19
- package/dist/http/internal/input.js.map +1 -1
- package/dist/http/internal/shape.d.ts +19 -0
- package/dist/http/internal/shape.d.ts.map +1 -1
- package/dist/http/internal/shape.js +58 -12
- package/dist/http/internal/shape.js.map +1 -1
- package/package.json +3 -2
- package/src/concurrency/Interaction.ts +7 -1
- package/src/http/Body.ts +2 -0
- package/src/http/Operation.ts +6 -0
- package/src/http/Policy.ts +27 -0
- package/src/http/Request.ts +122 -23
- package/src/http/Response.ts +11 -1
- package/src/http/UrlEncoding.ts +2 -1
- package/src/http/internal/excess.ts +333 -0
- package/src/http/internal/input.ts +133 -28
- package/src/http/internal/shape.ts +92 -12
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import { Equal, Schema, SchemaAST, SchemaIssue } from "effect";
|
|
2
|
+
/** Parse options that make a schema refuse what it would strip. */
|
|
3
|
+
export const strict = { onExcessProperty: "error" };
|
|
4
|
+
/**
|
|
5
|
+
* The node a schema's encoding ends at. `toEncoded` gives the same names, but it flattens each
|
|
6
|
+
* field's own encoding away, and that encoding is how a field says what it writes.
|
|
7
|
+
*/
|
|
8
|
+
export const encodedEnd = (ast) => {
|
|
9
|
+
const last = ast.encoding?.at(-1);
|
|
10
|
+
return last === undefined ? ast : encodedEnd(last.to);
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* The parts of a schema this can see. What a declaration holds, as an `Option` or a `Map` does, is
|
|
14
|
+
* not among them: nothing here can reach it, so the schema's own reading applies there.
|
|
15
|
+
*/
|
|
16
|
+
const partsOf = (ast) => {
|
|
17
|
+
switch (ast._tag) {
|
|
18
|
+
case "Objects":
|
|
19
|
+
return [...ast.propertySignatures, ...ast.indexSignatures].map(({ type }) => type);
|
|
20
|
+
case "Arrays":
|
|
21
|
+
return [...ast.elements, ...ast.rest];
|
|
22
|
+
case "Union":
|
|
23
|
+
return ast.types;
|
|
24
|
+
default:
|
|
25
|
+
return [];
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
/** Whether a schema can read an object's keys, and so can strip one of them. */
|
|
29
|
+
const readsKeys = (ast) => ast._tag === "Objects" ||
|
|
30
|
+
ast._tag === "ObjectKeyword" ||
|
|
31
|
+
ast._tag === "Suspend" ||
|
|
32
|
+
ast._tag === "Unknown" ||
|
|
33
|
+
ast._tag === "Any" ||
|
|
34
|
+
partsOf(ast).some(readsKeys) ||
|
|
35
|
+
(ast.encoding ?? []).some(({ to }) => readsKeys(to));
|
|
36
|
+
/** A choice of member: a union between objects, or a suspended schema, which may not exist yet. */
|
|
37
|
+
const isMemberChoice = (ast) => ast._tag === "Suspend" ||
|
|
38
|
+
(ast._tag === "Union" && ast.types.filter(readsKeys).length > 1);
|
|
39
|
+
/**
|
|
40
|
+
* A choice of side: two ends that do not state the same names, as a renamed field's do, or an
|
|
41
|
+
* encoded end with a rest. A value on the encoded side can then fit the decoded side by losing the
|
|
42
|
+
* names it holds. An encoded side with no keys, such as text, has none to lose.
|
|
43
|
+
*/
|
|
44
|
+
const isSideChoice = (ast) => {
|
|
45
|
+
const end = encodedEnd(ast);
|
|
46
|
+
if (end === ast || !readsKeys(end))
|
|
47
|
+
return false;
|
|
48
|
+
const decoded = fieldsOf(ast).map(({ name }) => name);
|
|
49
|
+
const encoded = fieldsOf(end).map(({ name }) => name);
|
|
50
|
+
return (decoded.length !== encoded.length ||
|
|
51
|
+
decoded.some((name) => !encoded.includes(name)) ||
|
|
52
|
+
(end._tag === "Objects" && end.indexSignatures.length > 0));
|
|
53
|
+
};
|
|
54
|
+
const holding = (isChoice) => {
|
|
55
|
+
const holds = (ast) => isChoice(ast) ||
|
|
56
|
+
partsOf(ast).some(holds) ||
|
|
57
|
+
(ast.encoding ?? []).some(({ to }) => holds(to));
|
|
58
|
+
return holds;
|
|
59
|
+
};
|
|
60
|
+
/** The fields of a struct or class, as they are declared, with each field's own encoding. */
|
|
61
|
+
export const fieldsOf = (ast) => ast._tag === "Objects"
|
|
62
|
+
? ast.propertySignatures
|
|
63
|
+
: ast._tag === "Declaration" && ast.encoding !== undefined
|
|
64
|
+
? fieldsOf(ast.encoding[0].to)
|
|
65
|
+
: [];
|
|
66
|
+
const isObject = (value) => typeof value === "object" && value !== null;
|
|
67
|
+
/** A plain object or a list, which can be taken apart. A class instance needs a reason to be. */
|
|
68
|
+
const isPlain = (value) => {
|
|
69
|
+
if (!isObject(value))
|
|
70
|
+
return false;
|
|
71
|
+
if (Array.isArray(value))
|
|
72
|
+
return true;
|
|
73
|
+
const prototype = Object.getPrototypeOf(value);
|
|
74
|
+
return prototype === Object.prototype || prototype === null;
|
|
75
|
+
};
|
|
76
|
+
const keysOf = (value) => Reflect.ownKeys(value).filter((key) => Object.prototype.propertyIsEnumerable.call(value, key));
|
|
77
|
+
/** Whether a side only holds literals, and this value is none of them. */
|
|
78
|
+
const refuses = (side, value) => side._tag === "Literal"
|
|
79
|
+
? side.literal !== value
|
|
80
|
+
: side._tag === "Union" &&
|
|
81
|
+
side.types.every((member) => refuses(member, value));
|
|
82
|
+
/** Whether a literal field refuses a value on every side, which a tag can rule out by. */
|
|
83
|
+
const refusesLiteral = (type, value) => [type, ...(type.encoding ?? []).map(({ to }) => to)].every((side) => refuses(side, value));
|
|
84
|
+
/** Whether a member's tag rules it out for a value, as Effect's union does before it reads one. */
|
|
85
|
+
const ruledOut = (member, value) => isObject(value) &&
|
|
86
|
+
fieldsOf(member).some(({ name, type }) => !SchemaAST.isOptional(type) &&
|
|
87
|
+
value[name] !== undefined &&
|
|
88
|
+
refusesLiteral(type, value[name]));
|
|
89
|
+
/** The schemas a value can be read against here: each member its tag leaves, on each side. */
|
|
90
|
+
const sidesOf = (ast, value) => [
|
|
91
|
+
...(ast._tag === "Union"
|
|
92
|
+
? ast.types
|
|
93
|
+
.filter((member) => !ruledOut(member, value))
|
|
94
|
+
.flatMap((member) => sidesOf(member, value))
|
|
95
|
+
: ast._tag === "Suspend"
|
|
96
|
+
? sidesOf(ast.thunk(), value)
|
|
97
|
+
: [ast]),
|
|
98
|
+
...(ast.encoding ?? []).flatMap(({ to }) => sidesOf(to, value)),
|
|
99
|
+
];
|
|
100
|
+
/** The schemas of what these sides hold under one key. */
|
|
101
|
+
const typesAt = (sides, key) => sides.flatMap((side) => {
|
|
102
|
+
if (side._tag === "Arrays") {
|
|
103
|
+
const element = side.elements[Number(key)];
|
|
104
|
+
return element === undefined ? side.rest : [element];
|
|
105
|
+
}
|
|
106
|
+
return side._tag === "Objects"
|
|
107
|
+
? [
|
|
108
|
+
...side.propertySignatures
|
|
109
|
+
.filter(({ name }) => name === key)
|
|
110
|
+
.map(({ type }) => type),
|
|
111
|
+
...side.indexSignatures.map(({ type }) => type),
|
|
112
|
+
]
|
|
113
|
+
: [];
|
|
114
|
+
});
|
|
115
|
+
/** The names on a schema's decoded side, which a value can use as well as the wire names. */
|
|
116
|
+
const decodedNames = (ast, value) => ast._tag === "Union"
|
|
117
|
+
? ast.types
|
|
118
|
+
.filter((member) => !ruledOut(member, value))
|
|
119
|
+
.flatMap((member) => decodedNames(member, value))
|
|
120
|
+
: ast._tag === "Suspend"
|
|
121
|
+
? decodedNames(ast.thunk(), value)
|
|
122
|
+
: fieldsOf(ast).map(({ name }) => String(name));
|
|
123
|
+
/** Whether these sides leave a value's keys open, so none can be called unstated. */
|
|
124
|
+
const leaveKeysOpen = (sides) => sides.some(({ _tag }) => _tag === "Any" || _tag === "Unknown" || _tag === "ObjectKeyword") || !sides.some(({ _tag }) => _tag === "Objects" || _tag === "Arrays");
|
|
125
|
+
/**
|
|
126
|
+
* The names a struct here reads from a value although they are no keys of it, as an accessor on a
|
|
127
|
+
* class is. A strict read only counts keys as excess, so it would strip such a field with no error.
|
|
128
|
+
*/
|
|
129
|
+
const unlisted = (sides, value) => Array.isArray(value)
|
|
130
|
+
? []
|
|
131
|
+
: Array.from(new Set(sides.flatMap(fieldsOf).map(({ name }) => name))).filter((name) => name !== "__proto__" &&
|
|
132
|
+
value[name] !== undefined &&
|
|
133
|
+
!Object.prototype.propertyIsEnumerable.call(value, name));
|
|
134
|
+
/**
|
|
135
|
+
* Whether only a struct can read a value here. A struct builds a plain object from any object, so a
|
|
136
|
+
* class instance loses nothing by becoming one. A declaration may need the instance itself, so
|
|
137
|
+
* there it keeps its keys, and stays an instance unless something in it has to change.
|
|
138
|
+
*/
|
|
139
|
+
const readAsStruct = (sides, open) => !open && !sides.some(({ _tag }) => _tag === "Declaration");
|
|
140
|
+
/** Bring a value's {@link unlisted} fields out as keys, at any depth, and take out what `drops` says. */
|
|
141
|
+
const rewriting = (drops) => {
|
|
142
|
+
const rewrite = (types, value) => {
|
|
143
|
+
if (types.length === 0 || !isObject(value))
|
|
144
|
+
return value;
|
|
145
|
+
const sides = types.flatMap((type) => sidesOf(type, value));
|
|
146
|
+
const hidden = unlisted(sides, value);
|
|
147
|
+
const open = leaveKeysOpen(sides);
|
|
148
|
+
const plain = isPlain(value);
|
|
149
|
+
const asStruct = !plain && readAsStruct(sides, open);
|
|
150
|
+
const isList = Array.isArray(value);
|
|
151
|
+
const keys = [...keysOf(value), ...hidden];
|
|
152
|
+
const entries = keys.flatMap((key) => {
|
|
153
|
+
const item = value[key];
|
|
154
|
+
const inner = typesAt(sides, key);
|
|
155
|
+
return (plain || asStruct) &&
|
|
156
|
+
!isList &&
|
|
157
|
+
drops(item, open || inner.length > 0)
|
|
158
|
+
? []
|
|
159
|
+
: [[key, rewrite(inner, item)]];
|
|
160
|
+
});
|
|
161
|
+
const same = !asStruct &&
|
|
162
|
+
hidden.length === 0 &&
|
|
163
|
+
entries.length === keys.length &&
|
|
164
|
+
entries.every(([key, item]) => item === value[key]);
|
|
165
|
+
return same
|
|
166
|
+
? value
|
|
167
|
+
: isList
|
|
168
|
+
? entries.map(([, item]) => item)
|
|
169
|
+
: Object.fromEntries(entries);
|
|
170
|
+
};
|
|
171
|
+
return rewrite;
|
|
172
|
+
};
|
|
173
|
+
/**
|
|
174
|
+
* What a caller means by a value, at any depth of plain objects and lists: no key set to `undefined`,
|
|
175
|
+
* which holds nothing, and no key the schema does not state, which a struct would strip anyway. A
|
|
176
|
+
* member a tag has ruled out states nothing. Returns `value` itself if there is nothing to change.
|
|
177
|
+
*/
|
|
178
|
+
export const normalised = rewriting((item, stated) => item === undefined || !stated);
|
|
179
|
+
/** A value as the caller gave it, but for what {@link unlisted} brings out as keys. */
|
|
180
|
+
export const exposed = rewriting(() => false);
|
|
181
|
+
/** The fields of a group the caller gave and a schema states, under either of their names. */
|
|
182
|
+
export const statedFields = (ast, value) => {
|
|
183
|
+
if (!isObject(value) || Array.isArray(value))
|
|
184
|
+
return [];
|
|
185
|
+
const sides = sidesOf(ast, value);
|
|
186
|
+
return [...keysOf(value), ...unlisted(sides, value)].filter((key) => value[key] !== undefined && typesAt(sides, key).length > 0);
|
|
187
|
+
};
|
|
188
|
+
const has = (value, key) => isObject(value) && key in value && value[key] !== undefined;
|
|
189
|
+
const at = (value, key) => isObject(value) ? value[key] : undefined;
|
|
190
|
+
/** Whether a value's keys mean something here: it is plain, or a struct here reads it. */
|
|
191
|
+
const comparable = (sides, value) => isPlain(value) || sides.some(({ _tag }) => _tag === "Objects");
|
|
192
|
+
/**
|
|
193
|
+
* What a read lost from `given`, a {@link normalised} value, by comparing it with what came out. A
|
|
194
|
+
* field has to be in the decoded value or in what was written. Under a name a member decodes, what
|
|
195
|
+
* was written has to be what was given too, because that name can be another field's on the wire.
|
|
196
|
+
* What this cannot see into, such as text or a `Map`, is left to the schema's own reading.
|
|
197
|
+
*/
|
|
198
|
+
export const missing = (types, given, value, output, path = []) => {
|
|
199
|
+
if (!isObject(given))
|
|
200
|
+
return [];
|
|
201
|
+
const sides = types.flatMap((type) => sidesOf(type, given));
|
|
202
|
+
if (!comparable(sides, given))
|
|
203
|
+
return [];
|
|
204
|
+
const decoded = new Set(types.flatMap((type) => decodedNames(type, given)));
|
|
205
|
+
const holdsAsWritten = (key) => has(output, key) &&
|
|
206
|
+
(!isObject(value) ||
|
|
207
|
+
!decoded.has(String(key)) ||
|
|
208
|
+
Equal.equals(at(output, key), given[key]));
|
|
209
|
+
return keysOf(given).flatMap((key) => {
|
|
210
|
+
const here = [...path, Array.isArray(given) ? Number(key) : key];
|
|
211
|
+
return given[key] === undefined || has(value, key) || holdsAsWritten(key)
|
|
212
|
+
? missing(typesAt(sides, key), given[key], at(value, key), at(output, key), here)
|
|
213
|
+
: [here];
|
|
214
|
+
});
|
|
215
|
+
};
|
|
216
|
+
const notHeld = new SchemaIssue.InvalidValue({
|
|
217
|
+
message: "The schema cannot keep this field with the rest of the value",
|
|
218
|
+
});
|
|
219
|
+
/** A refusal that names the fields and, like any other schema failure here, leaves out values. */
|
|
220
|
+
export const refusal = (ast, [first, ...others]) => new Schema.SchemaError(new SchemaIssue.Composite(ast, [
|
|
221
|
+
new SchemaIssue.Pointer(first, notHeld),
|
|
222
|
+
...others.map((path) => new SchemaIssue.Pointer(path, notHeld)),
|
|
223
|
+
]));
|
|
224
|
+
/** Whether a schema holds a choice of member, which only a stacked schema can settle for it. */
|
|
225
|
+
export const choosesMember = holding(isMemberChoice);
|
|
226
|
+
/**
|
|
227
|
+
* Whether a schema holds a choice that can cost a field, of member or of side, anywhere it can be
|
|
228
|
+
* seen. Any other schema skips the strict pass. `Schema.NullOr` around a struct has no such choice.
|
|
229
|
+
*/
|
|
230
|
+
export const choosing = holding((ast) => isMemberChoice(ast) || isSideChoice(ast));
|
|
231
|
+
//# sourceMappingURL=excess.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"excess.js","sourceRoot":"","sources":["../../../src/http/internal/excess.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,QAAQ,CAAC;AAO/D,mEAAmE;AACnE,MAAM,CAAC,MAAM,MAAM,GAA2B,EAAE,gBAAgB,EAAE,OAAO,EAAE,CAAC;AAE5E;;;GAGG;AACH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,GAAQ,EAAO,EAAE;IAC1C,MAAM,IAAI,GAAG,GAAG,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAClC,OAAO,IAAI,KAAK,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;AACxD,CAAC,CAAC;AAEF;;;GAGG;AACH,MAAM,OAAO,GAAG,CAAC,GAAQ,EAAsB,EAAE;IAC/C,QAAQ,GAAG,CAAC,IAA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AA0C,EACvC,EAAE,CACtB,IAAI,MAAM,CAAC,WAAW,CACpB,IAAI,WAAW,CAAC,SAAS,CAAC,GAAG,EAAE;IAC7B,IAAI,WAAW,CAAC,OAAO,CAAC,KAAK,EAAE,OAAO,CAAC;IACvC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,WAAW,CAAC,OAAO,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;CAChE,CAAC,CACH,CAAC;AAEJ,gGAAgG;AAChG,MAAM,CAAC,MAAM,aAAa,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC;AAErD;;;GAGG;AACH,MAAM,CAAC,MAAM,QAAQ,GAAG,OAAO,CAC7B,CAAC,GAAG,EAAE,EAAE,CAAC,cAAc,CAAC,GAAG,CAAC,IAAI,YAAY,CAAC,GAAG,CAAC,CAClD,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { Effect, Schema } from "effect";
|
|
1
|
+
import { Effect, Schema, type SchemaAST } from "effect";
|
|
2
|
+
import * as Excess from "./excess.js";
|
|
2
3
|
/** What a schema's constructor takes. This is the one place that names the `~`-prefixed key. */
|
|
3
4
|
type ConstructorInput<S extends Schema.Top> = S["~type.make.in"];
|
|
4
5
|
/** A field's type on the encoded side, in whichever union members have it. */
|
|
@@ -30,10 +31,40 @@ export type Input<S extends Schema.Top> = EitherSide<ConstructorInput<S>, S["Enc
|
|
|
30
31
|
* @remarks
|
|
31
32
|
* A value that fits the decoded side is read as decoded, so it is encoded once. Anything else goes
|
|
32
33
|
* through the decoder as a whole, then field by field. Both routes validate, and a value that fits
|
|
33
|
-
* neither fails with the decoded side's error, so existing failures read the same.
|
|
34
|
+
* neither fails with the decoded side's error, so existing failures read the same. `options` go to
|
|
35
|
+
* every parse.
|
|
34
36
|
*/
|
|
35
|
-
export declare const toDecoded: <S extends Schema.Top>(contract: S) => (input: Input<S>) => Effect.Effect<S["Type"], Schema.SchemaError, S["DecodingServices"]>;
|
|
36
|
-
/**
|
|
37
|
-
export
|
|
37
|
+
export declare const toDecoded: <S extends Schema.Top>(contract: S, options?: SchemaAST.ParseOptions) => (input: Input<S>) => Effect.Effect<S["Type"], Schema.SchemaError, S["DecodingServices"]>;
|
|
38
|
+
/** What a schema wrote, and the fields of its input it would lose, which may be held elsewhere. */
|
|
39
|
+
export interface Written<A> {
|
|
40
|
+
readonly output: A;
|
|
41
|
+
readonly lost: ReadonlyArray<Excess.Path>;
|
|
42
|
+
}
|
|
43
|
+
/** Takes a decoded value out through a schema, as the options say. */
|
|
44
|
+
type Write<S extends Schema.Top, A, R> = (value: S["Type"], options?: SchemaAST.ParseOptions) => Effect.Effect<A, Schema.SchemaError, R>;
|
|
45
|
+
/**
|
|
46
|
+
* Bring a value to the schema's decoded side, then write it, and say what that would lose.
|
|
47
|
+
*
|
|
48
|
+
* @remarks
|
|
49
|
+
* Effect takes the first union member that fits and strips what that member does not state, on the
|
|
50
|
+
* decoded side and again when it encodes, so a later member that holds everything would lose to an
|
|
51
|
+
* earlier one that holds part of it. A value on a schema's encoded side can lose to its decoded side
|
|
52
|
+
* in the same way, where the two state different names. So a schema with either choice reads what
|
|
53
|
+
* the caller means strictly, through both steps, and a read that passes has stripped nothing.
|
|
54
|
+
*
|
|
55
|
+
* A value that cannot be read that way is read as Effect reads it, and the result is compared with
|
|
56
|
+
* what went in, so what is lost is what actually went, not what a failure suggests would. The write
|
|
57
|
+
* is strict first, because a loose write can choose another member and lose what the read kept.
|
|
58
|
+
*
|
|
59
|
+
* A schema with neither choice skips all of this, so a plain struct costs what it always did.
|
|
60
|
+
*/
|
|
61
|
+
export declare const reader: <S extends Schema.Top, A, R>(contract: S, write: Write<S, A, R>) => (input: Input<S>) => Effect.Effect<Written<A>, Schema.SchemaError, R | S["DecodingServices"]>;
|
|
62
|
+
/**
|
|
63
|
+
* Refuse the fields a schema would lose, unless another schema in the group took them. Only a field
|
|
64
|
+
* of the group itself can be taken, so one further down a value stays lost.
|
|
65
|
+
*/
|
|
66
|
+
export declare const refuseLost: (contract: Schema.Top, lost: ReadonlyArray<Excess.Path>, taken?: ReadonlySet<PropertyKey>) => Effect.Effect<void, Schema.SchemaError>;
|
|
67
|
+
/** Bring a value to the schema's decoded side and write it, with nothing else to hold what it loses. */
|
|
68
|
+
export declare const encoder: <S extends Schema.Top, A, R>(contract: S, write: Write<S, A, R>) => (input: EitherSide<ConstructorInput<S>, S["Encoded"]>) => Effect.Effect<A, Schema.SchemaError, R | S["DecodingServices"]>;
|
|
38
69
|
export {};
|
|
39
70
|
//# sourceMappingURL=input.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"input.d.ts","sourceRoot":"","sources":["../../../src/http/internal/input.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"input.d.ts","sourceRoot":"","sources":["../../../src/http/internal/input.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,SAAS,EAAE,MAAM,QAAQ,CAAC;AACxD,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AAEtC,gGAAgG;AAChG,KAAK,gBAAgB,CAAC,CAAC,SAAS,MAAM,CAAC,GAAG,IAAI,CAAC,CAAC,eAAe,CAAC,CAAC;AAEjE,8EAA8E;AAC9E,KAAK,YAAY,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,SAAS,OAAO,GACvC,CAAC,SAAS,MAAM,CAAC,GACf,CAAC,CAAC,CAAC,CAAC,GACJ,KAAK,GACP,KAAK,CAAC;AAEV;;;GAGG;AACH,KAAK,SAAS,CAAC,CAAC,EAAE,CAAC,IACjB,CAAC,SAAS,aAAa,CAAC,OAAO,CAAC,GAC5B,KAAK,GACL,CAAC,SAAS,MAAM,GACd,CAAC,SAAS,MAAM,GACd;IAAE,QAAQ,EAAE,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,G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aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;CAC3C;AAED,sEAAsE;AACtE,KAAK,KAAK,CAAC,CAAC,SAAS,MAAM,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,IAAI,CACvC,KAAK,EAAE,CAAC,CAAC,MAAM,CAAC,EAChB,OAAO,CAAC,EAAE,SAAS,CAAC,YAAY,KAC7B,MAAM,CAAC,MAAM,CAAC,CAAC,EAAE,MAAM,CAAC,WAAW,EAAE,CAAC,CAAC,CAAC;AAc7C;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,MAAM,GAAI,CAAC,SAAS,MAAM,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,EAC/C,UAAU,CAAC,EACX,OAAO,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,aAKJ,KAAK,CAAC,CAAC,CAAC,6EAqC1B,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,UAAU,GACrB,UAAU,MAAM,CAAC,GAAG,EACpB,MAAM,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,EAChC,QAAQ,WAAW,CAAC,WAAW,CAAC,KAC/B,MAAM,CAAC,MAAM,CAAC,IAAI,EAAE,MAAM,CAAC,WAAW,CAOxC,CAAC;AAEF,wGAAwG;AACxG,eAAO,MAAM,OAAO,GAAI,CAAC,SAAS,MAAM,CAAC,GAAG,EAAE,CAAC,EAAE,CAAC,EAChD,UAAU,CAAC,EACX,OAAO,KAAK,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC,8HAUtB,CAAC"}
|
|
@@ -1,40 +1,93 @@
|
|
|
1
|
-
import { Effect, Schema
|
|
2
|
-
|
|
3
|
-
const fieldsOf = (ast) => ast._tag === "Objects"
|
|
4
|
-
? ast.propertySignatures
|
|
5
|
-
: ast._tag === "Declaration" && ast.encoding !== undefined
|
|
6
|
-
? fieldsOf(ast.encoding[0].to)
|
|
7
|
-
: [];
|
|
1
|
+
import { Effect, Schema } from "effect";
|
|
2
|
+
import * as Excess from "./excess.js";
|
|
8
3
|
/**
|
|
9
4
|
* Bring a value on either side of a schema to its decoded side.
|
|
10
5
|
*
|
|
11
6
|
* @remarks
|
|
12
7
|
* A value that fits the decoded side is read as decoded, so it is encoded once. Anything else goes
|
|
13
8
|
* through the decoder as a whole, then field by field. Both routes validate, and a value that fits
|
|
14
|
-
* neither fails with the decoded side's error, so existing failures read the same.
|
|
9
|
+
* neither fails with the decoded side's error, so existing failures read the same. `options` go to
|
|
10
|
+
* every parse.
|
|
15
11
|
*/
|
|
16
|
-
export const toDecoded = (contract) => {
|
|
17
|
-
const
|
|
12
|
+
export const toDecoded = (contract, options) => {
|
|
13
|
+
const making = options && { parseOptions: options };
|
|
14
|
+
const make = (input) => Effect.mapError(contract.makeEffect(input, making), (issue) => new Schema.SchemaError(issue));
|
|
18
15
|
const decode = Schema.decodeUnknownEffect(contract);
|
|
19
16
|
const byField = Effect.fnUntraced(function* (input) {
|
|
20
17
|
const output = { ...input };
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
18
|
+
// A field need not be a key, as an accessor on a class is not, so it comes from `input`.
|
|
19
|
+
const fields = input;
|
|
20
|
+
for (const { name, type } of Excess.fieldsOf(contract.ast))
|
|
21
|
+
if (fields[name] !== undefined)
|
|
22
|
+
output[name] = yield* fromPart(type, fields[name], options);
|
|
24
23
|
return yield* make(output);
|
|
25
24
|
});
|
|
26
|
-
const byMember = (input) => contract.ast._tag === "Union"
|
|
27
|
-
? Effect.firstSuccessOf(contract.ast.types.map((member) => fromPart(member, input)))
|
|
25
|
+
const byMember = (input) => contract.ast._tag === "Union" && contract.ast.types.length > 0
|
|
26
|
+
? Effect.firstSuccessOf(contract.ast.types.map((member) => fromPart(member, input, options)))
|
|
28
27
|
: byField(input);
|
|
29
|
-
return (input) => Effect.catch(make(input), (failure) => Effect.mapError(Effect.catch(decode(input), (encodedFailure) => typeof input === "object" && input !== null
|
|
28
|
+
return (input) => Effect.catch(make(input), (failure) => Effect.mapError(Effect.catch(decode(input, options), (encodedFailure) => typeof input === "object" && input !== null
|
|
30
29
|
? byMember(input)
|
|
31
30
|
: Effect.fail(encodedFailure)), () => failure));
|
|
32
31
|
};
|
|
33
32
|
/** Read a field or union member, which may be on either side of its own schema. */
|
|
34
|
-
const fromPart = (ast, value) => toDecoded(Schema.make(ast))(value);
|
|
35
|
-
/** Bring a value to the schema's decoded side, then write it. */
|
|
36
|
-
|
|
33
|
+
const fromPart = (ast, value, options) => toDecoded(Schema.make(ast), options)(value);
|
|
34
|
+
/** Bring a value to the schema's decoded side, then write it, as Effect reads it. */
|
|
35
|
+
const stripping = (contract, write) => {
|
|
37
36
|
const decoded = toDecoded(contract);
|
|
38
37
|
return (input) => Effect.flatMap(decoded(input), write);
|
|
39
38
|
};
|
|
39
|
+
/**
|
|
40
|
+
* Bring a value to the schema's decoded side, then write it, and say what that would lose.
|
|
41
|
+
*
|
|
42
|
+
* @remarks
|
|
43
|
+
* Effect takes the first union member that fits and strips what that member does not state, on the
|
|
44
|
+
* decoded side and again when it encodes, so a later member that holds everything would lose to an
|
|
45
|
+
* earlier one that holds part of it. A value on a schema's encoded side can lose to its decoded side
|
|
46
|
+
* in the same way, where the two state different names. So a schema with either choice reads what
|
|
47
|
+
* the caller means strictly, through both steps, and a read that passes has stripped nothing.
|
|
48
|
+
*
|
|
49
|
+
* A value that cannot be read that way is read as Effect reads it, and the result is compared with
|
|
50
|
+
* what went in, so what is lost is what actually went, not what a failure suggests would. The write
|
|
51
|
+
* is strict first, because a loose write can choose another member and lose what the read kept.
|
|
52
|
+
*
|
|
53
|
+
* A schema with neither choice skips all of this, so a plain struct costs what it always did.
|
|
54
|
+
*/
|
|
55
|
+
export const reader = (contract, write) => {
|
|
56
|
+
const stripped = stripping(contract, write);
|
|
57
|
+
const keepsAll = (output) => ({ output, lost: [] });
|
|
58
|
+
if (!Excess.choosing(contract.ast))
|
|
59
|
+
return (input) => Effect.map(stripped(input), keepsAll);
|
|
60
|
+
const decoded = toDecoded(contract);
|
|
61
|
+
const exact = toDecoded(contract, Excess.strict);
|
|
62
|
+
const kept = (input) => Effect.flatMap(exact(input), (value) => write(value, Excess.strict));
|
|
63
|
+
const compared = (input, given) => Effect.flatMap(decoded(input), (value) => Effect.map(Effect.catch(write(value, Excess.strict), () => write(value)), (output) => ({
|
|
64
|
+
output,
|
|
65
|
+
lost: Excess.missing([contract.ast], given, value, output),
|
|
66
|
+
})));
|
|
67
|
+
return (input) => {
|
|
68
|
+
const given = Excess.normalised([contract.ast], input);
|
|
69
|
+
// A strict read cannot see a field outside a value's keys, so it may not fall back on `input`.
|
|
70
|
+
const seen = (given === input ? input : Excess.exposed([contract.ast], input));
|
|
71
|
+
return Effect.catch(Effect.map(given === input
|
|
72
|
+
? kept(input)
|
|
73
|
+
: Effect.catch(kept(given), () => kept(seen)), keepsAll), () => compared(input, given));
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
/**
|
|
77
|
+
* Refuse the fields a schema would lose, unless another schema in the group took them. Only a field
|
|
78
|
+
* of the group itself can be taken, so one further down a value stays lost.
|
|
79
|
+
*/
|
|
80
|
+
export const refuseLost = (contract, lost, taken) => {
|
|
81
|
+
const [first, ...others] = lost.filter((path) => path.length > 1 || taken?.has(path[0]) !== true);
|
|
82
|
+
return first === undefined
|
|
83
|
+
? Effect.void
|
|
84
|
+
: Effect.fail(Excess.refusal(contract.ast, [first, ...others]));
|
|
85
|
+
};
|
|
86
|
+
/** Bring a value to the schema's decoded side and write it, with nothing else to hold what it loses. */
|
|
87
|
+
export const encoder = (contract, write) => {
|
|
88
|
+
if (!Excess.choosing(contract.ast))
|
|
89
|
+
return stripping(contract, write);
|
|
90
|
+
const read = reader(contract, write);
|
|
91
|
+
return (input) => Effect.flatMap(read(input), ({ output, lost }) => Effect.as(refuseLost(contract, lost), output));
|
|
92
|
+
};
|
|
40
93
|
//# sourceMappingURL=input.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"input.js","sourceRoot":"","sources":["../../../src/http/internal/input.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"input.js","sourceRoot":"","sources":["../../../src/http/internal/input.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAkB,MAAM,QAAQ,CAAC;AACxD,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AAoDtC;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CACvB,QAAW,EACX,OAAgC,EAChC,EAAE;IACF,MAAM,MAAM,GAAG,OAAO,IAAI,EAAE,YAAY,EAAE,OAAO,EAAE,CAAC;IACpD,MAAM,IAAI,GAAG,CAAC,KAAc,EAAE,EAAE,CAC9B,MAAM,CAAC,QAAQ,CACb,QAAQ,CAAC,UAAU,CAAC,KAAK,EAAE,MAAM,CAAC,EAClC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,MAAM,CAAC,WAAW,CAAC,KAAK,CAAC,CACzC,CAAC;IACJ,MAAM,MAAM,GAAG,MAAM,CAAC,mBAAmB,CAAC,QAAQ,CAAC,CAAC;IACpD,MAAM,OAAO,GAAG,MAAM,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,KAAa;QACxD,MAAM,MAAM,GAAiC,EAAE,GAAG,KAAK,EAAE,CAAC;QAC1D,yFAAyF;QACzF,MAAM,MAAM,GAAiC,KAAc,CAAC;QAC5D,KAAK,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,IAAI,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC;YACxD,IAAI,MAAM,CAAC,IAAI,CAAC,KAAK,SAAS;gBAC5B,MAAM,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,CAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,CAAC;QAChE,OAAO,KAAK,CAAC,CAAC,IAAI,CAAC,MAA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AAC,CAAC,MAAM,CAAC,IAAI;QACb,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,QAAQ,CAAC,GAAG,EAAE,CAAC,KAAK,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;AACpE,CAAC,CAAC;AAEF,wGAAwG;AACxG,MAAM,CAAC,MAAM,OAAO,GAAG,CACrB,QAAW,EACX,KAAqB,EACrB,EAAE;IACF,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,QAAQ,CAAC,GAAG,CAAC;QAAE,OAAO,SAAS,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IACtE,MAAM,IAAI,GAAG,MAAM,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IACrC,OAAO,CACL,KAAe,EACkD,EAAE,CACnE,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,EAAE,MAAM,EAAE,IAAI,EAAE,EAAE,EAAE,CAC/C,MAAM,CAAC,EAAE,CAAC,UAAU,CAAC,QAAQ,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,CAC9C,CAAC;AACN,CAAC,CAAC"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Effect, Schema } from "effect";
|
|
2
|
+
import * as Excess from "./excess.js";
|
|
2
3
|
import type { Fold } from "./fields.js";
|
|
3
4
|
/** What one schema says about its field group. Names are wire names, in their compared form. */
|
|
4
5
|
export interface Shape {
|
|
@@ -8,6 +9,15 @@ export interface Shape {
|
|
|
8
9
|
readonly open: boolean;
|
|
9
10
|
/** The names it always writes, so a raw edit of one could never reach the client. */
|
|
10
11
|
readonly required: ReadonlySet<string>;
|
|
12
|
+
/** Whether it holds a union that can lose a field, which a stacked schema may take. */
|
|
13
|
+
readonly chooses: boolean;
|
|
14
|
+
}
|
|
15
|
+
/** What the schemas stacked on a group have told each other. */
|
|
16
|
+
export interface Taken {
|
|
17
|
+
/** The fields of the caller's input a schema took. Only this says a field is not lost. */
|
|
18
|
+
readonly fields: ReadonlySet<PropertyKey>;
|
|
19
|
+
/** The compared names schemas with no union allow. Each settles a raw value under its names. */
|
|
20
|
+
readonly settled: ReadonlySet<string>;
|
|
11
21
|
}
|
|
12
22
|
/** Whether a shape allows a name, given in its compared form. */
|
|
13
23
|
export declare const allows: (shape: Shape, name: string) => boolean;
|
|
@@ -20,6 +30,14 @@ export declare const required: (names: ReadonlyArray<string>, shapes: ReadonlyAr
|
|
|
20
30
|
* so a default stays, while an empty list is the caller saying there are no entries.
|
|
21
31
|
*/
|
|
22
32
|
export declare const written: (output: object, fold: Fold) => ReadonlySet<string>;
|
|
33
|
+
/**
|
|
34
|
+
* The names a raw value does not concern a union under, in their compared form: what another schema
|
|
35
|
+
* took from the caller, and what a schema with no union settles itself. A schema with no choice
|
|
36
|
+
* settles its own defaults, so nothing is left to it by this.
|
|
37
|
+
*/
|
|
38
|
+
export declare const others: (shape: Shape, taken: Taken | undefined, fold: Fold) => ReadonlySet<PropertyKey>;
|
|
39
|
+
/** The record once a schema has added what it took. */
|
|
40
|
+
export declare const taking: (taken: Taken | undefined, own: ReadonlySet<PropertyKey>) => Taken;
|
|
23
41
|
/**
|
|
24
42
|
* What went into `settle` and is gone from what came out, in the spelling it went in with. A schema
|
|
25
43
|
* can write less than it was given, and that must not pass as a group that was built.
|
|
@@ -40,5 +58,6 @@ export declare const lost: (output: object, defaults: ReadonlyArray<readonly [st
|
|
|
40
58
|
export declare const bind: <S extends Schema.Constraint>(contract: S, fold: Fold) => {
|
|
41
59
|
shape: Shape;
|
|
42
60
|
settle: <W extends object, A>(output: W, defaults: ReadonlyArray<readonly [string, A]>) => Effect.Effect<W, Schema.SchemaError, S["DecodingServices"] | S["EncodingServices"]>;
|
|
61
|
+
taken: (input: unknown, output: object, losing: ReadonlyArray<Excess.Path>) => ReadonlySet<PropertyKey>;
|
|
43
62
|
};
|
|
44
63
|
//# sourceMappingURL=shape.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"shape.d.ts","sourceRoot":"","sources":["../../../src/http/internal/shape.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAa,MAAM,QAAQ,CAAC;AACnD,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAExC,gGAAgG;AAChG,MAAM,WAAW,KAAK;IACpB,mCAAmC;IACnC,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IACpC,sFAAsF;IACtF,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,qFAAqF;IACrF,QAAQ,CAAC,QAAQ,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"shape.d.ts","sourceRoot":"","sources":["../../../src/http/internal/shape.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,EAAa,MAAM,QAAQ,CAAC;AACnD,OAAO,KAAK,MAAM,MAAM,aAAa,CAAC;AACtC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAExC,gGAAgG;AAChG,MAAM,WAAW,KAAK;IACpB,mCAAmC;IACnC,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IACpC,sFAAsF;IACtF,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;IACvB,qFAAqF;IACrF,QAAQ,CAAC,QAAQ,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IACvC,uFAAuF;IACvF,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;CAC3B;AAED,gEAAgE;AAChE,MAAM,WAAW,KAAK;IACpB,0FAA0F;IAC1F,QAAQ,CAAC,MAAM,EAAE,WAAW,CAAC,WAAW,CAAC,CAAC;IAC1C,gGAAgG;IAChG,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;CACvC;AAsKD,iEAAiE;AACjE,eAAO,MA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AC,EACD,MAAM,CAAC,WAAW,EAClB,CAAC,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAC,kBAAkB,CAAC,CAC9C;mBAjBQ,OAAO,UACN,MAAM,UACN,aAAa,CAAC,MAAM,CAAC,IAAI,CAAC,KACjC,WAAW,CAAC,WAAW,CAAC;CAgD5B,CAAC"}
|
|
@@ -1,15 +1,31 @@
|
|
|
1
1
|
import { Effect, Schema, SchemaAST } from "effect";
|
|
2
|
+
import * as Excess from "./excess.js";
|
|
2
3
|
const holdsUndefined = (ast) => ast._tag === "Undefined" ||
|
|
3
4
|
ast._tag === "Void" ||
|
|
4
5
|
(ast._tag === "Union" && ast.types.some(holdsUndefined));
|
|
6
|
+
/**
|
|
7
|
+
* Whether a field whose encoded side is optional is written all the same, as a decoding default is.
|
|
8
|
+
* The decoded side has to hold a value, and the step that makes the encoded side optional has to
|
|
9
|
+
* hand that value on as it is. A step that decides for itself may write nothing, as
|
|
10
|
+
* `Schema.OptionFromOptionalKey` does for `None` and the `omit` strategy of a default always does.
|
|
11
|
+
*/
|
|
12
|
+
const writesOptional = (type) => {
|
|
13
|
+
const decoded = SchemaAST.toType(type);
|
|
14
|
+
const step = type.encoding?.find(({ to }) => SchemaAST.isOptional(to));
|
|
15
|
+
return (!SchemaAST.isOptional(decoded) &&
|
|
16
|
+
!holdsUndefined(decoded) &&
|
|
17
|
+
step?.transformation._tag === "Transformation" &&
|
|
18
|
+
step.transformation.encode._tag === "Passthrough");
|
|
19
|
+
};
|
|
5
20
|
const readObjects = (ast) => {
|
|
6
|
-
const stated = ast.propertySignatures.map(({ name, type }) => [String(name), type]);
|
|
21
|
+
const stated = ast.propertySignatures.map(({ name, type }) => [String(name), SchemaAST.toEncoded(type), type]);
|
|
7
22
|
return {
|
|
8
|
-
fields: new Map(stated.map(([name,
|
|
9
|
-
rest: ast.indexSignatures.map(({ type }) => type),
|
|
23
|
+
fields: new Map(stated.map(([name, encoded]) => [name, [encoded]])),
|
|
24
|
+
rest: ast.indexSignatures.map(({ type }) => SchemaAST.toEncoded(type)),
|
|
10
25
|
open: ast.indexSignatures.length > 0,
|
|
11
26
|
required: new Set(stated
|
|
12
|
-
.filter(([, type]) => !SchemaAST.isOptional(
|
|
27
|
+
.filter(([, encoded, type]) => (!SchemaAST.isOptional(encoded) && !holdsUndefined(encoded)) ||
|
|
28
|
+
writesOptional(type))
|
|
13
29
|
.map(([name]) => name)),
|
|
14
30
|
};
|
|
15
31
|
};
|
|
@@ -27,12 +43,15 @@ const readUnion = (members) => {
|
|
|
27
43
|
required: new Set(Array.from(first?.required ?? []).filter((name) => others.every(({ required }) => required.has(name)))),
|
|
28
44
|
};
|
|
29
45
|
};
|
|
30
|
-
/** Read a schema's encoded
|
|
31
|
-
const read = (ast) =>
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
?
|
|
35
|
-
:
|
|
46
|
+
/** Read a schema's encoded side. Where names cannot be read, the schema allows any name. */
|
|
47
|
+
const read = (ast) => {
|
|
48
|
+
const end = Excess.encodedEnd(ast);
|
|
49
|
+
return end._tag === "Objects"
|
|
50
|
+
? readObjects(end)
|
|
51
|
+
: end._tag === "Union"
|
|
52
|
+
? readUnion(end.types.map(read))
|
|
53
|
+
: { fields: new Map(), rest: [], open: true, required: new Set() };
|
|
54
|
+
};
|
|
36
55
|
const takesList = (ast) => ast._tag === "Arrays" || (ast._tag === "Union" && ast.types.some(takesList));
|
|
37
56
|
/** The single values a field's encoded type takes, looking inside unions and lists. */
|
|
38
57
|
const scalars = (ast) => ast._tag === "Union"
|
|
@@ -120,6 +139,23 @@ const writtenNames = (output) => (Symbol.iterator in output
|
|
|
120
139
|
* so a default stays, while an empty list is the caller saying there are no entries.
|
|
121
140
|
*/
|
|
122
141
|
export const written = (output, fold) => new Set(writtenNames(output).map(fold));
|
|
142
|
+
const noNames = new Set();
|
|
143
|
+
/**
|
|
144
|
+
* The names a raw value does not concern a union under, in their compared form: what another schema
|
|
145
|
+
* took from the caller, and what a schema with no union settles itself. A schema with no choice
|
|
146
|
+
* settles its own defaults, so nothing is left to it by this.
|
|
147
|
+
*/
|
|
148
|
+
export const others = (shape, taken, fold) => !shape.chooses || taken === undefined
|
|
149
|
+
? noNames
|
|
150
|
+
: new Set([
|
|
151
|
+
...Array.from(taken.fields, (name) => typeof name === "string" ? fold(name) : name),
|
|
152
|
+
...taken.settled,
|
|
153
|
+
]);
|
|
154
|
+
/** The record once a schema has added what it took. */
|
|
155
|
+
export const taking = (taken, own) => ({
|
|
156
|
+
fields: new Set([...(taken?.fields ?? []), ...own]),
|
|
157
|
+
settled: taken?.settled ?? new Set(),
|
|
158
|
+
});
|
|
123
159
|
/**
|
|
124
160
|
* What went into `settle` and is gone from what came out, in the spelling it went in with. A schema
|
|
125
161
|
* can write less than it was given, and that must not pass as a group that was built.
|
|
@@ -141,15 +177,25 @@ export const lost = (output, defaults, settled, fold) => {
|
|
|
141
177
|
* Without one it takes the looser match, and {@link lost} tells the caller what that left out.
|
|
142
178
|
*/
|
|
143
179
|
export const bind = (contract, fold) => {
|
|
144
|
-
const reading = read(
|
|
180
|
+
const reading = read(contract.ast);
|
|
145
181
|
const spelling = new Map(Array.from(reading.fields.keys(), (name) => [fold(name), name]));
|
|
146
182
|
const decode = Schema.decodeUnknownEffect(contract);
|
|
147
183
|
const encode = Schema.encodeEffect(contract);
|
|
184
|
+
const chooses = Excess.choosesMember(contract.ast);
|
|
148
185
|
const shape = {
|
|
149
186
|
names: new Set(spelling.keys()),
|
|
150
187
|
open: reading.open,
|
|
151
188
|
required: new Set(Array.from(reading.required, fold)),
|
|
189
|
+
chooses,
|
|
152
190
|
};
|
|
191
|
+
/**
|
|
192
|
+
* The caller's fields this schema took: what it wrote, and any it states and the caller gave,
|
|
193
|
+
* which may be written under another name or not at all. What it would lose is not among them.
|
|
194
|
+
*/
|
|
195
|
+
const taken = (input, output, losing) => new Set([
|
|
196
|
+
...writtenNames(output),
|
|
197
|
+
...Excess.statedFields(contract.ast, input),
|
|
198
|
+
].filter((name) => !losing.some(([key]) => key === name)));
|
|
153
199
|
const settle = (output, defaults) => {
|
|
154
200
|
if (defaults.length === 0)
|
|
155
201
|
return Effect.succeed(output);
|
|
@@ -174,6 +220,6 @@ export const bind = (contract, fold) => {
|
|
|
174
220
|
const through = (options) => Effect.flatMap(decode(group, options), (decoded) => encode(decoded, options));
|
|
175
221
|
return Effect.catch(through({ onExcessProperty: "error" }), () => through());
|
|
176
222
|
};
|
|
177
|
-
return { shape, settle };
|
|
223
|
+
return { shape, settle, taken };
|
|
178
224
|
};
|
|
179
225
|
//# sourceMappingURL=shape.js.map
|