How to skip validation for array that is empty? - laravel

I have an incoming post data from js like this
[form] => Array (
[name] => 'a form'
[type] => 'form'
...
[children] => Array (
[0] =>
[1] =>
[2] => Array(
[title] => 'first'
[order] => '1'
...
}
[3] => Array(
[title] => 'second'
[order] => '2'
...
)
...
)
...
)
and rules like
[
'form.name' => 'required|string',
'form.type' => 'required|string',
...
'form.children.*.title' => 'requered|string'
'form.children.*.order' => 'requered|integer'
...
]
What is the best way to completely exclude/skip the form.children arrays that are empty and process the ones with data?

Try this:
[
'form.name' => 'required|string',
'form.type' => 'required|string',
...
'form.children.*.title' => 'sometimes|string'
'form.children.*.order' => 'sometimes|integer'
...
]
Sometimes means, if there is something, follow the next rule(s).
https://laravel.com/docs/8.x/validation#validating-when-present
Addition:
For more complex situations, f.ex. you are not interested in an order value if there is no title, makes sense right? Try this:
[
'form.name' => 'required|string',
'form.type' => 'required|string',
...
'form.children.*.title' => 'sometimes|string'
'form.children.*.order' => 'exclude_if:form.children.*.title,null|integer'
...
]
I have never tested/used this on arrays though.

I managed to completely remove the empty arrays just by filtering them out of the request with laravel's prepareForValidation() method and array_filter(). Worked great for me.
protected function prepareForValidation()
{
$this->merge([
"form" => [
"children" => array_filter($this->form["children"])
]
]);
}
#DimitriMostrey's answer worked as well. His answer is a shorter solution without an additional method. Will accept his answer so anyone facing a similar situation can pick the one that suits the most.

Related

How to assert array value but not strict?

How can we assert some of the array's property values which contain the expected object values?
My code below is working okay, but it checks all array property values. I want to ask if there's a way we can check only some of it.
$dataToBeTested = [
'name' => 'Johnny',
'address' => 'Somewhere',
'age' => 21,
'card_no' => 13331577121,
'rep_no' => 441546661,
'status' => 'in-progress',
'created_at' => '2022-07-31T10:05:27.011000Z',
'updated_at' => '2022-07-31T10:05:27.011000Z',
];
$expectedPropValue = [
'name' => 'Johnny',
'address' => 'Somewhere',
'age' => 21,
];
as expected it will return fail, since expectedPropValue has some missing properties.
$this->assertEquals($dataToBeTested, $expectedPropValue);
Goal is something like this,
$this->assertSomeOfIt($dataToBeTested, $expectedPropValue); // return true
You are looking for assertArraySubset, Which it's been deprecated with the newer version of PHPUnit. As mentioned here https://github.com/sebastianbergmann/phpunit/issues/3494. But you can always introduce your approach and use it for your project. Or you can also try this package https://packagist.org/packages/dms/phpunit-arraysubset-asserts. which gives you what you need
self::assertArraySubset($expectedSubset, $content, true);

Laravel: How can I assertJson an array

