fix(medusa): Allow deleting metadata fields (#3055)
This commit is contained in:
@@ -12,6 +12,7 @@ export function setMetadata(
|
||||
): Record<string, unknown> {
|
||||
const existing = obj.metadata || {}
|
||||
const newData = {}
|
||||
|
||||
for (const [key, value] of Object.entries(metadata)) {
|
||||
if (typeof key !== "string") {
|
||||
throw new MedusaError(
|
||||
@@ -19,6 +20,21 @@ export function setMetadata(
|
||||
"Key type is invalid. Metadata keys must be strings"
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* We reserve the empty string as a way to delete a key.
|
||||
* If the value is an empty string, we don't
|
||||
* set it, and if it exists in the existing metadata, we
|
||||
* unset the field.
|
||||
*/
|
||||
if (value === "") {
|
||||
if (key in existing) {
|
||||
delete existing[key]
|
||||
}
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
newData[key] = value
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user