ianstormtaylor / superstruct

A simple and composable way to validate data in JavaScript (and TypeScript).
https://docs.superstructjs.org
MIT License
7.02k stars 224 forks source link

Feature Request: Add depth Option to partial for Nested Optional Attributes #1266

Open yeoffrey opened 3 months ago

yeoffrey commented 3 months ago

Description

It would be beneficial to enhance the partial function in the superstruct library to support a depth option. This option would allow attributes to be made optional up to a specified level of nested objects.

Example

import { number, object, optional, partial, string } from 'superstruct'

const MyStruct = object({
  name: string(),
  address: object({
    street: string(),
    number: number(),
  }),
})

const PartialMyStruct = partial(MyStruct, { depth: 1 })

// The same as:
// const PartialMyStruct = optional(
//   object({
//     name: optional(string()),
//     address: optional(
//       object({
//         street: string(),
//         number: number(),
//       })
//     ),
//   })
// )