Improvements for validation

This commit is contained in:
David Bomba 2024-02-17 05:46:26 +11:00
parent ceb4708b5d
commit 478dbe5c4a
3 changed files with 75 additions and 0 deletions

View File

@ -94,6 +94,7 @@ class StoreClientRequest extends Request
$rules['number'] = ['bail', 'nullable', Rule::unique('clients')->where('company_id', $user->company()->id)];
$rules['id_number'] = ['bail', 'nullable', Rule::unique('clients')->where('company_id', $user->company()->id)];
$rules['classification'] = 'bail|sometimes|nullable|in:individual,business,company,partnership,trust,charity,government,other';
$rules['documents'] = 'bail|sometimes|array';
return $rules;
}

View File

@ -84,6 +84,8 @@ class UpdateClientRequest extends Request
//'regex:/[@$!%*#?&.]/', // must contain a special character
];
$rules['documents'] = 'bail|sometimes|array';
return $rules;
}

View File

@ -59,6 +59,78 @@ class ClientApiTest extends TestCase
Model::reguard();
}
public function testDocumentValidation()
{
$data = [
'name' => 'name of client',
'documents' => [],
];
$response = $this->withHeaders([
'X-API-TOKEN' => $this->token,
])->postJson("/api/v1/clients",$data)
->assertStatus(200);
}
public function testDocumentValidationFails()
{
$data = [
'name' => 'name of client',
'documents' => 'wut',
];
$response = $this->withHeaders([
'X-API-TOKEN' => $this->token,
])->postJson("/api/v1/clients", $data)
->assertStatus(422);
$data = [
'name' => 'name of client',
'documents' => null,
];
$response = $this->withHeaders([
'X-API-TOKEN' => $this->token,
])->postJson("/api/v1/clients", $data)
->assertStatus(422);
}
public function testDocumentValidationPutFails()
{
$data = [
'name' => 'name of client',
'documents' => 'wut',
];
$response = $this->withHeaders([
'X-API-TOKEN' => $this->token,
])->putJson("/api/v1/clients/{$this->client->hashed_id}", $data)
->assertStatus(422);
$data = [
'name' => 'name of client',
'documents' => null,
];
$response = $this->withHeaders([
'X-API-TOKEN' => $this->token,
])->putJson("/api/v1/clients/{$this->client->hashed_id}", $data)
->assertStatus(422);
$data = [
'name' => 'name of client',
'documents' => [],
];
$response = $this->withHeaders([
'X-API-TOKEN' => $this->token,
])->putJson("/api/v1/clients/{$this->client->hashed_id}", $data)
->assertStatus(200);
}
public function testClientDocumentQuery()
{