I would like to filter some data coming from an API payload in which i have to check if some certain part of the data is an object, such as this:
"object"{
"propety":value,
"another_propety":value,
}
I wanna be sure that the "object" that comes from the payload is actually an object, which holds properties and not an integer, neither, an array or any other type...but an object. Is there any way i can solve this by using Laravel's native validator i have to create a custom rule?
Thank you
Considering the laravel lifecycle, By the time the request payload reaches validation the object has already changed to a php array, use the below to validate your key.
$this->validate($request, [
'object' => 'required|array',
'object.property' => 'required|string',
]);
https://laravel.com/docs/5.8/validation#rule-array
also in case it is somehow going to remain a JSON object, check the official documentation for doing so -> https://laravel.com/docs/5.8/validation#rule-json
This will help you identify the object key as JSON while the request gets vaildated.
Related
I am making an iOS app with Alamofire as the HTTP Request handler, and in the iOS App, i want to send a JSON payload to a Laravel API server. More or less, the JSON structure looks like this:
{
"test_data": "Any data is here",
"another_data": "Another data is here"
}
Now, in my Laravel project, I've been implementing a validation rule, basically to validate that those 2 params in the JSON is required and in a type of String. Here's my code to implement the validator:
$validator = Validator::make($request->all(), [
'test_data' => 'required|string',
'another_data' => 'required|string'
]);
if ($validator->fails()) {
return redirect()->route('dataNotComplete');
}
The confusion is that this works, but it only works in order. Hence, if the JSON structure becomes flipped like this:
{
"another_data": "Another data is here",
"test_data": "Any data is here"
}
Validator will fails the test, even though the data is technically correct. How do I make the validator doesn't care about data ordering, or do another library exist for validating JSON specifically? I just want to validate the data of each params, not including the order of the data on the JSON Payload. Any thoughts or idea will be appreciated! Thank you!
P.S. I read from here that JSON order of parameters shouldn't matter, so that's that.
I'm saving a value into Redis cache from a symfony application using Symfony\Component\Cache\Psr16Cache implementation. I do
$cache->set('key',[
'value1' => (new \DateTime())->getTimestamp(),
'value2' => (new \DateTime())->getTimestamp(),
'value3' => 'message'
])
Obviously two \Datetime's are different. What it does is kind of serialization of the array and datetime objects into string like :
127.0.0.10:6379> GET key
"\x00\x00\x00\x02\x14\x03\x11\x16value1\x17\bDateTime\x14\x03\x11\x04date\x11\x1a2019-09-25 09:12:00.000000\x11\rtimezone_type\x06\x03\x11\btimezone\x11\x10America/New_York\x11\x14value2\x1a\x01\x14\x03\x0e\x02\x11\x1a2019-09-28 20:39:00.000000\x0e\x04\x06\x03\x0e\x05\x0e\x06\x11\x13message\x11\x11message"
So it's type of string, not array.
Then I need to read this key from another app. This app uses this Redis class and its hgetall call, which returns (error) WRONGTYPE Operation against a key holding the wrong kind of value for the key I saved above from the other app.
Question: what call from Redis library should I use to get the array from the serialized value that PSR16 symfony implementation saved?
I have this function that receives a "user" model by parameter , I want to collect the properties of that object, for this I do it this way, the code works for me but the editor "phpstorm" complained to me with this error and it was to know what would be the best way to do this.
Thank you
public function sendEmail(User $user)
{
$params = [
'name' => "{$user->completeName}",
'language' => "{$user->locale}",
'user_id' => "{$user->id}"
];
}
Field accessed via magic method less... (Ctrl+F1)
Inspection info: Referenced field is not found in subject class. Note: Check is not performed on objects of type "stdClass" or derived.
Thanxs,
maybe this is simpler
$params = $user->toArray();
or
$params = $user->toJson();
That's because in Laravel your model does not actually have the properties defined.
In PHP there is the concept of magic methods though ( http://php.net/manual/en/language.oop5.overloading.php#object.get ), where the __get() method allows you to basically intercept the access to an otherwise inaccessible (or non-existing) property.
This is what happens behind the scenes in Laravel. All your property accesses are "intercepted" and Laravel looks if your database contains a column which is named like the property you are trying to access (very simplified speaking).
In a Laravel context you can savely ignore this warning.
I'm working with a Presence Channel and according to the docs, you can return a specific array of data of what you need. e.g. in this case, it's the id and name from the variable $user.
Broadcast::channel('chat', function ($user) {
return ['id' => $user->id, 'name' => $user->name];
});
In my JS file I have
Echo.join(`chat`)
.here((users) => {
console.log('users here: ',users);
})
The console.log() returns an array of all user data (like email addresses) instead of just the id and name that I specified in the Broadcast.
How can I only make it contain the id and name ?
First of all, Broadcast::channel takes an authentication callback to authenticate whether the user is allowed to listen to this channel. This is not the data payload, this array is for authentication!
To specify the data payload use broadcastWith as specified in the docs
I restarted the browser and it just started working as normal... odd. There seems to be some type of caching thing going on that is not within the browser but perhaps on the Pusher side of things.. I'm not sure.
Has anyone been able to succssesfuly use the Respect Validation library with an Idiorm result object? It appears as though Respect Validation will only work on vanilla, not sure if that's the correct term, objects. Idiorm result objects allow you to access attributes by using $user->name but when you var_dump the object it's obviously just mapped to work like an object with attributes but isn't actually a straight object.
Excerpt from a dump below
object(ORM)[47]
protected '_data' =>
array (size=9)
'id' => string '100000' (length=6)
'name' => string 'test' (length=4)
The code below always fails because Respect Validation can't access the attribute through a reference. Is there a way around this? It would be great to use the objects as is and not have to convert them to arrays.
$user= ORM::for_table('user')->find_one(5);
$userValidator = v::attribute('name', v::string()->length(1,32));
$userValidator->validate($user);
There isn't actually an attribute name into to the object. That is probably made available through magic methods which validation using attributes is not supported (because magic methods are not attributes).
What you could do though, is use this validator inside another validator for the _data attribute:
$user = ORM::for_table('user')->find_one(5);
$userDataValidator = v::arr()
->key('name', v::string()->length(1,32));
$userObjectValidator = v::instance('ORM')
->attribute('_data', $userDataValidator);
$userObjectValidator->validate($user);
Let me know if that works, I haven't tested it. Some rules may be with wrong names... But I think you can get an idea what I am going for.