diff --git a/dictation_client/src/common/parser.test.ts b/dictation_client/src/common/parser.test.ts index d4aff39..a318a76 100644 --- a/dictation_client/src/common/parser.test.ts +++ b/dictation_client/src/common/parser.test.ts @@ -131,4 +131,23 @@ describe("parse", () => { expect(actualValue.role).toEqual(expectValue.role); } }); + + it("author_id,encryption_passwordが数値のみの場合でも、文字列として変換できる", async () => { + const text = fs.readFileSync("src/common/test/test_008.csv", "utf-8"); + const actualData = await parseCSV(text); + const expectData: CSVType[] = [ + { + name: "hoge", + email: "sample@example.com", + role: 1, + author_id: "1111", + auto_renew: 1, + notification: 1, + encryption: 1, + encryption_password: "222222", + prompt: 0, + }, + ]; + expect(actualData).toEqual(expectData); + }); }); diff --git a/dictation_client/src/common/parser.ts b/dictation_client/src/common/parser.ts index 15fe85b..30e0b43 100644 --- a/dictation_client/src/common/parser.ts +++ b/dictation_client/src/common/parser.ts @@ -42,7 +42,24 @@ export const parseCSV = async (csvString: string): Promise => download: false, worker: false, // XXX: workerを使うとエラーが発生するためfalseに設定 header: true, - dynamicTyping: true, + dynamicTyping: { + // author_id, encryption_passwordは数値のみの場合、numberに変換されたくないためdynamicTypingをtrueにしない + role: true, + auto_renew: true, + notification: true, + encryption: true, + prompt: true, + }, + // dynamicTypingがfalseの場合、空文字をnullに変換できないためtransformを使用する + transform: (value, field) => { + if (field === "author_id" || field === "encryption_password") { + // 空文字の場合はnullに変換する + if (value === "") { + return null; + } + } + return value; + }, complete: (results: ParseResult) => { // ヘッダーがCSVTypeFieldsと一致しない場合はエラーを返す if (!equals(results.meta.fields ?? [], CSVTypeFields)) { diff --git a/dictation_client/src/common/test/test_008.csv b/dictation_client/src/common/test/test_008.csv new file mode 100644 index 0000000..ccc9f52 --- /dev/null +++ b/dictation_client/src/common/test/test_008.csv @@ -0,0 +1,2 @@ +name,email,role,author_id,auto_renew,notification,encryption,encryption_password,prompt +hoge,sample@example.com,1,1111,1,1,1,222222,0 \ No newline at end of file