Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getDefaults returns undefined for optional with a default of empty string or false #356

Closed
dboune opened this issue Jan 7, 2024 · 2 comments
Assignees
Labels
bug Something isn't working priority This has priority workaround Workaround fixes problem

Comments

@dboune
Copy link

dboune commented Jan 7, 2024

Problem

getDefaults returns undefined for an optional with a default set to an empty string or false. I believe it should return the value supplied.

Note: This same issue applies to getFallbacks with fallback. In all cases, parse does the right thing.

Example:

import { object, optional, string, getDefaults } from 'valibot';

const Schema = object({
  field: optional(string(), ''),
});

const schemaDefaults = getDefaults(Schema)

console.log(schemaDefaults);

I believe the reason for the current behavior may be here:
https://github.com/fabian-hiller/valibot/blob/6578c3304062e6c2e5cb14fc0f4d6dbb1e6eaffb/library/src/methods/getDefaults/getDefaults.ts#L34C3-L34C24

Rationale

I'm currently evaluating a move from TypeBox to Valibot for form validation because dealing with the validation of unions in TypeBox is complex, and potentially maintaining two schemas for input and output is undesirable. We use Value.Create(Schema) to obtain the initial state of our form from our "single source of truth" about the shape of the form state object. I was attempting to re-create this with Valibot.

In React + MUI, we cannot (or should not) set the initial value of our form fields to undefined unless we intend to operate them uncontrolled, which is discouraged.

Workaround

Using a function to supply the default value renders the desired result.

import { object, optional, string, getDefaults } from 'valibot';

const Schema = object({
  field: optional(string(), () => ''),
});

const schemaDefaults = getDefaults(Schema)

console.log(schemaDefaults);

Misc

Valibot looks very nice overall. Thank you!

@dboune dboune changed the title getDefaults returns undefined for optional with a default of empty string getDefaults returns undefined for optional with a default of empty string or false Jan 7, 2024
@fabian-hiller fabian-hiller self-assigned this Jan 8, 2024
@fabian-hiller fabian-hiller added bug Something isn't working priority This has priority workaround Workaround fixes problem labels Jan 8, 2024
@fabian-hiller
Copy link
Owner

Thanks for reporting this bug. I will change the if-statement to schema.default !== undefined and release this change with the next version. Until then, a function can be used as a workaround when passing empty strings.

@fabian-hiller
Copy link
Owner

v0.26.0 is available

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working priority This has priority workaround Workaround fixes problem
Projects
None yet
Development

No branches or pull requests

2 participants