superstruct icon indicating copy to clipboard operation
superstruct copied to clipboard

[Feature] Support for `Required`

Open kevcao-certik opened this issue 1 year ago • 2 comments

As we have support for Typescript's Partial, can we also have support for Required as well?

kevcao-certik avatar Apr 17 '23 18:04 kevcao-certik

Makes sense to me! Very open to a PR if anyone wants to take this on.

arturmuller avatar Dec 18 '23 10:12 arturmuller

Just some thoughts I had about this feature.

I think in general SuperStruct wants its user to start with the most restrictive validation, and then gradually loosen it with optional and nullable, etc... Adding required here might become quite confusing.

For example, if we require something that is optional, do we just negate it? What about for partial? To me, the below seems quite awkward.

import { number, object, optional, partial, required } from '../../../src'

export const Struct = required(optional(number()))

export const Struct2 = required(
  partial(
    object({
      thing: number(),
    })
  )
)

The other consideration here is that if we do allow this, then we might end up with a huge callback problem. I've already run into it while trying to test my own implementation of this, but essentially under the hood this is going to make a struct which for each check, will make things optional, then make this required, then optional, then required... Unless we can go into the struct itself and have it simplify itself, I'm worried about the performance of this.

So I can't really see the reason for having this when we could just start in the first place with everything required and loosen it up with modifiers.

yeoffrey avatar Jun 03 '24 21:06 yeoffrey