Create object from schema with default values #1253
michalCapo
started this conversation in
Ideas
Replies: 1 comment 3 replies
-
Hi @michalCapo -- thank you for the suggestion! I think it would be pretty hard to set default values that people would universally agree on. However, you can achieve the equivalent functionality to the above with defaults. // Defaults on individual fields
const Some = object({
age: defaulted(number(), Infinity),
name: defaulted(string(), ""),
list: defaulted(array(string()), () => []),
});
Some.create({}) // or create({}, Some)
// Default on the whole object
const Some2 = defaulted(
object({
age: number(),
name: string(),
list: array(string()),
}),
() => ({ age: Infinity, name: "", list: []})
)
Some2.create(undefined) // or create(undefined, Some) We could consider making the the first argument to |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
This should be as core functionality. Function make should create JS object with default values given by schema.
Beta Was this translation helpful? Give feedback.
All reactions