I want to ascertain that a value my api receives is a positive integer and not zero or null
This is my code for my data object, which is used by the serializer:
<code>class WorkHour
{
[....]
#[OAProperty(description: 'Arbeitsstunden', type: 'integer')]
#[AssertNotEqualTo(value: 0, message: 'Please add number of hours.', groups: ['create', 'update'])]
#[SerializerGroups(['view', 'create', 'update', 'collection'])]
public int $hours = 0;
[....]
</code>
<code>class WorkHour
{
[....]
#[OAProperty(description: 'Arbeitsstunden', type: 'integer')]
#[AssertNotEqualTo(value: 0, message: 'Please add number of hours.', groups: ['create', 'update'])]
#[SerializerGroups(['view', 'create', 'update', 'collection'])]
public int $hours = 0;
[....]
</code>
class WorkHour
{
[....]
#[OAProperty(description: 'Arbeitsstunden', type: 'integer')]
#[AssertNotEqualTo(value: 0, message: 'Please add number of hours.', groups: ['create', 'update'])]
#[SerializerGroups(['view', 'create', 'update', 'collection'])]
public int $hours = 0;
[....]
I also tried out “NotBlank” and “Positive”, but the result is the same.
Here is the matching Controller code:
<code>
[....]
#[
ValidationGroup(['create']),
Groups(['create']),
ViewContext(['groups' => ['create'], 'view_link' => true]),
IsGranted('ROLE_USER', message: 'no right for adding items', statusCode: 403),
OAPost(
operationId: 'workhour_create ',
description: 'give number of hours',
summary: 'Arbeitsstunden eintragen',
requestBody: new OARequestBody(
description: 'WorkHour dto',
required: true,
content: new Model(type: WorkHourDto::class)
),
tags: ['WorkHour Data'],
responses: [
new OAResponse(
response: 201,
description: 'Created',
content: new Model(type: WorkHourDto::class)
),
new OAResponse(response: 401, description: 'Unauthorized'),
new OAResponse(response: 403, description: 'Forbidden (access denied)'),
new OAResponse(response: 422, description: 'Unprocessable Entity'),
new OAResponse(response: 500, description: 'Internal Server Error'),
]
),
Route(path: '', name: 'create', methods: ['POST'])]
public function create(WorkHourDto $workHourDto): CreatedView
{
[....]
</code>
<code>
[....]
#[
ValidationGroup(['create']),
Groups(['create']),
ViewContext(['groups' => ['create'], 'view_link' => true]),
IsGranted('ROLE_USER', message: 'no right for adding items', statusCode: 403),
OAPost(
operationId: 'workhour_create ',
description: 'give number of hours',
summary: 'Arbeitsstunden eintragen',
requestBody: new OARequestBody(
description: 'WorkHour dto',
required: true,
content: new Model(type: WorkHourDto::class)
),
tags: ['WorkHour Data'],
responses: [
new OAResponse(
response: 201,
description: 'Created',
content: new Model(type: WorkHourDto::class)
),
new OAResponse(response: 401, description: 'Unauthorized'),
new OAResponse(response: 403, description: 'Forbidden (access denied)'),
new OAResponse(response: 422, description: 'Unprocessable Entity'),
new OAResponse(response: 500, description: 'Internal Server Error'),
]
),
Route(path: '', name: 'create', methods: ['POST'])]
public function create(WorkHourDto $workHourDto): CreatedView
{
[....]
</code>
[....]
#[
ValidationGroup(['create']),
Groups(['create']),
ViewContext(['groups' => ['create'], 'view_link' => true]),
IsGranted('ROLE_USER', message: 'no right for adding items', statusCode: 403),
OAPost(
operationId: 'workhour_create ',
description: 'give number of hours',
summary: 'Arbeitsstunden eintragen',
requestBody: new OARequestBody(
description: 'WorkHour dto',
required: true,
content: new Model(type: WorkHourDto::class)
),
tags: ['WorkHour Data'],
responses: [
new OAResponse(
response: 201,
description: 'Created',
content: new Model(type: WorkHourDto::class)
),
new OAResponse(response: 401, description: 'Unauthorized'),
new OAResponse(response: 403, description: 'Forbidden (access denied)'),
new OAResponse(response: 422, description: 'Unprocessable Entity'),
new OAResponse(response: 500, description: 'Internal Server Error'),
]
),
Route(path: '', name: 'create', methods: ['POST'])]
public function create(WorkHourDto $workHourDto): CreatedView
{
[....]
My phpunit test expects an http status 422, but gets an 201 and happy ever after.
What am I missing here? How can I achieve that a zero or null is not accepted as a value here?