Skip to content

Commit 071a0f0

Browse files
authored
Merge pull request #209 from fleetbase/fix/driver-vehicle-validation-error
fix: Resolve vehicle from uuid, public_id, or object when creating/updating driver
2 parents 2a6178e + b50d622 commit 071a0f0

File tree

6 files changed

+117
-4
lines changed

6 files changed

+117
-4
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fleetbase/fleetops-api",
3-
"version": "0.6.36",
3+
"version": "0.6.37",
44
"description": "Fleet & Transport Management Extension for Fleetbase",
55
"keywords": [
66
"fleetbase-extension",

extension.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Fleet-Ops",
3-
"version": "0.6.36",
3+
"version": "0.6.37",
44
"description": "Fleet & Transport Management Extension for Fleetbase",
55
"repository": "https://github.com/fleetbase/fleetops",
66
"license": "AGPL-3.0-or-later",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fleetbase/fleetops-engine",
3-
"version": "0.6.36",
3+
"version": "0.6.37",
44
"description": "Fleet & Transport Management Extension for Fleetbase",
55
"fleetbase": {
66
"route": "fleet-ops"

server/src/Http/Controllers/Internal/v1/DriverController.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ public function createRecord(Request $request)
4545
{
4646
$input = $request->input('driver');
4747

48+
// Normalize vehicle field - the frontend may send the full vehicle object,
49+
// a UUID string, or a public_id string. Normalize to a single identifier
50+
// so the ResolvableVehicle rule can validate it correctly.
51+
if (isset($input['vehicle']) && is_array($input['vehicle'])) {
52+
$input['vehicle'] = data_get($input['vehicle'], 'id')
53+
?? data_get($input['vehicle'], 'public_id')
54+
?? data_get($input['vehicle'], 'uuid')
55+
?? null;
56+
$request->merge(['driver' => $input]);
57+
}
58+
4859
// create validation request
4960
$createDriverRequest = CreateDriverRequest::createFrom($request);
5061
$rules = $createDriverRequest->rules();
@@ -262,6 +273,17 @@ public function updateRecord(Request $request, string $id)
262273
// get input data
263274
$input = $request->input('driver');
264275

276+
// Normalize vehicle field - the frontend may send the full vehicle object,
277+
// a UUID string, or a public_id string. Normalize to a single identifier
278+
// so the ResolvableVehicle rule can validate it correctly.
279+
if (isset($input['vehicle']) && is_array($input['vehicle'])) {
280+
$input['vehicle'] = data_get($input['vehicle'], 'id')
281+
?? data_get($input['vehicle'], 'public_id')
282+
?? data_get($input['vehicle'], 'uuid')
283+
?? null;
284+
$request->merge(['driver' => $input]);
285+
}
286+
265287
// create validation request
266288
$updateDriverRequest = UpdateDriverRequest::createFrom($request);
267289
$rules = $updateDriverRequest->rules();

server/src/Http/Requests/Internal/CreateDriverRequest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Fleetbase\FleetOps\Http\Requests\CreateDriverRequest as CreateDriverApiRequest;
66
use Fleetbase\FleetOps\Rules\ResolvablePoint;
7+
use Fleetbase\FleetOps\Rules\ResolvableVehicle;
78
use Fleetbase\Support\Auth;
89
use Illuminate\Validation\Rule;
910

@@ -49,7 +50,7 @@ public function rules()
4950
'internal_id' => 'nullable|string|max:255',
5051
'country' => 'nullable|string|size:2',
5152
'city' => 'nullable|string|max:255',
52-
'vehicle' => 'nullable|string|starts_with:vehicle_|exists:vehicles,public_id',
53+
'vehicle' => ['nullable', new ResolvableVehicle()],
5354
'status' => 'nullable|string|in:active,inactive',
5455
'vendor' => 'nullable|exists:vendors,public_id',
5556
'job' => 'nullable|exists:orders,public_id',
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace Fleetbase\FleetOps\Rules;
4+
5+
use Fleetbase\FleetOps\Models\Vehicle;
6+
use Illuminate\Contracts\Validation\Rule;
7+
use Illuminate\Support\Str;
8+
9+
class ResolvableVehicle implements Rule
10+
{
11+
/**
12+
* The resolved vehicle instance, if found.
13+
*/
14+
protected ?Vehicle $resolved = null;
15+
16+
/**
17+
* Determine if the validation rule passes.
18+
*
19+
* Accepts:
20+
* - A public_id string (e.g. "vehicle_abc123")
21+
* - A UUID string (e.g. "550e8400-e29b-41d4-a716-446655440000")
22+
* - An array/object containing an "id", "public_id", or "uuid" key
23+
*
24+
* @param string $attribute
25+
*
26+
* @return bool
27+
*/
28+
public function passes($attribute, $value)
29+
{
30+
$identifier = $this->extractIdentifier($value);
31+
32+
if (empty($identifier)) {
33+
return true; // nullable — let the nullable rule handle empty values
34+
}
35+
36+
if (Str::isUuid($identifier)) {
37+
$this->resolved = Vehicle::where('uuid', $identifier)->first();
38+
} else {
39+
$this->resolved = Vehicle::where('public_id', $identifier)->first();
40+
}
41+
42+
return $this->resolved !== null;
43+
}
44+
45+
/**
46+
* Get the validation error message.
47+
*
48+
* @return string
49+
*/
50+
public function message()
51+
{
52+
return 'The :attribute must be a valid vehicle public ID, UUID, or vehicle object.';
53+
}
54+
55+
/**
56+
* Extract a string identifier from the given value.
57+
*
58+
* Handles a plain string, an associative array, or a stdClass object.
59+
*/
60+
protected function extractIdentifier($value): ?string
61+
{
62+
if (is_string($value)) {
63+
return $value;
64+
}
65+
66+
if (is_array($value)) {
67+
return data_get($value, 'id')
68+
?? data_get($value, 'public_id')
69+
?? data_get($value, 'uuid')
70+
?? null;
71+
}
72+
73+
if (is_object($value)) {
74+
return data_get($value, 'id')
75+
?? data_get($value, 'public_id')
76+
?? data_get($value, 'uuid')
77+
?? null;
78+
}
79+
80+
return null;
81+
}
82+
83+
/**
84+
* Get the resolved Vehicle model instance after validation passes.
85+
*/
86+
public function getResolved(): ?Vehicle
87+
{
88+
return $this->resolved;
89+
}
90+
}

0 commit comments

Comments
 (0)