I am creating a feature test for a Seminar. Everything is working great; I am trying to update my feature test to account for the seminar dates.
Each Seminar can have one or many dates, so I am saving these values as a json field:
// migration:
...
$table->json('dates');
...
Here is what my Seminar model looks like:
// Seminar.php
protected $casts = [
'dates' => 'array',
];
When saving the seminar, I am returning a json resource:
if ($seminar->save()) {
return response()->json(new SeminarResource($seminar), 200);
...
Using Postman, my Seminar looks a like this:
...
"capacity": 100,
"dates": [
"2020-10-15",
"2020-10-16"
],
...
So far so good!
In my test, I am testing that a seminar can be created.
$http->assertStatus(201)
->assertJson([
'type' => 'seminars',
'id' => (string)$response->id,
'attributes' => [
'dates' => $response->attributes->dates, // General error: 25 column index out of range
I've tried to convert the array to a string, or json_encode the value in the resource. I don't think that's the correct way since I am already casting the value as an array in the model.
How can I assert that my dates is returning an array?
+"dates": array:2 [
0 => "2020-10-15"
1 => "2020-10-16"
]
Thank you for your suggestions!
EDIT
When I dd($response->attributes->dates); this is what I'm getting (which is correct).
array:2 [
0 => "2020-10-15"
1 => "2020-10-16"
]
What I'm not sure is how to assert an array like that. Since I'm using faker to generate the date, I don't really know (or care) what the date is, just want to assert that it is in fact an array.
I've tried something like:
'dates' => ['*'],
However, that just adds another element to the array.
EDIT 2
If I make the array a string,
'dates' => json_encode($response->attributes->dates),
I'll get an error like this:
--- Expected
+++ Actual
## ##
- 'dates' => '["2020-10-15","2020-10-16"]',
+ 'dates' =>
+ array (
+ 0 => '2020-10-15',
+ 1 => '2020-10-16',
+ ),
In my database, the values are stored like this:
["2020-10-15","2020-10-16"]
My actual test looks like this:
$http->assertStatus(201)
->assertJsonStructure([
'type', 'id', 'attributes' => [
'name', 'venue', 'dates', 'description', 'created_at', 'updated_at',
],
])
->assertJson([
'type' => 'workshops',
'id' => (string)$response->id,
'attributes' => [
'name' => $response->attributes->name,
'venue' => $response->attributes->venue,
'dates' => $response->attributes->dates,
'description' => $response->attributes->description,
'created_at' => (string)$response->attributes->created_at,
'updated_at' => (string)$response->attributes->updated_at,
],
]);
$this->assertDatabaseHas('workshops', [
'id' => $response->id,
'name' => $response->attributes->name,
'venue' => $response->attributes->venue,
'dates' => $response->attributes->dates,
'description' => $response->attributes->description,
]);

Laravel: custom array validation depends on previous selected value

Laravel version: 7.x
I have tested the code with hard-coded valid device_company_id (which is associated with the selected device) and it works fine.
Tables
device_companies
|- id
|- company_id
|- title
|- ...
devices
|- id
|- device_company_id
|- ...
Request data
Array
(
...
[devices] => Array
(
[0] => Array
(
[device_company_id] => 1
[device_id] => 2
[device_inventory_id] => null
[amount] =>
[refundable] =>
[description] =>
)
)
)
Rules:
...
$rules = array_merge($rules, [
'devices' => [
'required',
'array'
],
'devices.*.device_company_id' => [
'required',
'integer',
'exists:device_companies,id,company_id,' . auth()->user()->id
],
'devices.*.device_id' => [
'required',
'integer',
'exists:devices,id,device_company_id,devices.*.device_company_id'
],
]);
...
I need a custom validation rule for the exists validator to validate if device actually belongs the selected device_company or not. Because I don't want anyone opening the inspect tool, change the value and causing the application an error or anything like that.
Here is the link where I found the reference https://ericlbarnes.com/2015/04/04/laravel-array-validation/
My code:
...
$rules = [
...other rules
];
$newRules = [
'devices' => [
'required',
'array'
],
'devices.*.device_company_id' => [
'required',
'integer',
'exists:device_companies,id,company_id,' . auth()->user()->id
],
];
foreach($data['devices'] as $key => $array)
{
$newRules["devices.{$key}.device_id"] = [
'required',
'integer',
"exists:devices,id,device_company_id,{$array["device_company_id"]}",
];
}
$rules = array_merge($rules, $newRules);
...
Although, this code is working for me but I feel its not the proper way to do it. It feels kind of a way around instead of the proper solution. If anyone finds a better solution then please do post your answer.
Hope this can be helpful for someone. Thanks :)

Stripe webhook test returns empty table on execution, but works on stripe's webhook tester

We're having a problem with our test suite.
When we run this using the test suite we get a 'The table is empty...' response from PHPUnit.
We know it works as we've also tested using Stripe's 'Send a web hook' test function which works, and the response is stored as expected.
Our code is here:
public function test_webhook_received()
{
$this->expectsJobs([StoreStripeWebHookJob::class]);
$this->postJson('/stripeHook', [
'created' => 1326853478,
'livemode' => false,
'id' => 'evt_00000000000000',
'type' => 'account.external_account.created',
'object' => 'event',
'request' => NULL,
'pending_webhooks' => 1,
'api_version' => '2019-12-03',
'data' => [
'object' => [
'id' => 'ba_00000000000000',
'object' => 'bank_account',
'account' => 'acct_00000000000000',
'account_holder_name' => 'Jane Austin',
'account_holder_type' => 'individual',
'bank_name' => 'STRIPE TEST BANK',
'country' => 'US',
'currency' => 'gbp',
'fingerprint' => '8JXtPxqbdX5GnmYz',
'last4' => '6789',
'metadata' => [],
'routing_number' => '110000000',
'status' => 'new',
],
],
]);
$this->assertDatabaseHas('stripe_webhooks', [
'stripe_created_at' => 1326853478,
'type' => 'account.external_account.created',
]);
}
The response received is:
Failed asserting that a row in the table [stripe_webhooks] matches the
attributes {
"stripe_created_at": 1326853478,
"type": "account.external_account.created" }.
The table is empty..
If we remove the
$this->expectsJobs([StoreStripeWebHookJob::class]);
tests succeed. Obviously the expectsJob() call should be where it is though.
ExpectsJob also intercepts the job. Much like expectsException. Judging from your clean naming convention "StoreStripe..." - I'd say it's really not storing under these test circumstances.
You'll need to test separately that your endpoint/controller is queuing a job... and that the job is storing the data. 2 tests.

Setting Validation Rules at Runtime in CakePHP

My problem right now is that a Model has a set of Validation rules like so:
var $validate = array(
'title' => array(
'rule' => 'notEmpty'
),
'uri' => array(
'slugged' => array(
'rule' => '/^[a-z0-9-_]+$/i',
'message' => 'This field should only contain characters, numbers, dashes and underscores'
),
'uniqueUrl' => array(
'rule' => array('uniqueUrl'),
'message' => 'A page has already acquired this url'
)
),
'meta_keywords' => array(
'rule' => 'notEmpty'
),
'meta_description' => array(
'rule' => 'notEmpty'
),
'layout' => array(
'rule' => 'notEmpty'
)
);
The problem is that in another model that has hasOne relationship its controller also inserts data into it. I want to NOT require the title, uri and layout from that page. How do I do it?
I have a Post Model and I set Page values from there.
Array
(
[Post] => Array
(
[title] => data[Post][title]
[body] =>
Post Body
)
[Category] => Array
(
[Category] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
)
[Page] => Array
(
[meta_keywords] => data[Page][meta_keywords]
[meta_description] => data[Page][meta_description]
)
)
I do this from the controller to set info for the Page model
$this->data['Page']['title'] = $this->data['Post']['title'];
It turns to be like this:
Array
(
[Post] => Array
(
[title] => data[Post][title]
[body] =>
Post Body
)
[Category] => Array
(
[Category] => Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
)
[Page] => Array
(
[meta_keywords] => data[Page][meta_keywords]
[meta_description] => data[Page][meta_description]
[title] => data[Post][title]
)
)
My problem is that I do not require a Page field when saving. Post belongsTo Page.
I don't require [Page][layout] when saving a Post as a Post uses the default view of the method in the Post Controller. A page uses static pages and require them when creating a Page, not when creating a Post.
You're preprocessing the data before it goes for validation, so you're taking some control away from validation. As you seem to be making the decision (in code) whether or not certain fields need to be artificially populated you are rendering those parts of the validation redundant and you should remove them. If you have multiple validations in php, you're going to end up confused.
Where to do this or how to do it more cleanly? WellbeforeValidate might be the 'correct' place to do this, but I would do it wherever it best fits with the logic of your application. Function should come before elegance.

Resources