-
-
Notifications
You must be signed in to change notification settings - Fork 216
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
design discussion: toSnakeCase
, toCamelCase
& related actions
#1010
Comments
toSnakeCase
, toCamelCase
and related transformation actionstoSnakeCase
, toCamelCase
& related actions
@krokyze Feel free to share your opnion. |
To be honest, I’d be happier to see something like #163 implemented, as that aligns more closely with my experience using Kotlin and Dart data serialization libraries. In my view, it also offers better performance in several scenarios. That said, my TypeScript experience is somewhat limited, so I could be mistaken.
Having control over which specific values should be transformed, like
For example, after implementing the current recommended solution, nesting is handled automatically, which is advantageous but comes with a caveat. In my project, both Product.ts and User.ts models utilize v.pipe() with toCamelCase functionality. In various scenarios, these models are parsed separately; however, the Product model includes User as a nested value. So toCamelCase is applied twice to the User value within Product, potentially impacting performance. Also It would be great if it supported "Go to Definition," which is currently broken when using |
Thank you Elton! My initial thought was to keep it simple and not allow passing arguments as we can always extend it later. After reading Martin's comment, I think we should investigate how such a feature affects the implementation in terms of performance and bundle size. When it comes to whether we should transform nested keys, I tend to not transform them. This gives our users more control and simplifies the implementation, but I am aware of the pros and cons of both decisions. We could also mark such actions as One last thing we need to investigate is what the action should do in the following cases when a snake case key of a camel case key already exists. const Schema = v.pipe(
v.object({ someKey: v.string(), some_key: v.number() }),
v.toSnakeCase()
); I think we have two choices. We can overwrite it, or we can skip it. The important thing is that our runtime implementation matches our type implementation. That's probably a rare case. So it is not too important how we decide, and maybe the more practical choice is just to choose the simpler implementation. |
I think it would be a bit difficult and not ideal to implement something like #163 for this feature, as Valibot prefers users to define the input schema and then transform. By Example 1 and Example 2, I meant both cases will be supported. I assume my initial message was not clear. I apologize. Related to the concern of passing the entire object through the pipe to transform one or more keys (Example 2 of my initial message): I think it is something the pipeline approach favors. One way to think about it is that the pipeline has more information than the individual schemas mapped to a key inside the object. This makes the pipe an ideal place to transform the object safely. |
@krokyze and @fabian-hiller thanks for providing your inputs! I'll implement and investigate some of the concerns mentioned in the discussion. Will keep everyone posted. |
I need everyone's opinion on these questions:
|
The most complex part is probably the key transformation in TypeScript. |
For points 2 and 3, I favor providing customizable actions. But I also don't like the idea of adding complexity to provide customization capabilities that might not be used at all. I think it's sensible to mark such actions as So I'll follow @fabian-hiller suggestions if there are no additional comments. |
Sounds good 👍 |
Regarding this statement I tend to disagree, because this will probably be impossible in some scenarios or really really hard. |
@michachen Can you provide an example that might explain the difficulties faced? |
As concluded in #981, adding
toSnakeCase
-like transformation actions is a good idea. I have created this issue to discuss the design of the actions. Once the design is finalized, I'll work on adding the actions. The initial requirements that affect the design of the actions are:Should be flexible enough to exclude or include keys of the object
Example 1: Without any key (transforms all of the keys)
Example 2: With one or more keys (transforms only the listed keys)
Should not affect the keys in any nested object
This requirement adds more control over which keys should be transformed. Every nested object will be associated with a Valibot schema. If a user wants to transform the keys of a nested object, the user can use the nested object's schema as shown in the example below.
I created the initial requirements based on what I felt was sensible. Feel free to discuss so that we can change the requirements (if required) and finalize the design.
The text was updated successfully, but these errors were encountered: