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

[feat]: Parse schema with partial fallback values #155

Closed
hermanseder opened this issue Sep 11, 2023 · 12 comments
Closed

[feat]: Parse schema with partial fallback values #155

hermanseder opened this issue Sep 11, 2023 · 12 comments
Assignees
Labels
enhancement New feature or request

Comments

@hermanseder
Copy link

hermanseder commented Sep 11, 2023

It would be nice to have a functionality to parse a schema with partial fallback values.

For example, that this input:

const schema = object({
  name: fallback(string(), 'test'),
  age: number(),
});

console.log(parse(schema, {}));

results in this output:

{
   age: undefined
   name: 'test'
}

Or is such a scenario already possible by combining multiple functions together?

As idea, I implemented here a custom version of partial, which uses fallback instead of optional:
https://stackblitz.com/edit/valibot-partial-fallback-example

@fabian-hiller
Copy link
Owner

Maybe with optional? If not, please describe in more detail what behavior you are looking for. I don't quite understand it yet.

const schema = object({
  name: optional(string(), 'test'),
  age: number(),
});

@hermanseder
Copy link
Author

My goal is to extract all default values. Your suggestion changes on the one hand the typing of the inferred model. On the other hand, it is not possible to parse it, because the property age is missing.

I want to get an object which includes just the fallback values and the type should be Partial<Output<typeof schema>>.

@fabian-hiller
Copy link
Owner

For what exactly do you need the fallback values? Technically this is possible, but not that easy. Especially it is difficult to make it type safe.

@hermanseder
Copy link
Author

hermanseder commented Sep 12, 2023

My intention is, that I can parse a schema, where some of the properties have a fallback value, and some not. From this schema I want to extract, only the properties which has a fallback value, into an object. All other properties, which have no fallback value, should be undefined in the extracted object.

As background, I want to use Valibot that different components can define in a generic way their required configuration schema. A further requirement is, that these component can also define a partial default value, and therefore I need to extract these values. Another solution would be to separate the schema and the default configuration. But I thought it would be nice to combine them into one (valibot) configuration object.

Your suggested solution with optional does not work, because if I call parse(schema) on that schema, the library says that age is missing. Or have I overlooked an additional option/setting which I can use?

@fabian-hiller
Copy link
Owner

parse(schema) must throw an error, and in this case must not return undefined for values with no fallback or default value. The behavior you are looking for is not currently supported by default. However, you can implement it yourself using optional and a custom getDefaults function. With optional, nullable and nullish you can access the default value with .default or getDefault. So getDefaults must check if .default exists and return the value or undefined as appropriate.

@hermanseder
Copy link
Author

Thanks for your detailed explanation. Then I will implement a custom logic in my project. I thought, such a functionality could maybe be helpful for other users.

@fabian-hiller
Copy link
Owner

Yes, feel free to share your implementation. Maybe I will integrate it in getDefault or add another method called getDefaults.

@fabian-hiller fabian-hiller self-assigned this Sep 16, 2023
@fabian-hiller
Copy link
Owner

Were you able to find a solution to your problem?

@fabian-hiller fabian-hiller added the enhancement New feature or request label Sep 17, 2023
BThomann added a commit to BThomann/valibot that referenced this issue Sep 18, 2023
Creates a default object from a given object schema.
The object will contain possible default values that could have been derived from default or fallback values.
@BThomann
Copy link

I created a PR (#169) for that feature. Happy to hear what you think about it!

@fabian-hiller
Copy link
Owner

I have implemented getDefaults. The API will be available in the next version.

@fabian-hiller
Copy link
Owner

v0.20.0 is now available

@BThomann
Copy link

Awesome thank you! In the end the getFallbacks was exactly what we have been looking for!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants