format json errors in Laravel when validation fails - json

I'm new to Laravel. I'm trying to create an API post request. This is the validation I have.
$request->validate([
'name' => 'required',
'phone' => 'required',
'address' => 'required',
'started_date' => 'required|after:2012-01-01|before:tomorrow',
]);
When the validation fails, with the validation I have, let's say "name" and "started_date" are missing, this is the response I get.
{
"message": "The given data was invalid.",
"errors": {
"name": [
"The name field is required."
],
"started_date": [
"The started date must be a date before tomorrow."
]
}
However, It's not the format I want. Here is what I want
{
"message": "The given data was invalid.",
"errors": {
"name": "The name field is required.",
"started_date": "The started date must be a date before tomorrow."
}
So what should I do to format the response?

Related

How to handle dynamic json in flutter?

{
"status": "error",
"msg": "Please fill all the fields",
"error": {
"device_token": [
"The device token field is required.",
]
"mobile_number": [
"The mobile number field is required."
]
}
}
Here is my json response .How can i handle my error as the error object is dynamic
Keep your JSON content in a variable and access your data as following.
Also, don't forget the comma "," after the "device_token" key
var res = {
"status": "error",
"msg": "Please fill all the fields",
"error": {
"device_token": [
"The device token field is required."
],
"mobile_number": [
"The mobile number field is required."
]
}
};
var deviceToken = (res["error"] as Map)["device_token"].first;
var mobileNumber = (res["error"] as Map)["mobile_number"].first;
print(deviceToken);
print(mobileNumber);
I'd recommend reading JSON and Serialization in the Flutter docs.
I use json_seializable. It is a good way to solve this problem.
Just to mark your models with decorators and generate each method automatically. BTW, this is recommended on flutter documentation.
And also it is really important to add types to your variables and use the Dart compiler's power

JSON - replacement characters for Rest API query

I'm trying to retrieve data from a WEB API rest server.
https://stat-xplore.dwp.gov.uk/webapi/online-help/Open-Data-API.html
Which stores the data in Cubes.
I understand the basics and am successfully retrieving data. However I want to use a Postcode, (ZIP Code) grouping dimension.
This dimension groups postcodes by their first 3 characters. CF1, CF2 etc.
The schema has this as the definition for sending the request for this dimension.
["str:group:UC_Households:X_Geography+%28postcode%29"]
I'm assuming I'm meant to replace the end part with the Postcode group that I'm interested in and enclose the postcode group with some special characters.
So far I have tried
["str:group:UC_Households:X_Geography:CF1"]
["str:group:UC_Households:X_Geography:'CF1'"]
["str:group:UC_Households:X_Geography:#CF1#"]
["str:group:UC_Households:X_Geography:CF1%"]
["str:group:UC_Households:X_Geography#CF1#"]
["str:group:UC_Households:X_GeographyCF1"]
["str:group:UC_Households:X_Geography:CF1"]
the complete JSON is
{ "database" : "str:database:UC_Households",
"measures" : [ "str:count:UC_Households:V_F_UC_HOUSEHOLDS" ],
"dimensions" : [
[ "str:field:UC_Households:V_F_UC_HOUSEHOLDS:HNFAMILY_TYPE" ],
["str:group:UC_Households:X_Geography:CF1"]
]
}
Any help would be appreciated.
How to easily generate the query JSON for the Stat-Xplore API
Log into https://stat-xplore.dwp.gov.uk
Open the data set and table you want
In the top-right hand corner, find the "Download Table" tool
In the drop-down box, select "Open Data API Query (.json) and click "Go"
This will download a JSON file with the parameters for the query that describes the table you're looking at in your web browser. An example is shown below:
{
"database" : "str:database:UC_Monthly",
"measures" : [ "str:count:UC_Monthly:V_F_UC_CASELOAD_FULL" ],
"recodes" : {
"str:field:UC_Monthly:F_UC_DATE:DATE_NAME" : {
"map" : [ [ "str:value:UC_Monthly:F_UC_DATE:DATE_NAME:C_UC_DATE:202010" ] ],
"total" : false
},
"str:field:UC_Monthly:V_F_UC_CASELOAD_FULL:EMPLOYMENT_CODE" : {
"map" : [ [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:EMPLOYMENT_CODE:C_UC_EMPLOYMENT:0" ], [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:EMPLOYMENT_CODE:C_UC_EMPLOYMENT:1" ] ],
"total" : true
},
"str:field:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE" : {
"map" : [ [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE:V_C_MASTERGEOG11_PARLC_TO_REGION:E14000541" ], [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE:V_C_MASTERGEOG11_PARLC_TO_REGION:E14000542" ], [ "str:value:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE:V_C_MASTERGEOG11_PARLC_TO_REGION:W07000041" ] ],
"total" : true
}
},
"dimensions" : [ [ "str:field:UC_Monthly:V_F_UC_CASELOAD_FULL:PARLC_CODE" ], [ "str:field:UC_Monthly:F_UC_DATE:DATE_NAME" ], [ "str:field:UC_Monthly:V_F_UC_CASELOAD_FULL:EMPLOYMENT_CODE" ] ]
}
The API table endpoint docs give more info.
An explanation for OP's issue
I'm also attempting to use this API. (I happen to be using Python requests library.)
If I make a request to the /v1/table endpoint where one dimension is of type GROUP (rather than FIELD)
measures=["str:count:UC_Monthly:V_F_UC_CASELOAD_FULL"],
dimensions=[["str:field:UC_Monthly:F_UC_DATE:DATE_NAME"],
["str:group:UC_Monthly:X_Geography+%28residence-based%29"]
]
then this error 422 is returned in the HTTP response:
requests.exceptions.HTTPError: 422 Client Error: Unprocessable Entity for url: https://stat-xplore.dwp.gov.uk/webapi/rest/v1/table
This is the payload of the response, which describes the error in more detail:
{
"message":"Unexpected schema component type 'group' for 'str:group:UC_Monthly:X_Geography+%28residence-based%29'. Expected one of type [field, valueset, udf].",
"errorType":"UNEXPECTED_SCHEMA_COMPONENT_TYPE_EXCEPTION",
"component" : "str:group:UC_Monthly:X_Geography+%28residence-based%29",
"type" : "group",
"expectedTypes" : [ "field", "valueset", "udf" ]
}
If I understand this correctly, it's not complaining about the formatting of the string, it's saying that only FIELD, VALUESET and UDF objects are expected.
If you look at the schema endpoint one can retrieve info about the possible groups, fields, etc.
{
'id': 'str:group:UC_Monthly:X_Geography+%28postcode%29',
'label': 'Geography (postcode)',
'location': 'https://stat-xplore.dwp.gov.uk/webapi/rest/v1/schema/str:group:UC_Monthly:X_Geography+%2528postcode%2529',
'type': 'GROUP'
}

Looping through Laravel validation errors object in Reactjs

I'm using laravel validation request.
I need to loop the object in the key "errors".
Im using React, but just want understand how to loop it with JavaScript. This is the json object:
{
"message": "The given data was invalid.",
"errors": {
"email": [
"The email field is required."
],
"name": [
"The name field is required."
],
"description" : [
"The description field is required."
]
}
}
Use Object.keys which returns an array of object properties.
Object.keys(validation.errors).forEach(key => {
console.log(validation.errors[key])
})

Google Fit location datatype yields 403 Permission Denied

In my app I am requesting data from a Google Fit dataset using the "aggregate" function. This works fine for heart rate, speed, distance, etc., but this stopped working (but has been working before) for location data.
This is the request template I am using:
POST https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate
{
"aggregateBy": [
{
"dataTypeName": "com.google.location.sample"
}
],
"endTimeMillis": xxxx,
"startTimeMillis": xxxx
}
For dataTypeName=="com.google.location.sample" the request returns:
403
- Show headers -
{
"error": {
"errors": [
{
"domain": "global",
"reason": "forbidden",
"message": "No permission to read data for this private data source."
}
],
"code": 403,
"message": "No permission to read data for this private data source."
}
}
Why is location data private? Has there been a change on the Google
API side? Is there a different way to request location data?
These are the scopes my app is authorized for:
"https://www.googleapis.com/auth/fitness.activity.read",
"https://www.googleapis.com/auth/fitness.body.read",
"https://www.googleapis.com/auth/fitness.location.read",
"https://www.googleapis.com/auth/fitness.nutrition.read"
Note that the list includes "fitness.location.read".
Thanks for any advice.
I also try to get data from Google Fit but for "steps" and also sometimes getting the response with status 403
REQUEST:
POST https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate
$headers = [
'Authorization' => 'Bearer ' . $access_token,
'Content-Type' => 'application/json',
];
$body = [
"aggregateBy" => [
[
"dataTypeName" => "com.google.step_count.delta",
"dataSourceId" =>
"derived:com.google.step_count.delta:com.google.android.gms:estimated_steps"
],
[
"dataTypeName" => "com.google.distance.delta",
"dataSourceId" =>
"derived:com.google.distance.delta:com.google.android.gms:merge_distance_delta"
]
],
"bucketByTime" => ["durationMillis" => xxxx],
"startTimeMillis" => xxxx,
"endTimeMillis" => xxxx
];
RESPONSE:
"error": {
"errors": [
{
"domain": "global",
"reason": "forbidden",
"message": "datasource not found (truncated...)
POST https://www.googleapis.com/fitness/v1/users/me/dataset:aggregate` resulted in a `403 Forbidden` response:
DECISION:
All descriptions for error 403 you can see here:
https://developers.google.com/analytics/devguides/reporting/core/v3/errors
For resolving you can try to do this algorithm: https://developers.google.com/analytics/devguides/reporting/core/v3/errors#backoff
or your users should have sufficient permissions (403 "insufficientPermissions")

Logstash is not converting correctly in JSON

Following is my json log file
[
{
"error_message": " Failed to get line from input file (end of file?).",
"type": "ERROR",
"line_no": "2625",
"file": "GTFplainText.c",
"time": "17:40:02",
"date": "01/07/16",
"error_code": "GTF-00014"
},
{
"error_message": " Bad GTF plain text file header or footer line. ",
"type": "ERROR",
"line_no": "2669",
"file": "GTFplainText.c",
"time": "17:40:02",
"date": "01/07/16",
"error_code": "GTF-00004"
},
{
"error_message": " '???' ",
"type": "ERROR",
"line_no": "2670",
"file": "GTFplainText.c",
"time": "17:40:02",
"date": "01/07/16",
"error_code": "GTF-00005"
},
{
"error_message": " Failed to find 'event source'/'product detail' records for event source '3025188506' host event type 1 valid",
"type": "ERROR",
"line_no": "0671",
"file": "RGUIDE.cc",
"time": "15:43:48",
"date": "06/07/16",
"error_code": "RGUIDE-00033"
}
]
According to my understanding As the log is already in json, We do not need filter section in logstash configuration. Following is my logstash config
input {
file{
path => "/home/ishan/sf_shared/log_json.json"
start_position => "beginning"
codec => "json"
}
}
and the output configuration is
output {
elasticsearch {
hosts => ["localhost:9200"]
sniffing => true
manage_template => false
index => "%{[#metadata][beat]}-%{+YYYY.MM.dd}"
document_type => "%{[#metadata][type]}"
}
stdout { codec => rubydebug }
}
But it seems like the data is not going into ES, as I am not able to see the data when I query the index. What am I missing?
I think the problem is that the json codec expects a full json message on one line and won't work with a message on multiple lines.
A possible work around would be to use the multiline codex and use the json filter.
The configuration for the multiline codec would be:
multiline {
pattern => "]"
negate => "true"
what => "next"
}
All the lines that do not begin with ] will be regrouped with the next line, so you'll have one full json document to give to the json filter.