Laravel 5.8 Validation Race Condition

Viewed 296

I have this validation rule on POST request method inside a controller:

class CreateOrderController extends Controller
{
    public function create(Request $request)
    {
        $this->validate($request, [
            'store_id' => $request->order_type === OrdersTypeConstants::P2P  ? "" : "required|" . 'exists:stores,id',
            'p2p_type' => [Rule::in([OrdersTypeConstants::P2PCOURIER, OrdersTypeConstants::P2PPURCHASE])],
            'items' => 'required_if:p2p_type,'.OrdersTypeConstants::P2PPURCHASE.'|array',
            'items.*.id' => 'nullable|numeric',
            'items.*.quantity' => 'nullable|integer|min:1',
            'items.*.adjustment' => 'nullable|numeric',
            'items.*.image' => 'nullable|string',
            'items.*.addons' => 'array',
            'items.*.reward_applied' => 'boolean',
            'items.*.replacement_strategy.type' => [
                'string',
                Rule::in([ItemReplacementStrategyConstants::REMOVE,ItemReplacementStrategyConstants::BEST_MATCH, ItemReplacementStrategyConstants::SPECIFIC])
            ],
            'items.*.replacement_strategy.quantity' => 'integer|min:1',
            'items.*.replacement_strategy.item_id' => 'numeric',
            'address_id' => 'exists:addresses,id,user_id,' . $client_id,
            'address_id_p1' => 'exists:addresses,id,user_id,' . $client_id,
            'use_cash_deposit' => 'boolean',
        ]);

Sometime it returns The store id field is required even if it is actually being sent as you can see here in the error log: enter image description here

It is only happening randomly -not consistently- only on production environment, reported only on firebase.

Why could that be happening?

2 Answers

Happened to me several times. All the times the problem was an invalid JSON. You can validate yours here.

When Laravel encounters an invalid JSON, it just ignores it, and assumes the payload is empty.

The fact that it gives you a validation error on store_id is required is not a coincidence. Your framework is setup to stop on first failed rule, and this rule just happens to be store_id => 'required'.

Check if request()->all() has any data (your json could be invalid)

Related