Skip to content

Add support for defaults at schema level #207

@DawidWraga

Description

@DawidWraga

Support defining property default values at the schema level, for more concise inserts and avoiding repetition.

Example:

const todoSchema = z.object({
  id: z.string().default(()=> crypto.randomUUID()),
  text: z.string().default(""),
  completed: z.boolean().default(false),
  createdAt: z.coerce.date().default(()=> new Date()),
  updatedAt: z.coerce.date().default(()=> new Date()),
})

So that you can simply write:

todoCollection.insert({
                text: newTodoText,
              });

Instead of:

  todoCollection.insert({
                // Random temporary id.
                id: Math.floor(Math.random() * 1000000),
                text: newTodoText,
                completed: false,
                created_at: new Date(),
                updated_at: new Date(),
              });

I think only needed for inserts.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions