Looping through Laravel validation errors object in Reactjs - json

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])
})

Related

People API getting error when Age Ranges is specified

I have tried with People contact create API but I am getting error. I believe the ENUM data is correct
This happens for Skills and Age ranges
{
"error": {
"code": 400,
"message": "person.age_ranges is a read only field.",
"status": "INVALID_ARGUMENT"
}
}
People.People.createContact( {"skills": [
{
"value": "Playing"
}
],
"ageRanges": [
{
"ageRange": "TWENTY_ONE_OR_OLDER"
}
],
})
Answer:
The ageRanges[] field can not be set as it is a read-only field.
More Information:
As per the Person resource in the documentation:
ageRanges[]
object (AgeRangeType)
Output only. The person's age ranges
As this is an output only value, it can not be written to by calling people.createContact.

Get all fields from google spreadsheet with api v4

I'm setting up a project in NodeJS and for testing I get the information from a spreadsheet in JSON format from the following page: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/batchGet
To obtain the information of a sheet, in the spreadsheetId field I put the ID of the spreadsheet and in the range field the name of the sheet.
This is the JSON I get:
{
"spreadsheetId": "{spreadsheetId}",
"valueRanges": [
{
"range": "Parameters!A1:Z1000",
"majorDimension": "ROWS",
"values": [
[
"Country",
"COLOMBIA"
]
]
}
]
}
What I want is to show all the fields of the sheet to obtain the title and the modification and creation dates. To do this, in the fields field, I am putting the title string but I get the following error:
{
"error": {
"code": 400,
"message": "Request contains an invalid argument.",
"status": "INVALID_ARGUMENT",
"details": [
{
"#type": "type.googleapis.com/google.rpc.BadRequest",
"fieldViolations": [
{
"field": "title",
"description": "Error expanding 'fields' parameter. Cannot find matching fields for path 'title'."
}
]
}
]
}
}
I have tried putting * but I get the same JSON.
My problem: How can I get the date of creation and the date of last modification of the spreadsheet?
you must use this endpoint to get a spreadsheet properties: https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/get

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

format json errors in Laravel when validation fails

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?

How to access the json data in react native mentioned in below pattern?

The json data is in the below pattern. And the Json data is coming from backend and getting through api and storing in a state variable.
{
"message": "user created successfully",
"status": "success",
"student": {
"class": "10",
"email": "user#gmail.com",
"name": "User",
"password": "user",
"phone_number": "some phone number",
"school": "1",
"section": "a"
}
}
I have stored the data which is returned through api in a state variable.
constructor(){
super();
this.state = {
jsonData: ''
}
}
And tried accessing using below fashion.
this.state.jsonData.status
but not able to access. How can I access the status value in react?
Please check type of jsonData in state when you call it using typeof or instanceof.
It maybe by you store fetched data in string type without check and manipulating.
If it is string type, convert it using JSON.parse