Skip to content

Proposal: add request data types #40

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

DonnieRich
Copy link

@DonnieRich DonnieRich commented Apr 13, 2025

Description

In this PR I added the logic to add a type based on the validation rules applied by the Form Request Validation.

Logic

Now in the GenerateCommand following methods (writeNamedMethodExport, writeControllerMethodExport, writeMultiRouteControllerMethodExport) there is a new field called request. It's an array created by the controllerMethodRequest method from the Route class.

The controllerMethodRequest check if the controller method as a FormRequest as one of its parameters. In this case it invoke the rules method and create a new instance of the Request class to parse all the fiels and rules returned.

The Request class applies some logic to select what should be the right types for each field.

As an example, the following validation rules:

class UpdatePostRequest extends FormRequest
{
  public function rules(): array
      {
          return [
              'name' => ['required', 'string', 'max:255'],
              'description' => ['required', 'string'],
              'price' => ['required', 'numeric'],
              'stock' => ['required', 'integer', 'between:2,33'],
              'hidden' => ['boolean', Rule::when(true, '')],
              'catalog_id' => ['required', 'integer', 'exists:catalogs,id'],
              'slug' => ['string', 'max:255', 'unique:products', Rule::excludeIf(true)],
              'image' => ['nullable', 'sometimes', File::image()->dimensions(Rule::dimensions()->maxWidth(1024)->maxHeight(1024))->min('1kb')->max('500kb')],
              'code' => ['required', 'string', 'max:255', 'unique:products'],
              'category_id' => ['required']
          ];
      }
}

Are converted into:

export type UpdatePostRequest = {
    name: string
    description: string
    price: number
    stock: number
    hidden?: boolean | 1 | 0 | '1' | '0'
    catalog_id: number
    slug: string
    image?: null | File
    code: string
}

Help wanted (and appreciated)

It would be useful to double check the types derived from the validation rules and the general logic.
I also tried to write a couple of test but testing only types declarations seems to be a bit tricky, so here someone with more experience than me could surely be of great help.

@joetannenbaum
Copy link
Collaborator

Hey this is great stuff, thank you! I also tinkered with this last week, if we decide to move forward I'll merge my work with yours and see what comes out the other side. Marking as a draft for now until we decide if we're going to pursue.

@joetannenbaum joetannenbaum marked this pull request as draft April 22, 2025 17:59
@DonnieRich
Copy link
Author

Thanks to you man 😁
Yeah, no problem! I wasn't sure you wanted to add this feature, so I'm happy to know you were thinking about it too 😅

Should you need any help I'll gladly lend a hand 😁

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

Successfully merging this pull request may close these issues.

2 participants