Skip to content

Commit 7c96bc4

Browse files
fix: Variant / Product Metadata making Product import fail when provided in csv input file
- fix: make Metadata Variant and Product Variant static columns use the new function, so they don't fail later with the create / update product zod schema - chore: add tests for processAsJson fucntion Co-authored-by: Oli Juhl <[email protected]>
1 parent 9751c54 commit 7c96bc4

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

packages/core/utils/src/product/__tests__/csv-normalizer.spec.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,6 +1101,40 @@ describe("CSV processor", () => {
11011101
`)
11021102
})
11031103

1104+
describe("Variant Metadata column", () => {
1105+
it("should process variant metadata as JSON correctly", () => {
1106+
const csvRow = {
1107+
"Product Handle": "test-product",
1108+
"Variant Title": "Test Variant",
1109+
"Variant Metadata": '{ "key": "value", "number": 123 }',
1110+
}
1111+
1112+
const normalized = CSVNormalizer.preProcess(csvRow, 1)
1113+
const processor = new CSVNormalizer([normalized])
1114+
const result = processor.proccess()
1115+
1116+
expect(result.toCreate["test-product"].variants[0].metadata).toEqual({
1117+
key: "value",
1118+
number: 123,
1119+
})
1120+
})
1121+
1122+
it("should throw an error for invalid JSON in variant metadata", () => {
1123+
const csvRow = {
1124+
"Product Handle": "test-product",
1125+
"Variant Title": "Test Variant",
1126+
"Variant Metadata": 'invalid json',
1127+
}
1128+
1129+
const normalized = CSVNormalizer.preProcess(csvRow, 1)
1130+
const processor = new CSVNormalizer([normalized])
1131+
1132+
expect(() => processor.proccess()).toThrow(
1133+
'Row 1: Invalid value provided for "variant metadata". Expected a valid JSON string, received "invalid json"'
1134+
)
1135+
})
1136+
})
1137+
11041138
describe("System-generated columns", () => {
11051139
it("should ignore product timestamp columns during import", () => {
11061140
const csvRow: Record<string, string | boolean | number> = {

packages/core/utils/src/product/csv-normalizer.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,28 @@ function processAsString<Output>(
7979
}
8080
}
8181

82+
/**
83+
* Process a column value as a json object
84+
*/
85+
function processAsJson<Output>(
86+
inputKey: string,
87+
outputKey: keyof Output
88+
): ColumnProcessor<Output> {
89+
return (csvRow, _, rowNumber, output) => {
90+
const value = csvRow[inputKey]
91+
if (isPresent(value)) {
92+
if (typeof value === 'string') {
93+
try {
94+
output[outputKey] = JSON.parse(value);
95+
} catch (error) {
96+
throw createError(rowNumber, `Invalid value provided for "${inputKey}". Expected a valid JSON string, received "${value}"`);
97+
}
98+
}
99+
}
100+
return undefined
101+
}
102+
}
103+
82104
/**
83105
* Processes a column value but ignores it (no-op processor for system-generated fields)
84106
*/
@@ -193,7 +215,7 @@ const productStaticColumns: {
193215
),
194216
"product weight": processAsNumber("product weight", "weight"),
195217
"product width": processAsNumber("product width", "width"),
196-
"product metadata": processAsString("product metadata", "metadata"),
218+
"product metadata": processAsJson("product metadata", "metadata"),
197219
"shipping profile id": processAsString(
198220
"shipping profile id",
199221
"shipping_profile_id"
@@ -257,7 +279,7 @@ const variantStaticColumns: {
257279
"variant height": processAsNumber("variant height", "height"),
258280
"variant length": processAsNumber("variant length", "length"),
259281
"variant material": processAsString("variant material", "material"),
260-
"variant metadata": processAsString("variant metadata", "metadata"),
282+
"variant metadata": processAsJson("variant metadata", "metadata"),
261283
"variant origin country": processAsString(
262284
"variant origin country",
263285
"origin_country"

0 commit comments

Comments
 (0)