Effect-TS / effect

An ecosystem of tools to build robust applications in TypeScript
https://effect.website
MIT License
6.45k stars 205 forks source link

From Discord: Schema Partial with Nullable Option #3112

Open effect-bot opened 3 weeks ago

effect-bot commented 3 weeks ago

Summary

The conversation revolves around the need to simplify the process of making schema properties optional and nullable in the Effect-TS ecosystem. Jesse Kelly mentions using Schema.partial(_, { nullable: true, as: "Option" }) to avoid manually writing Schema.optional(x, { nullable: true, as: "Optional" }) for each property, which can be cumbersome. Tylor agrees and mentions that he often uses a helper for the optional variant. Jesse Kelly further elaborates that being able to define the Struct.optional configuration for every property at once would reduce boilerplate, especially for entirely partial structures or when dealing with separate RequiredProps and OptionalProps structs.

Key Takeaways:

  1. There is a need to simplify making schema properties optional and nullable.
  2. Manually writing optional and nullable configurations for each property is cumbersome.
  3. Helpers or utilities to handle this configuration can reduce boilerplate.
  4. Defining the optional configuration for all properties at once is desirable for efficiency.

Discord thread

https://discord.com/channels/795981131316985866/1255872346267451402

jessekelly881 commented 3 weeks ago

In short I think it would make sense for Schema.partial() to convert the fields to PropertySignatures if they aren't already. If the prop is already a property signature I would expect that the prop specific def would take precedence in order to allow assigning defaults, etc. In general however I would expect

Schema.Struct({
    a: Schema.Number
}).pipe(Schema.partial({ nullable: true }))

to be equivalent to

Schema.Struct({
    a: Schema.optional(Schema.Number, { nullable: true })
})