MarcinOrlowski / laravel-api-response-builder

Builds nice, normalized and easy to consume REST JSON responses for Laravel powered APIs.
MIT License
721 stars 79 forks source link

errors key for validation #227

Open ghost opened 2 years ago

ghost commented 2 years ago

I'm trying to create a validation error with this package. can you generate a new key for the error? I'm not comfortable entering validation error data into the key data.

{
    "success": false,
    "code": 422,
    "locale": "en",
    "message": "validation error, please check the input form",
    "data" : null,
    "errors": {
        "email": [
            "The email field is required."
        ],
        "password": [
            "The password field is required."
        ]
    }
}

LoginRequest.php
    protected function failedValidation(Validator $validator)
    {
        $response = RB::asError($apiCode)->withError($validator->errors()->toArray())->withMessage($msg)->build();
        throw new HttpResponseException($response);
    }

or more simply by adding a key parameter to replace the key data.
withData($errors, "errors")->build();
MarcinOrlowski commented 1 year ago

Sorry for late reply but I am having troubles understanding your request. Can you please elaborate (if that still matters)?