From ebf3f61285f19d8054dbc322f00ac33a381adefd Mon Sep 17 00:00:00 2001 From: Stefano Frontini Date: Wed, 24 Jan 2024 11:54:50 +0100 Subject: [PATCH] adds convertFormat, tests and refactor inputFieldName --- src/formatter/date.ts | 2 +- src/types/__tests__/boolean.test.ts | 2 - src/types/__tests__/case.test.ts | 2 - src/types/__tests__/date.test.ts | 57 +++++++++++++++++-------- src/types/__tests__/number.test.ts | 11 ----- src/types/__tests__/renameField.test.ts | 6 +-- src/types/__tests__/singleInput.test.ts | 34 ++++++++++++++- src/types/__tests__/string.test.ts | 7 --- src/types/boolean.ts | 2 - src/types/case.ts | 4 +- src/types/date.ts | 23 +++++----- src/types/number.ts | 4 -- src/types/renameField.ts | 1 - src/types/singleInput.ts | 11 ++++- src/types/string.ts | 7 --- 15 files changed, 96 insertions(+), 77 deletions(-) diff --git a/src/formatter/date.ts b/src/formatter/date.ts index 983d4db..07fd0a0 100644 --- a/src/formatter/date.ts +++ b/src/formatter/date.ts @@ -62,7 +62,7 @@ export const dateStringFromTimestampFormat = ( ) ); -const OutputFormat = t.union([ +export const OutputFormat = t.union([ t.literal("yyyy-MM-dd"), t.literal("yyyy-MM-dd HH:mm"), t.literal("yyyy-MM-dd HH:mm:ss") diff --git a/src/types/__tests__/boolean.test.ts b/src/types/__tests__/boolean.test.ts index 281bcbf..faa5a4f 100644 --- a/src/types/__tests__/boolean.test.ts +++ b/src/types/__tests__/boolean.test.ts @@ -4,7 +4,6 @@ import { BooleanMapping } from "../boolean"; describe("BooleanToStringMapping", () => { it("should validate with correct values", () => { const validMapping = { - inputFieldName: "foo", falseString: "false", mapper: "BOOLEAN_TO_STRING", trueString: "true" @@ -17,7 +16,6 @@ describe("BooleanToStringMapping", () => { it("should not validate with incorrect values", () => { const invalidMapping = { - inputFieldName: "foo", falseString: 123, mapper: "BOOLEAN_TO_STRING", trueString: "true" diff --git a/src/types/__tests__/case.test.ts b/src/types/__tests__/case.test.ts index f67378b..e15291b 100644 --- a/src/types/__tests__/case.test.ts +++ b/src/types/__tests__/case.test.ts @@ -8,7 +8,6 @@ describe("SwitchCaseMapping", () => { case1: "value1", case2: "value2" }, - inputFieldName: "foo", defaultValue: "default" }; const result = SwitchCaseMapping.decode(validMapping); @@ -16,7 +15,6 @@ describe("SwitchCaseMapping", () => { }); it("should not decode if cases is not an object", () => { const invalidMapping = { - inputFieldName: "foo", cases: "invalidCases", defaultValue: "default" }; diff --git a/src/types/__tests__/date.test.ts b/src/types/__tests__/date.test.ts index 02accd1..8bf60eb 100644 --- a/src/types/__tests__/date.test.ts +++ b/src/types/__tests__/date.test.ts @@ -2,37 +2,60 @@ import * as E from "fp-ts/Either"; import { DateMapping } from "../date"; describe("DateMapping", () => { - it("should decode a correct dateMapping type properly", () => { + it("should decode a correct DATE_TO_ISO type properly", () => { const validData = { - inputFieldName: "foo", - dateString: "2023-12-19", mapper: "DATE_TO_ISO" }; const res = DateMapping.decode(validData); expect(E.isRight(res)).toBeTruthy(); }); - it("should not decode an invalid dateMapping type properly", () => { - const invalidData = { - inputFieldName: "foo", - dateString: "2023-12-19", - mapper: "INVALID MAPPER" + it("should decode a correct DATE_TO_UTC type properly", () => { + const validData = { + mapper: "DATE_TO_UTC" }; - const res = DateMapping.decode(invalidData); - expect(E.isLeft(res)).toBeTruthy(); + const res = DateMapping.decode(validData); + expect(E.isRight(res)).toBeTruthy(); + }); + it("should decode a correct ISO_TO_UTC type properly", () => { + const validData = { + mapper: "ISO_TO_UTC" + }; + const res = DateMapping.decode(validData); + expect(E.isRight(res)).toBeTruthy(); + }); + it("should decode a correct DATE_TO_TIMESTAMP type properly", () => { + const validData = { + mapper: "DATE_TO_TIMESTAMP" + }; + const res = DateMapping.decode(validData); + expect(E.isRight(res)).toBeTruthy(); + }); + it("should decode a correct DATE_FROM_TIMESTAMP type properly", () => { + const validData = { + mapper: "DATE_FROM_TIMESTAMP" + }; + const res = DateMapping.decode(validData); + expect(E.isRight(res)).toBeTruthy(); + }); + it("should decode a correct CONVERT_FORMAT type properly", () => { + const validData = { + mapper: "CONVERT_FORMAT", + output: "yyyy-MM-dd" + }; + const res = DateMapping.decode(validData); + expect(E.isRight(res)).toBeTruthy(); }); - it("should not decode if dateString is not a string", () => { + it("should not decode an invalid CONVERT_FORMAT type properly", () => { const invalidData = { - inputFieldName: "foo", - dateString: 2023 - 12 - 19, - mapper: "DATE_TO_ISO" + mapper: "CONVERT_FORMAT", + output: "INVALID FORMAT" }; const res = DateMapping.decode(invalidData); expect(E.isLeft(res)).toBeTruthy(); }); - it("should not decode if inpuFieldName is missing", () => { + it("should not decode an invalid dateMapping type properly", () => { const invalidData = { - dateString: "2023-12-19", - mapper: "DATE_TO_ISO" + mapper: "INVALID MAPPER" }; const res = DateMapping.decode(invalidData); expect(E.isLeft(res)).toBeTruthy(); diff --git a/src/types/__tests__/number.test.ts b/src/types/__tests__/number.test.ts index c6490a1..c3e93ec 100644 --- a/src/types/__tests__/number.test.ts +++ b/src/types/__tests__/number.test.ts @@ -5,7 +5,6 @@ describe("NumberMapping", () => { describe("MULTIPLY_NUMBER", () => { it('should validate when multiplier is a number and mapper is "MULTIPLY_NUMBER"', () => { const validData = { - inputFieldName: "foo", multiplier: 5, mapper: "MULTIPLY_NUMBER" }; @@ -14,7 +13,6 @@ describe("NumberMapping", () => { }); it("should not validate when multiplier is not a number", () => { const invalidData = { - inputFieldName: "foo", multiplier: "invalid", mapper: "MULTIPLY_NUMBER" }; @@ -23,7 +21,6 @@ describe("NumberMapping", () => { }); it("should not validate when mapper is not 'MULTIPLY_NUMBER'", () => { const invalidData = { - inputFieldName: "foo", multiplier: 5, mapper: "INVALID_MAPPER" }; @@ -34,7 +31,6 @@ describe("NumberMapping", () => { describe("DIVIDE_NUMBER", () => { it('should validate when divider is a number and mapper is "DIVIDE_NUMBER"', () => { const validData = { - inputFieldName: "foo", divider: 5, mapper: "DIVIDE_NUMBER" }; @@ -46,7 +42,6 @@ describe("NumberMapping", () => { it("should not validate when divider is not a number", () => { const invalidData = { - inputFieldName: "foo", divider: "invalid", mapper: "DIVIDE_NUMBER" }; @@ -58,7 +53,6 @@ describe("NumberMapping", () => { it('should not validate when mapper is not "DIVIDE_NUMBER"', () => { const invalidData = { - inputFieldName: "foo", divider: 5, mapper: "INVALID_MAPPER" }; @@ -70,7 +64,6 @@ describe("NumberMapping", () => { it('should not validate when mapper is "DIVIDE_NUMBER" and decimals attribute is evaluated', () => { const invalidData = { - inputFieldName: "foo", decimals: 2, mapper: "DIVIDE_NUMBER" }; @@ -84,7 +77,6 @@ describe("NumberMapping", () => { describe("ROUND_NUMBER", () => { it('should validate when decimals is a number and mapper is "ROUND_NUMBER"', () => { const validData = { - inputFieldName: "foo", decimals: 5, mapper: "ROUND_NUMBER" }; @@ -96,7 +88,6 @@ describe("NumberMapping", () => { it("should not validate when decimals is not a number", () => { const invalidData = { - inputFieldName: "foo", decimals: "5", mapper: "ROUND_NUMBER" }; @@ -108,7 +99,6 @@ describe("NumberMapping", () => { it('should not validate when mapper is not "ROUND_NUMBER"', () => { const invalidData = { - inputFieldName: "foo", decimals: 5, mapper: "INVALID" }; @@ -120,7 +110,6 @@ describe("NumberMapping", () => { it('should not validate when mapper is "ROUND_NUMBER" and divider attribute is evaluated', () => { const invalidData = { - inputFieldName: "foo", divider: 2, mapper: "ROUND_NUMBER" }; diff --git a/src/types/__tests__/renameField.test.ts b/src/types/__tests__/renameField.test.ts index 18ecbc5..6223288 100644 --- a/src/types/__tests__/renameField.test.ts +++ b/src/types/__tests__/renameField.test.ts @@ -4,7 +4,6 @@ import { RenameFieldMapping, RenameMapping } from "../renameField"; describe("RenameFieldMapping", () => { it("should decode if config is a valid RenameFieldMapping", () => { const validData = { - inputFieldName: "foo", newFieldName: "fooo", mapper: "RENAME_FIELD" }; @@ -13,8 +12,7 @@ describe("RenameFieldMapping", () => { }); it("should not validate if config is not a valid input", () => { const invalidData = { - invalidInputField: "foo", - newFieldName: "fooo", + newFieldName: "", mapper: "RENAME_FIELD" }; const result = RenameFieldMapping.decode(invalidData); @@ -22,7 +20,6 @@ describe("RenameFieldMapping", () => { }); it("should not validate if mapper is not 'RENAME_FIELD'", () => { const invalidData = { - inputFieldName: "foo", newFieldName: "fooo", mapper: "INVALID_MAPPER" }; @@ -48,7 +45,6 @@ describe("RenameMapping", () => { it("should decode if config is a valid RenameMapping instance of RenameField mapper", () => { const validData = { - inputFieldName: "foo", newFieldName: "fooo", mapper: "RENAME_FIELD" }; diff --git a/src/types/__tests__/singleInput.test.ts b/src/types/__tests__/singleInput.test.ts index 5fad3f1..e5b4b7d 100644 --- a/src/types/__tests__/singleInput.test.ts +++ b/src/types/__tests__/singleInput.test.ts @@ -17,6 +17,13 @@ const invalidNumberCaseMapping = { outputFieldName: "bar" }; +const anotherInvalidNumberCaseMapping = { + type: "SINGLE_INPUT", + divider: 10, + mapper: "DIVIDE_NUMBER", + outputFieldName: "bar" +}; + const stringCaseMapping = { type: "SINGLE_INPUT", inputFieldName: "foo", @@ -82,10 +89,23 @@ const invalidRenameFieldMapping = { const dateMapping = { type: "SINGLE_INPUT", inputFieldName: "foo", - dateString: "2023-12-19", mapper: "DATE_TO_ISO" }; +const convertFormatMapping = { + type: "SINGLE_INPUT", + inputFieldName: "foo", + mapper: "CONVERT_FORMAT", + output: "yyyy-MM-dd" +}; + +const invalidConvertFormatMapping = { + type: "SINGLE_INPUT", + inputFieldName: "foo", + mapper: "CONVERT_FORMAT", + output: "invalid output string" +}; + describe("SingleInputMapping", () => { it("should decode a correct numberCaseMapping type properly", () => { const res = SingleInputMapping.decode(numberCaseMapping); @@ -111,6 +131,10 @@ describe("SingleInputMapping", () => { const res = SingleInputMapping.decode(invalidNumberCaseMapping); expect(E.isLeft(res)).toBeTruthy(); }); + it("should not decode another invalid numberCaseMapping properly", () => { + const res = SingleInputMapping.decode(anotherInvalidNumberCaseMapping); + expect(E.isLeft(res)).toBeTruthy(); + }); it("should not decode an invalid stringCaseMapping type properly", () => { const res = SingleInputMapping.decode(invalidStringCaseMapping); expect(E.isLeft(res)).toBeTruthy(); @@ -131,4 +155,12 @@ describe("SingleInputMapping", () => { const res = SingleInputMapping.decode(dateMapping); expect(E.isRight(res)).toBeTruthy(); }); + it("should decode a correct convertFormatMapping type properly", () => { + const res = SingleInputMapping.decode(convertFormatMapping); + expect(E.isRight(res)).toBeTruthy(); + }); + it("should not decode an invalid convertFormatMapping type properly", () => { + const res = SingleInputMapping.decode(invalidConvertFormatMapping); + expect(E.isLeft(res)).toBeTruthy(); + }); }); diff --git a/src/types/__tests__/string.test.ts b/src/types/__tests__/string.test.ts index 66c2f71..8e68486 100644 --- a/src/types/__tests__/string.test.ts +++ b/src/types/__tests__/string.test.ts @@ -2,41 +2,34 @@ import * as E from "fp-ts/Either"; import { StringMapping } from "../string"; const upperCaseMapping = { - inputFieldName: "foo", mapper: "UPPER_CASE" }; const lowerCaseMapping = { - inputFieldName: "foo", mapper: "LOWER_CASE" }; const capitalizeMapping = { - inputFieldName: "foo", mapper: "CAPITALIZE" }; const trimMapping = { - inputFieldName: "foo", mapper: "TRIM" }; const replaceMapping = { - inputFieldName: "foo", mapper: "REPLACE", placeholder: "a", toBeReplaced: "b" }; const replaceAllMapping = { - inputFieldName: "foo", mapper: "REPLACE", placeholder: "a", toBeReplaced: "b" }; const invalidMapping = { - inputFieldName: "foo", mapper: "INVALID" }; diff --git a/src/types/boolean.ts b/src/types/boolean.ts index 4b06ce2..a850d1f 100644 --- a/src/types/boolean.ts +++ b/src/types/boolean.ts @@ -1,9 +1,7 @@ -import { NonEmptyString } from "@pagopa/ts-commons/lib/strings"; import * as t from "io-ts"; const BooleanToStringMapping = t.type({ falseString: t.string, - inputFieldName: NonEmptyString, mapper: t.literal("BOOLEAN_TO_STRING"), trueString: t.string }); diff --git a/src/types/case.ts b/src/types/case.ts index 035dd74..b4f4386 100644 --- a/src/types/case.ts +++ b/src/types/case.ts @@ -1,10 +1,8 @@ -import { NonEmptyString } from "@pagopa/ts-commons/lib/strings"; import * as t from "io-ts"; export const SwitchCaseMapping = t.type({ cases: t.record(t.string, t.unknown), - defaultValue: t.unknown, - inputFieldName: NonEmptyString + defaultValue: t.unknown }); export type SwitchCaseMapping = t.TypeOf; diff --git a/src/types/date.ts b/src/types/date.ts index 19d49ea..6746b8d 100644 --- a/src/types/date.ts +++ b/src/types/date.ts @@ -1,9 +1,7 @@ -import { NonEmptyString } from "@pagopa/ts-commons/lib/strings"; import * as t from "io-ts"; +import { OutputFormat } from "../formatter/date"; const DateStringToUtcFormatMapping = t.type({ - dateString: NonEmptyString, - inputFieldName: NonEmptyString, mapper: t.literal("DATE_TO_UTC") }); @@ -12,16 +10,12 @@ type DateStringToUtcFormatMapping = t.TypeOf< >; const IsoToUtcFormatMapping = t.type({ - inputFieldName: NonEmptyString, - isoString: NonEmptyString, mapper: t.literal("ISO_TO_UTC") }); type IsoToUtcFormatMapping = t.TypeOf; const DateStringToIsoFormatMapping = t.type({ - dateString: NonEmptyString, - inputFieldName: NonEmptyString, mapper: t.literal("DATE_TO_ISO") }); type DateStringToIsoFormatMapping = t.TypeOf< @@ -29,8 +23,6 @@ type DateStringToIsoFormatMapping = t.TypeOf< >; const DateStringToTimestampFormatMapping = t.type({ - dateString: NonEmptyString, - inputFieldName: NonEmptyString, mapper: t.literal("DATE_TO_TIMESTAMP") }); type DateStringToTimestampFormatMapping = t.TypeOf< @@ -38,11 +30,15 @@ type DateStringToTimestampFormatMapping = t.TypeOf< >; const DateStringFromTimestampFormatMapping = t.type({ - inputFieldName: NonEmptyString, - mapper: t.literal("DATE_FROM_TIMESTAMP"), - timestamp: t.number + mapper: t.literal("DATE_FROM_TIMESTAMP") }); +const CovertFormat = t.type({ + output: OutputFormat +}); + +type CovertFormat = t.TypeOf; + type DateStringFromTimestampFormatMapping = t.TypeOf< typeof DateStringFromTimestampFormatMapping >; @@ -52,5 +48,6 @@ export const DateMapping = t.union([ IsoToUtcFormatMapping, DateStringToIsoFormatMapping, DateStringToTimestampFormatMapping, - DateStringFromTimestampFormatMapping + DateStringFromTimestampFormatMapping, + CovertFormat ]); diff --git a/src/types/number.ts b/src/types/number.ts index 15a9edd..2777db9 100644 --- a/src/types/number.ts +++ b/src/types/number.ts @@ -1,8 +1,6 @@ -import { NonEmptyString } from "@pagopa/ts-commons/lib/strings"; import * as t from "io-ts"; const MultiplyMapping = t.type({ - inputFieldName: NonEmptyString, mapper: t.literal("MULTIPLY_NUMBER"), multiplier: t.number }); @@ -11,7 +9,6 @@ type MultiplyMapping = t.TypeOf; const DivideMapping = t.type({ divider: t.number, - inputFieldName: NonEmptyString, mapper: t.literal("DIVIDE_NUMBER") }); @@ -19,7 +16,6 @@ type DivideMapping = t.TypeOf; const RoundMapping = t.type({ decimals: t.number, - inputFieldName: NonEmptyString, mapper: t.literal("ROUND_NUMBER") }); diff --git a/src/types/renameField.ts b/src/types/renameField.ts index 8a97bbd..a093bdb 100644 --- a/src/types/renameField.ts +++ b/src/types/renameField.ts @@ -2,7 +2,6 @@ import { NonEmptyString } from "@pagopa/ts-commons/lib/strings"; import * as t from "io-ts"; export const RenameFieldConfig = t.type({ - inputFieldName: NonEmptyString, newFieldName: NonEmptyString }); export type RenameFieldConfig = t.TypeOf; diff --git a/src/types/singleInput.ts b/src/types/singleInput.ts index 0507ca9..9fb1ca6 100644 --- a/src/types/singleInput.ts +++ b/src/types/singleInput.ts @@ -1,3 +1,4 @@ +import { NonEmptyString } from "@pagopa/ts-commons/lib/strings"; import * as t from "io-ts"; import { BooleanMapping } from "./boolean"; import { SwitchCaseMapping } from "./case"; @@ -10,11 +11,19 @@ const inputType = t.type({ type: t.literal("SINGLE_INPUT") }); +const inputFieldName = t.type({ + inputFieldName: NonEmptyString +}); + const OutputFieldName = t.partial({ outputFieldName: t.string }); -export const SingleInputConfig = t.intersection([inputType, OutputFieldName]); +export const SingleInputConfig = t.intersection([ + inputType, + inputFieldName, + OutputFieldName +]); export const SingleInputNumberMapping = t.intersection([ SingleInputConfig, diff --git a/src/types/string.ts b/src/types/string.ts index 8ed2d52..658c55c 100644 --- a/src/types/string.ts +++ b/src/types/string.ts @@ -1,36 +1,30 @@ -import { NonEmptyString } from "@pagopa/ts-commons/lib/strings"; import * as t from "io-ts"; const UpperCaseMapping = t.type({ - inputFieldName: NonEmptyString, mapper: t.literal("UPPER_CASE") }); type UpperCaseMapping = t.TypeOf; const LowerCaseMapping = t.type({ - inputFieldName: NonEmptyString, mapper: t.literal("LOWER_CASE") }); type LowerCaseMapping = t.TypeOf; const CapitalizeMapping = t.type({ - inputFieldName: NonEmptyString, mapper: t.literal("CAPITALIZE") }); type CapitalizeMapping = t.TypeOf; const TrimMapping = t.type({ - inputFieldName: NonEmptyString, mapper: t.literal("TRIM") }); type TrimMapping = t.TypeOf; const ReplaceMapping = t.type({ - inputFieldName: NonEmptyString, mapper: t.literal("REPLACE"), placeholder: t.string, toBeReplaced: t.string @@ -39,7 +33,6 @@ const ReplaceMapping = t.type({ type ReplaceMapping = t.TypeOf; const ReplaceAllMapping = t.type({ - inputFieldName: NonEmptyString, mapper: t.literal("REPLACE_ALL"), placeholder: t.string, toBeReplaced: t.string