Skip to content

Generate object with empty values matching Zod schema

Notifications You must be signed in to change notification settings

Ced-Sharp/zod-defaults

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

npm size libera manifesto

Zod Defaults

A Zod utility library to generate default values for Zod schemas.

Preface

This is not an official Zod library. This is a personal project which is mainly meant to be used by my other projects. However, you are free to use it if you find it useful.

This library is currently in development and not all functionality from zod is supported yet.

Requirements

This package expects Zod to be installed.

Install

Using npm (or yarn/pnpm/bun):

npm install zod-defaults

Usage

Create a schema and call the getDefaultsForSchema function to generate an object filled with the default or empty values matching your schema.

import {z} from 'zod';
import {getDefaultsForSchema} from 'zod-defaults';

const schema = z.object({
    name: z.string().default('John Doe'),
    age: z.number(),
    isStudent: z.boolean(),
});

const person = getDefaultsForSchema(schema);

console.log(person);
// { name: 'John Doe', age: 0, isStudent: false }

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

Generate object with empty values matching Zod schema

Resources

Stars

Watchers

Forks

Packages

No packages published