viell-effect-schema-http 0.3.0 → 0.5.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.
@@ -115,15 +115,52 @@ const refusesLiteral = (type: Ast, value: unknown): boolean =>
115
115
  refuses(side, value),
116
116
  );
117
117
 
118
- /** Whether a member's tag rules it out for a value, as Effect's union does before it reads one. */
119
- const ruledOut = (member: Ast, value: unknown): boolean =>
120
- isObject(value) &&
121
- fieldsOf(member).some(
118
+ const onlyLiterals = (ast: Ast): boolean =>
119
+ ast._tag === "Literal" ||
120
+ (ast._tag === "Union" &&
121
+ ast.types.every(
122
+ (member) => member.encoding === undefined && onlyLiterals(member),
123
+ ));
124
+
125
+ /** Each side of an object transformation, without guessing which names correspond. */
126
+ const objectSides = (ast: Ast): ReadonlyArray<Ast> => [
127
+ ast,
128
+ ...(ast.encoding ?? []).flatMap(({ to }) => objectSides(to)),
129
+ ];
130
+
131
+ /** Whether this view's literal tags make it impossible, without reading a default. */
132
+ const tagView = (
133
+ side: Ast,
134
+ value: Record<PropertyKey, unknown>,
135
+ constructing: boolean,
136
+ ) => {
137
+ const tags = fieldsOf(side).filter(
138
+ ({ type }) =>
139
+ !SchemaAST.isOptional(type) && objectSides(type).every(onlyLiterals),
140
+ );
141
+ const mismatch = tags.some(
142
+ ({ name, type }) =>
143
+ value[name] !== undefined && refusesLiteral(type, value[name]),
144
+ );
145
+ const absent = tags.some(
122
146
  ({ name, type }) =>
123
- !SchemaAST.isOptional(type) &&
124
- value[name] !== undefined &&
125
- refusesLiteral(type, value[name]),
147
+ value[name] === undefined &&
148
+ (!constructing || type.context?.constructorDefault === undefined),
126
149
  );
150
+ return { impossible: mismatch || absent, mismatch };
151
+ };
152
+
153
+ /** A tag can choose on either side; both views must rule a member out before its fields go. */
154
+ const ruledOut = (member: Ast, value: unknown): boolean => {
155
+ if (!isObject(value)) return false;
156
+ const decoded = tagView(member, value, true);
157
+ const encoded = tagView(SchemaAST.toEncoded(member), value, false);
158
+ return (
159
+ decoded.impossible &&
160
+ encoded.impossible &&
161
+ (decoded.mismatch || encoded.mismatch)
162
+ );
163
+ };
127
164
 
128
165
  /** The schemas a value can be read against here: each member its tag leaves, on each side. */
129
166
  const sidesOf = (ast: Ast, value: unknown): ReadonlyArray<Ast> => [
@@ -147,20 +147,27 @@ const stripping = <S extends Schema.Top, A, R>(
147
147
  * what went in, so what is lost is what actually went, not what a failure suggests would. The write
148
148
  * is strict first, because a loose write can choose another member and lose what the read kept.
149
149
  *
150
- * A schema with neither choice skips all of this, so a plain struct costs what it always did.
150
+ * A schema with neither choice skips the strict read and loss comparison.
151
151
  */
152
152
  export const reader = <S extends Schema.Top, A, R>(
153
153
  contract: S,
154
154
  write: Write<S, A, R>,
155
155
  ) => {
156
- const stripped = stripping(contract, write);
157
- const keepsAll = (output: A): Written<A> => ({ output, lost: [] });
158
- if (!Excess.choosing(contract.ast))
159
- return (input: Input<S>) => Effect.map(stripped(input), keepsAll);
160
156
  const decoded = toDecoded(contract);
157
+ const written = (
158
+ reading: ReturnType<typeof decoded>,
159
+ options?: SchemaAST.ParseOptions,
160
+ ) =>
161
+ Effect.flatMap(reading, (value) =>
162
+ Effect.map(write(value, options), (output): Written<A> => ({
163
+ output,
164
+ lost: [],
165
+ })),
166
+ );
167
+ if (!Excess.choosing(contract.ast))
168
+ return (input: Input<S>) => written(decoded(input));
161
169
  const exact = toDecoded(contract, Excess.strict);
162
- const kept = (input: Input<S>) =>
163
- Effect.flatMap(exact(input), (value) => write(value, Excess.strict));
170
+ const kept = (input: Input<S>) => written(exact(input), Excess.strict);
164
171
  const compared = (input: Input<S>, given: unknown) =>
165
172
  Effect.flatMap(decoded(input), (value) =>
166
173
  Effect.map(
@@ -184,12 +191,9 @@ export const reader = <S extends Schema.Top, A, R>(
184
191
  given === input ? input : Excess.exposed([contract.ast], input)
185
192
  ) as Input<S>;
186
193
  return Effect.catch(
187
- Effect.map(
188
- given === input
189
- ? kept(input)
190
- : Effect.catch(kept(given as Input<S>), () => kept(seen)),
191
- keepsAll,
192
- ),
194
+ given === input
195
+ ? kept(input)
196
+ : Effect.catch(kept(given as Input<S>), () => kept(seen)),
193
197
  () => compared(input, given),
194
198
  );
195
199
  };
@@ -274,6 +274,14 @@ export const lost = (
274
274
  );
275
275
  };
276
276
 
277
+ /** Only stable absent/data properties prove the caller did not supply this field. */
278
+ const omitted = (input: object, name: string): boolean => {
279
+ const property = Object.getOwnPropertyDescriptor(input, name);
280
+ return property === undefined
281
+ ? !(name in input)
282
+ : "value" in property && property.value === undefined;
283
+ };
284
+
277
285
  /**
278
286
  * Read what a schema says about its group, and build what it sends.
279
287
  *
@@ -300,6 +308,45 @@ export const bind = <S extends Schema.Constraint>(contract: S, fold: Fold) => {
300
308
  required: new Set(Array.from(reading.required, fold)),
301
309
  chooses,
302
310
  };
311
+ // A group transformation or declaration can change names or construct in its own way.
312
+ const directNames =
313
+ contract.ast._tag === "Objects" && contract.ast.encoding === undefined
314
+ ? contract.ast.propertySignatures.flatMap(({ name }) =>
315
+ typeof name === "string" ? [name] : [],
316
+ )
317
+ : [];
318
+ /** Snapshot caller omission before preparation can run a default or encoder. */
319
+ const candidates = (
320
+ input: unknown,
321
+ edits: ReadonlySet<string> | undefined,
322
+ ): ReadonlyArray<string> =>
323
+ edits !== undefined && typeof input === "object" && input !== null
324
+ ? directNames.filter(
325
+ (name) => edits.has(fold(name)) && omitted(input, name),
326
+ )
327
+ : [];
328
+ /** Preparation supplied these omitted, edited fields in the record it actually writes. */
329
+ const filled = (
330
+ names: ReadonlyArray<string>,
331
+ output: unknown,
332
+ ): ReadonlyArray<string> => {
333
+ if (
334
+ typeof output !== "object" ||
335
+ output === null ||
336
+ Symbol.iterator in output
337
+ )
338
+ return [];
339
+ return names
340
+ .filter((name) => {
341
+ const encoded = Object.getOwnPropertyDescriptor(output, name);
342
+ return (
343
+ encoded?.enumerable === true &&
344
+ "value" in encoded &&
345
+ encoded.value !== undefined
346
+ );
347
+ })
348
+ .map(fold);
349
+ };
303
350
  /**
304
351
  * The caller's fields this schema took: what it wrote, and any it states and the caller gave,
305
352
  * which may be written under another name or not at all. What it would lose is not among them.
@@ -355,5 +402,5 @@ export const bind = <S extends Schema.Constraint>(contract: S, fold: Fold) => {
355
402
  through(),
356
403
  );
357
404
  };
358
- return { shape, settle, taken };
405
+ return { shape, settle, taken, candidates, filled };
359
406
  };