Skip to content

Commit

Permalink
serializer: handle marketplace errors
Browse files Browse the repository at this point in the history
  • Loading branch information
M3nin0 committed Jun 5, 2024
1 parent 7c589d6 commit 434d07d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 24 deletions.
18 changes: 0 additions & 18 deletions src/lib/DepositRecordSerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,24 +296,6 @@ export class RDMDepositRecordSerializer extends DepositRecordSerializer {
fieldpath: "metadata.marketplace.vendor_contact",
deserializedDefault: "",
}),
pricing: new SchemaField({
fieldpath: "metadata.marketplace.pricing",
schema: {
title: new Field({
fieldpath: "title",
}),
description: new Field({
fieldpath: "description",
}),
url: new Field({
fieldpath: "url",
}),
value: new Field({
fieldpath: "value",
}),
},
deserializedDefault: [],
}),
};
}

Expand Down
2 changes: 0 additions & 2 deletions src/lib/DepositRecordSerializer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ describe("RDMDepositRecordSerializer tests", () => {
marketplace: {
launch_url: "",
vendor_contact: "",
pricing: [],
},
version: "",
geo_work_programme_activity: "",
Expand Down Expand Up @@ -361,7 +360,6 @@ describe("RDMDepositRecordSerializer tests", () => {
marketplace: {
launch_url: "",
vendor_contact: "",
pricing: [],
},
contributors: [
{
Expand Down
9 changes: 5 additions & 4 deletions src/lib/components/FormFeedback.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
FILE_UPLOAD_SAVE_DRAFT_FAILED,
RESERVE_PID_FAILED,
} from "../state/types";
import { leafTraverse } from "../utils";
import { leafTraverse, objectAsList } from "../utils";
import PropTypes from "prop-types";

const defaultLabels = {
Expand All @@ -47,6 +47,9 @@ const defaultLabels = {
"metadata.publisher": i18next.t("Publisher"),
"metadata.related_identifiers": i18next.t("Related works"),
"metadata.identifiers": i18next.t("Alternate identifiers"),
"metadata.marketplace": i18next.t("Launch URL and Vendor Contact"),
"metadata.marketplace_launch_url": i18next.t("Launch URL"),
"metadata.marketplace_vendor_contact": i18next.t("Vendor Contact"),
"access.embargo.until": i18next.t("Embargo until"),
"pids.doi": i18next.t("DOI"),
};
Expand Down Expand Up @@ -193,9 +196,7 @@ class DisconnectedFormFeedback extends Component {
// e.g., {metadata: {creators: ,,,}} => {"metadata.creators": ...}
// For now, only for metadata, files and access.embargo
const metadata = errors.metadata || {};
const step0Metadata = Object.entries(metadata).map(([key, value]) => {
return ["metadata." + key, value];
});
const step0Metadata = objectAsList(metadata, "metadata");
const files = errors.files || {};
const step0Files = Object.entries(files).map(([key, value]) => {
return ["files." + key, value];
Expand Down
44 changes: 44 additions & 0 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,50 @@ export function leafTraverse(obj, func = (l) => l) {
}
}

/**
* Transforms an object into an array of key-value pairs with a specified prefix.
* Nested objects are flattened with keys joined by underscores.
* The top-level key is prefixed with a given string.
*
* @param {Object} obj - The object to be transformed.
* @param {string} prefix - The prefix to be added to each key.
* @returns {Array} An array of key-value pairs with transformed keys.
*
* @example
* const inputObj = {
* "title": "Missing data for required field.",
* "marketplace": {
* "launch_url": "Missing data for required field."
* };
* };
*
* const prefix = 'metadata';
* console.log(objectAsList(inputObj, prefix));
* // Output:
* // [
* // ["metadata.title", "Missing data for required field."],
* // ["metadata.marketplace_launch_url", "Missing data for required field."]
* // ]
*/
export function objectAsList(obj, prefix) {
function transform(obj, parentKey = "", result = []) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
const newKey = parentKey ? `${parentKey}_${key}` : key;
if (typeof obj[key] === "object" && obj[key] !== null) {
transform(obj[key], newKey, result);
} else {
result.push([newKey, obj[key]]);
}
}
}
return result;
}

const result = transform(obj);
return result.map(([key, value]) => [`${prefix}.${key}`, value]);
}

/**
* Sort a list of string values (options).
* @param {list} options
Expand Down

0 comments on commit 434d07d

Please sign in to comment.