fix(session): omit undefined optional fields (#24676)

This commit is contained in:
Kit Langton
2026-04-27 17:50:09 -04:00
committed by GitHub
parent 576efed196
commit c4a2353ac3
4 changed files with 93 additions and 40 deletions

View File

@@ -1,4 +1,5 @@
import { Schema } from "effect"
import { Option, Schema, SchemaGetter } from "effect"
import { zod, ZodOverride } from "./effect-zod"
/**
* Integer greater than zero.
@@ -10,6 +11,19 @@ export const PositiveInt = Schema.Int.check(Schema.isGreaterThan(0))
*/
export const NonNegativeInt = Schema.Int.check(Schema.isGreaterThanOrEqualTo(0))
/**
* Optional public JSON field that accepts explicit `undefined` internally but
* encodes it as an omitted key, matching `JSON.stringify` legacy responses.
*/
export const optionalOmitUndefined = <S extends Schema.Top>(schema: S) =>
Schema.optionalKey(schema).pipe(
Schema.decodeTo(Schema.optional(schema), {
decode: SchemaGetter.passthrough({ strict: false }),
encode: SchemaGetter.transformOptional(Option.filter((value) => value !== undefined)),
}),
Schema.annotate({ [ZodOverride]: zod(schema).optional() }),
)
/**
* Strip `readonly` from a nested type. Stand-in for `effect`'s `Types.DeepMutable`
* until `effect:core/x228my` ("Types.DeepMutable widens unknown to `{}`") lands.