ianstormtaylor / superstruct

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

Create predefined JS object from Schema #1239

Closed michalCapo closed 1 month ago

michalCapo commented 2 months ago

This should be as core functionality. Function make should create JS object with default values given by schema.

const Some = object({
  age: number(),
  name: string(),
  list: array(string()),
})

const obj = Some.make()

assert(obj.age === Infinity)
assert(obj.name === "")
assert(obj.list.length === 0)
yeoffrey commented 1 month ago

Hey @michalCapo 👋

For a feature request, please add some more information. For example, this could end up being a bit weird. Do structs have default values, why should a string default to ""? I think theres some extra details that would need to be ironed out, but in general I support this idea.

michalCapo commented 1 month ago

Hi, we use superstruct for everything. We are creating database structure from schema, also we use struct schema for generation sql queries, generating object for our forms and afterwards validating input before schema is stored into db. To be honest i'm fine with structs defaults, this was only idea "stolen" from golang, to avoid null or undefined. So primitives should be defaulted to "" or 0 or false, but it's just an idea.

yeoffrey commented 1 month ago

Thanks for the info! Do you mind bring this convo into the discussions area? As we work on pruning some of the issues, we've decided that feature requests should end up in the discussion board. Then we can go from there?

michalCapo commented 1 month ago

OK, what i need to do?

yeoffrey commented 1 month ago

Just go here and open a discussion item with the type idea.

image