vinejs / vine

VineJS is a form data validation library for Node.js

Home Page:https://vinejs.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Default values

guillaumervls opened this issue Β· comments

Package version

1.7.1

Describe the bug

Hi and thanks for the work on VineJS!

Is there a way to add a transform/mutation to specify a default value when something is optional / undefined?

Like Zod's .default:

const stringWithDefault = z.string().default("tuna");
stringWithDefault.parse(undefined); // => "tuna"

Cheers!

Reproduction repo

No response

Hey @guillaumervls! πŸ‘‹πŸ»

Use the parse() method.

vine.string().parse(v => v ?? 'Unknown')

Thanks for your answer @RomainLanz! πŸ™

(BTW, feel free to close/keep this issue depending on whether you're considering adding a dedicated function or not.)

The parse method is a bit more verbose, but it is also way more powerful, so we are not going to extend Vine's API to add a new default method.

Closing. πŸ‘πŸ»