Skip to content

Commit

Permalink
date decoders
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanoFrontini committed Jan 23, 2024
1 parent eaadc78 commit 5ff18ed
Show file tree
Hide file tree
Showing 12 changed files with 154 additions and 3 deletions.
2 changes: 2 additions & 0 deletions src/types/__tests__/boolean.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BooleanMapping } from "../boolean";
describe("BooleanToStringMapping", () => {
it("should validate with correct values", () => {
const validMapping = {
inputFieldName: "foo",
falseString: "false",
mapper: "BOOLEAN_TO_STRING",
trueString: "true"
Expand All @@ -16,6 +17,7 @@ describe("BooleanToStringMapping", () => {

it("should not validate with incorrect values", () => {
const invalidMapping = {
inputFieldName: "foo",
falseString: 123,
mapper: "BOOLEAN_TO_STRING",
trueString: "true"
Expand Down
2 changes: 2 additions & 0 deletions src/types/__tests__/case.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ describe("SwitchCaseMapping", () => {
case1: "value1",
case2: "value2"
},
inputFieldName: "foo",
defaultValue: "default"
};
const result = SwitchCaseMapping.decode(validMapping);
expect(E.isRight(result)).toBeTruthy();
});
it("should not decode if cases is not an object", () => {
const invalidMapping = {
inputFieldName: "foo",
cases: "invalidCases",
defaultValue: "default"
};
Expand Down
40 changes: 40 additions & 0 deletions src/types/__tests__/date.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as E from "fp-ts/Either";
import { DateMapping } from "../date";

describe("DateMapping", () => {
it("should decode a correct dateMapping 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"
};
const res = DateMapping.decode(invalidData);
expect(E.isLeft(res)).toBeTruthy();
});
it("should not decode if dateString is not a string", () => {
const invalidData = {
inputFieldName: "foo",
dateString: 2023 - 12 - 19,
mapper: "DATE_TO_ISO"
};
const res = DateMapping.decode(invalidData);
expect(E.isLeft(res)).toBeTruthy();
});
it("should not decode if inpuFieldName is missing", () => {
const invalidData = {
dateString: "2023-12-19",
mapper: "DATE_TO_ISO"
};
const res = DateMapping.decode(invalidData);
expect(E.isLeft(res)).toBeTruthy();
});
});
11 changes: 11 additions & 0 deletions src/types/__tests__/number.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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"
};
Expand All @@ -13,6 +14,7 @@ describe("NumberMapping", () => {
});
it("should not validate when multiplier is not a number", () => {
const invalidData = {
inputFieldName: "foo",
multiplier: "invalid",
mapper: "MULTIPLY_NUMBER"
};
Expand All @@ -21,6 +23,7 @@ describe("NumberMapping", () => {
});
it("should not validate when mapper is not 'MULTIPLY_NUMBER'", () => {
const invalidData = {
inputFieldName: "foo",
multiplier: 5,
mapper: "INVALID_MAPPER"
};
Expand All @@ -31,6 +34,7 @@ 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"
};
Expand All @@ -42,6 +46,7 @@ describe("NumberMapping", () => {

it("should not validate when divider is not a number", () => {
const invalidData = {
inputFieldName: "foo",
divider: "invalid",
mapper: "DIVIDE_NUMBER"
};
Expand All @@ -53,6 +58,7 @@ describe("NumberMapping", () => {

it('should not validate when mapper is not "DIVIDE_NUMBER"', () => {
const invalidData = {
inputFieldName: "foo",
divider: 5,
mapper: "INVALID_MAPPER"
};
Expand All @@ -64,6 +70,7 @@ 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"
};
Expand All @@ -77,6 +84,7 @@ 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"
};
Expand All @@ -88,6 +96,7 @@ describe("NumberMapping", () => {

it("should not validate when decimals is not a number", () => {
const invalidData = {
inputFieldName: "foo",
decimals: "5",
mapper: "ROUND_NUMBER"
};
Expand All @@ -99,6 +108,7 @@ describe("NumberMapping", () => {

it('should not validate when mapper is not "ROUND_NUMBER"', () => {
const invalidData = {
inputFieldName: "foo",
decimals: 5,
mapper: "INVALID"
};
Expand All @@ -110,6 +120,7 @@ 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"
};
Expand Down
11 changes: 11 additions & 0 deletions src/types/__tests__/singleInput.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ const invalidRenameFieldMapping = {
newFieldName: "bar"
};

const dateMapping = {
type: "SINGLE_INPUT",
inputFieldName: "foo",
dateString: "2023-12-19",
mapper: "DATE_TO_ISO"
};

describe("SingleInputMapping", () => {
it("should decode a correct numberCaseMapping type properly", () => {
const res = SingleInputMapping.decode(numberCaseMapping);
Expand Down Expand Up @@ -120,4 +127,8 @@ describe("SingleInputMapping", () => {
const res = SingleInputMapping.decode(invalidRenameFieldMapping);
expect(E.isLeft(res)).toBeTruthy();
});
it("should decode a correct dateMapping type properly", () => {
const res = SingleInputMapping.decode(dateMapping);
expect(E.isRight(res)).toBeTruthy();
});
});
9 changes: 8 additions & 1 deletion src/types/__tests__/string.test.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
import * as E from "fp-ts/lib/Either";
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"
};

Expand Down
2 changes: 2 additions & 0 deletions src/types/boolean.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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
});
Expand Down
4 changes: 3 additions & 1 deletion src/types/case.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
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
defaultValue: t.unknown,
inputFieldName: NonEmptyString
});

export type SwitchCaseMapping = t.TypeOf<typeof SwitchCaseMapping>;
56 changes: 56 additions & 0 deletions src/types/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { NonEmptyString } from "@pagopa/ts-commons/lib/strings";
import * as t from "io-ts";

const DateStringToUtcFormatMapping = t.type({
dateString: NonEmptyString,
inputFieldName: NonEmptyString,
mapper: t.literal("DATE_TO_UTC")
});

type DateStringToUtcFormatMapping = t.TypeOf<
typeof DateStringToUtcFormatMapping
>;

const IsoToUtcFormatMapping = t.type({
inputFieldName: NonEmptyString,
isoString: NonEmptyString,
mapper: t.literal("ISO_TO_UTC")
});

type IsoToUtcFormatMapping = t.TypeOf<typeof IsoToUtcFormatMapping>;

const DateStringToIsoFormatMapping = t.type({
dateString: NonEmptyString,
inputFieldName: NonEmptyString,
mapper: t.literal("DATE_TO_ISO")
});
type DateStringToIsoFormatMapping = t.TypeOf<
typeof DateStringToIsoFormatMapping
>;

const DateStringToTimestampFormatMapping = t.type({
dateString: NonEmptyString,
inputFieldName: NonEmptyString,
mapper: t.literal("DATE_TO_TIMESTAMP")
});
type DateStringToTimestampFormatMapping = t.TypeOf<
typeof DateStringToTimestampFormatMapping
>;

const DateStringFromTimestampFormatMapping = t.type({
inputFieldName: NonEmptyString,
mapper: t.literal("DATE_FROM_TIMESTAMP"),
timestamp: t.number
});

type DateStringFromTimestampFormatMapping = t.TypeOf<
typeof DateStringFromTimestampFormatMapping
>;

export const DateMapping = t.union([
DateStringToUtcFormatMapping,
IsoToUtcFormatMapping,
DateStringToIsoFormatMapping,
DateStringToTimestampFormatMapping,
DateStringFromTimestampFormatMapping
]);
4 changes: 4 additions & 0 deletions src/types/number.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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
});
Expand All @@ -9,13 +11,15 @@ type MultiplyMapping = t.TypeOf<typeof MultiplyMapping>;

const DivideMapping = t.type({
divider: t.number,
inputFieldName: NonEmptyString,
mapper: t.literal("DIVIDE_NUMBER")
});

type DivideMapping = t.TypeOf<typeof DivideMapping>;

const RoundMapping = t.type({
decimals: t.number,
inputFieldName: NonEmptyString,
mapper: t.literal("ROUND_NUMBER")
});

Expand Down
9 changes: 8 additions & 1 deletion src/types/singleInput.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as t from "io-ts";
import { BooleanMapping } from "./boolean";
import { SwitchCaseMapping } from "./case";
import { DateMapping } from "./date";
import { NumberMapping } from "./number";
import { RenameFieldMapping } from "./renameField";
import { StringMapping } from "./string";
Expand Down Expand Up @@ -40,12 +41,18 @@ export const SingleInputSwitchCaseMapping = t.intersection([
SwitchCaseMapping
]);

export const SingleInputDateMapping = t.intersection([
SingleInputConfig,
DateMapping
]);

export const SingleInputMapping = t.union([
SingleInputNumberMapping,
SingleInputStringMapping,
SingleInputBooleanMapping,
SingleInputRenameFieldMapping,
SingleInputSwitchCaseMapping
SingleInputSwitchCaseMapping,
SingleInputDateMapping
]);

export type SingleInputMapping = t.TypeOf<typeof SingleInputMapping>;
Loading

0 comments on commit 5ff18ed

Please sign in to comment.