Я пытаюсь получить доступ к вложенным объектам и массиву, чтобы объединить все ошибки в одну строку для каждого ключа объекта, разделенного точкой .
.Я хотел бы, чтобы он перебрал тех, кому это нужно, и тех, кто не нуждается в нем, я хотел бы, чтобы он перешел к следующему ключу.
Мне удалось пройти первые два теста, сопоставив первые два ключа и успешно добавив точку и присоединив пробел, но я не могу пройти через вложенные объекты или массивы
function transformErrors(error) {
return error.map(value => {
// console.log("The current iteration is: " + index);
// console.log("The current element is: " + currElement);
return value
.map((currElement, index) => {
// console.log("The current iteration is: " + index);
// console.log("The current element is: " + currElement);
if (index === undefined && index === index) {
// return index++;
} else {
return `${currElement}.`;
}
})
.join(" ");
});
}
Тест, который я пытаюсь пройти:
it("should tranform errors", () => {
// example error object returned from API converted to Immutable.Map
const errors = Immutable.fromJS({
name: ["This field is required"],
age: ["This field is required", "Only numeric characters are allowed"],
urls: [
{},
{},
{
site: {
code: ["This site code is invalid"],
id: ["Unsupported id"]
}
}
],
url: {
site: {
code: ["This site code is invalid"],
id: ["Unsupported id"]
}
},
tags: [
{},
{
non_field_errors: ["Only alphanumeric characters are allowed"],
another_error: ["Only alphanumeric characters are allowed"],
third_error: ["Third error"]
},
{},
{
non_field_errors: [
"Minumum length of 10 characters is required",
"Only alphanumeric characters are allowed"
]
}
],
tag: {
nested: {
non_field_errors: ["Only alphanumeric characters are allowed"]
}
}
});
let result = transformErrors(errors);
assert.deepEqual(result.toJS(), {
name: "This field is required.",
age: "This field is required. Only numeric characters are allowed.",
urls: [
{},
{},
{
site: {
code: "This site code is invalid.",
id: "Unsupported id."
}
}
],
url: {
site: {
code: "This site code is invalid.",
id: "Unsupported id."
}
},
tags:
"Only alphanumeric characters are allowed. Third error. " +
"Minumum length of 10 characters is required.",
tag: "Only alphanumeric characters are allowed."
});
});
Токовый выход:
{
"age": "This field is required. Only numeric characters are
allowed."
"name": "This field is required."
"tag": "Map { \"non_field_errors\": List [ \"Only alphanumeric
characters are allowed\" ] }."
"tags": "Map {}. Map { \"non_field_errors\": List [ \"Only
alphanumeric characters are allowed\" ], \"another_error\": List [
\"Only alphanumeric characters are allowed\" ], \"third_error\":
List [ \"Third error\" ] }. Map {}. Map { \"non_field_errors\": List
[ \"Minumum length of 10 characters is required\", \"Only
alphanumeric characters are allowed\" ] }."
"url": "Map { \"code\": List [ \"This site code is invalid\" ],
\"id\": List [ \"Unsupported id\" ] }."
"urls": "Map {}. Map {}. Map { \"site\": Map { \"code\": List [
\"This site code is invalid\" ], \"id\": List [ \"Unsupported id\" ]
} }."
}
Ожидаемый результат:
"tag": "Only alphanumeric characters are allowed."
"tags": "Only alphanumeric characters are allowed. Third error.
Minumum length of 10 characters is required."
"url": {
"site": {
"code": "This site code is invalid."
"id": "Unsupported id."
}
}
"urls": [
{}
{}
{
"site": {
"code": "This site code is invalid."
"id": "Unsupported id."
}
}
]