Description
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)const Schema = v.pipe( v.object({ productCount: v.number(), someKey: v.string() }), v.toSnakeCase() ); /* Output type: { product_count: number; some_key: string; } */
Example 2: With one or more keys (transforms only the listed keys)
const Schema = v.pipe( v.object({ productCount: v.number(), someKey: v.string() }), v.toSnakeCase(["someKey"]) ); /* Output type: { productCount: number; some_key: string; } */
-
Should not affect the keys in any nested object
const Schema = v.pipe( v.object({ someK1: v.object({ someNestedKey: v.string() }), someK2: v.string(), }), v.toSnakeCase() ); /* Output type: { some_k1: { someNestedKey: string }; some_k2: string; } */
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.
const Schema = v.pipe( v.object({ someK1: v.pipe( v.object({ someNestedKey: v.string() }), v.toSnakeCase(), ), someK2: v.string(), }), v.toSnakeCase(), ); /* Output type: { some_k1: { some_nested_key: string }; some_k2: string; } */
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.