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

Parsing from snake_case to camelCase #981

Closed
krokyze opened this issue Dec 13, 2024 · 5 comments
Closed

Parsing from snake_case to camelCase #981

krokyze opened this issue Dec 13, 2024 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@krokyze
Copy link

krokyze commented Dec 13, 2024

Hey.

Is it possible to define the key for specific entries like productCount: v.pipe(v.key('product_count'), v.number(), v.integer()) so it would be parsed as product_count into productCount?

Or maybe something similar like yup which has camelCase().

@fabian-hiller
Copy link
Owner

fabian-hiller commented Dec 19, 2024

Great idea! Thank you for creating this issue. I think this only works in the pipeline of your object. But it will be challenging to get the types right. A package like ts-case-convert might help. We could add a toCamelCase and toSnakeCase action. Here is a simple code example:

import { objectToSnake } from 'ts-case-convert';
import * as v from 'valibot';

// With `ts-case-convert`
const Schema = v.pipe(
  v.object({ productCount: v.number() }),
  v.transform(objectToSnake)
);

// With a `toSnakeCase` action (not implemented yet)
const Schema = v.pipe(
  v.object({ productCount: v.number() }),
  v.toSnakeCase()
);

@fabian-hiller fabian-hiller self-assigned this Dec 19, 2024
@fabian-hiller fabian-hiller added the question Further information is requested label Dec 19, 2024
@krokyze
Copy link
Author

krokyze commented Dec 19, 2024

Thanks for the recommendation!

The only issue I see when using ts-case-convert is that the "Go to Definition" feature in VSCode doesn't work, but I'm fine with that. TypeScript hinting for non-existent properties works fine. Here's a video demonstrating the issue:

Screen.Recording.2024-12-19.at.20.06.05.mov

That said, how should I implement this for a recursive schema? I'm encountering some issues:

Type 'SchemaWithPipe<[ObjectSchema<{ readonly id: SchemaWithPipe<[NumberSchema<undefined>, IntegerAction<number, undefined>]>; readonly title: StringSchema<undefined>; readonly link: StringSchema<...>; readonly product_count: NumberSchema<...>; readonly children: LazySchema<...>; }, undefined>, TransformAction<...>]>' is not assignable to type 'GenericSchema<CategoryRecursive, CategoryRecursive, BaseIssue<unknown>>'.
  Types of property ''~standard'' are incompatible.
    Type 'StandardSchemaProps<{ id: number; title: string; link: string; product_count: number; children: CategoryRecursive[]; }, { id: number; title: string; link: string; productCount: number; children: { ...; }[]; }>' is not assignable to type 'StandardSchemaProps<CategoryRecursive, CategoryRecursive>'.
      Property 'product_count' is missing in type '{ id: number; title: string; link: string; productCount: number; children: { id: number; title: string; link: string; productCount: number; children: ...[]; }[]; }' but required in type 'CategoryRecursive'.

Screenshot 2024-12-19 at 20 25 02

@fabian-hiller
Copy link
Owner

Here is an example:

import { objectToSnake, ObjectToSnake } from "ts-case-convert";
import * as v from "valibot";

type InputType = {
  fooBar: number;
  nested: InputType;
};

type OutputType = ObjectToSnake<InputType>;

const Schema: v.GenericSchema<InputType, OutputType> = v.pipe(
  v.object({
    fooBar: v.number(),
    nested: v.lazy(() => Schema),
  }),
  v.transform(objectToSnake)
);

type Input = v.InferInput<typeof Schema>;
type Output = v.InferOutput<typeof Schema>;

@fabian-hiller fabian-hiller added enhancement New feature or request and removed question Further information is requested labels Dec 26, 2024
@fabian-hiller
Copy link
Owner

Probably after v1 is out we will investigate the implementation of a toSnakeCase and toCamelCase transformation action.

@krokyze
Copy link
Author

krokyze commented Jan 8, 2025

Thanks! Works as expected!

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

2 participants