andreaselia / laravel-api-to-postman

Generate a Postman collection automatically from your Laravel API
MIT License
958 stars 99 forks source link

Not support custom rules? #71

Closed sunxyw closed 2 years ago

sunxyw commented 2 years ago

Environment:

Laravel 9.14.1
PHP 8.1.6
laravel-api-to-postman 1.12.0

When executing php artisan export:postman with enable_formdata = true it throws an error: 圖片

It works well when enable_formdata = false.

Here is my request and rule:

class PasswordUpdateRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules(): array
    {
        return [
            'current_password' => [
                'required',
                'string',
                new CurrentPasswordRule(),
            ],
            'password'         => [
                'required',
                'confirmed',
                'min:8',
                new WeakPasswordRule(),
            ],
        ];
    }
}
class CurrentPasswordRule implements Rule
{
    /**
     * Determine if the validation rule passes.
     *
     * @param string $attribute
     * @param mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        return Hash::check($value, auth()->user()->password);
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return __('Your current password is not valid');
    }
}

Does this package not yet support custom rules? Or am I doing something wrong?

andreaselia commented 2 years ago

Hi @sunxyw, thank you for reporting this issue.

I believe I've managed to reproduce this in #72, so will work on a fix when I get a little time. If you have any suggestions towards a fix, feel free to message over on the PR 😄

(cc: @tomirons)

andreaselia commented 2 years ago

72 stops this throwing an error, we still don't have support for custom rules at this moment in time though.

sunxyw commented 2 years ago

This error still presents even updated to the latest version v1.12.1.

圖片

andreaselia commented 2 years ago

Weird, I thought the test case would have caught this. I’ll take a look at this when I get back home in a few hours.

cc: @tomirons

sunxyw commented 2 years ago

I tried fixing it myself, maybe it wasn't the best way, but it did get rid of the error.

https://github.com/andreaselia/laravel-api-to-postman/blob/352cc1b190c0f5a4ed8ced4a12bb25725a2e410b/src/Commands/ExportPostmanCommand.php#L329-L330

if (is_array($rules)) {
    foreach ($rules as $i => $rule) {
        if (is_object($rule)) {
            unset($rules[$i]);
        }
    }
    $this->validator = Validator::make([], [$attribute => implode('|', $rules)]);
andreaselia commented 2 years ago

Hi @sunxyw,

I'll be taking a look at this today, I had a bit of a busy day yesterday so didn't get round to it.

Thanks for posting your solution. I'll need to be able to reproduce this through a unit test before I can submit a fix though.

Do you have any ideas how we could maybe reproduce it in a unit test?

andreaselia commented 2 years ago

Okay, I managed to reproduce it. It's when a rule class is within an array of rules.

sunxyw commented 2 years ago

Good, please take your time, this is not in a hurry.

andreaselia commented 2 years ago

This has now been resolved in the latest release. Thanks again for your help on the issue.