Skip to content

Commit

Permalink
feat: update to io-ts 2.2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
douglasduteil committed Jul 1, 2020
1 parent 71b2bc2 commit 400704b
Show file tree
Hide file tree
Showing 19 changed files with 211 additions and 140 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -6,7 +6,7 @@
"bugs": "https://github.com/SocialGouv/kosko-charts/issues",
"dependencies": {
"fp-ts": "^2.6.6",
"io-ts": "2.2.6"
"io-ts": "2.2.7"
},
"devDependencies": {
"@babel/core": "^7.10.3",
Expand Down
34 changes: 16 additions & 18 deletions src/components/app/__snapshots__/index.test.ts.snap
Expand Up @@ -320,25 +320,23 @@ Object {
`;

exports[`should throw because of missing variables 1`] = `
"BadArgument:
├─ required property \\"containerPort\\"
│ └─ cannot decode undefined, should be number
├─ required property \\"image\\"
│ └─ cannot decode undefined, should be Record<string, unknown>
├─ required property \\"name\\"
│ └─ cannot decode undefined, should be string
├─ required property \\"namespace\\"
│ └─ cannot decode undefined, should be Record<string, unknown>
└─ required property \\"servicePort\\"
└─ cannot decode undefined, should be number"
"BadArgument:required property \\"containerPort\\"
└─ cannot decode undefined, should be number
required property \\"image\\"
└─ cannot decode undefined, should be object
required property \\"name\\"
└─ cannot decode undefined, should be string
required property \\"namespace\\"
└─ cannot decode undefined, should be object
required property \\"servicePort\\"
└─ cannot decode undefined, should be number"
`;

exports[`should throw because the containerPort is not an integer 1`] = `
"BadArgument:
├─ required property \\"containerPort\\"
│ └─ cannot refine 1234.56789, should be Integer
├─ required property \\"name\\"
│ └─ cannot decode undefined, should be string
└─ required property \\"servicePort\\"
└─ cannot refine 1234.56789, should be Integer"
"BadArgument:required property \\"containerPort\\"
└─ cannot decode 1234.56789, should be Integer
required property \\"name\\"
└─ cannot decode undefined, should be string
required property \\"servicePort\\"
└─ cannot decode 1234.56789, should be Integer"
`;
21 changes: 12 additions & 9 deletions src/components/app/params.ts
@@ -1,9 +1,10 @@
import { GlobalEnvironment } from "@socialgouv/kosko-charts/types";
import { Integer } from "@socialgouv/kosko-charts/utils/Integer";
import { NonEmptyString } from "@socialgouv/kosko-charts/utils/NonEmptyString";
import { pipe } from "fp-ts/lib/pipeable";
import * as D from "io-ts/lib/Decoder";

export const AppComponentParams = D.intersection(
export const AppComponentParams = pipe(
D.type({
containerPort: Integer,
image: D.type({
Expand All @@ -16,14 +17,16 @@ export const AppComponentParams = D.intersection(
}),
servicePort: Integer,
}),
D.partial({
ingress: D.partial({
secretName: D.string,
}),
labels: D.record(D.string),
limits: D.type({ cpu: D.string, memory: D.string }),
requests: D.type({ cpu: D.string, memory: D.string }),
})
D.intersect(
D.partial({
ingress: D.partial({
secretName: D.string,
}),
labels: D.record(D.string),
limits: D.type({ cpu: D.string, memory: D.string }),
requests: D.type({ cpu: D.string, memory: D.string }),
})
)
);

export type AppComponentEnvironment = Omit<
Expand Down
35 changes: 16 additions & 19 deletions src/components/azure-db/__snapshots__/index.test.ts.snap
Expand Up @@ -70,31 +70,28 @@ Object {
`;

exports[`should throw because of missing variables 1`] = `
"BadArgument:
├─ required property \\"database\\"
│ └─ cannot decode undefined, should be string
├─ required property \\"password\\"
│ └─ cannot decode undefined, should be string
└─ required property \\"user\\"
└─ cannot decode undefined, should be string"
"BadArgument:required property \\"database\\"
└─ cannot decode undefined, should be string
required property \\"password\\"
└─ cannot decode undefined, should be string
required property \\"user\\"
└─ cannot decode undefined, should be string"
`;

exports[`should throw because of missing variables 2`] = `
"BadArgument:
├─ required property \\"database\\"
│ └─ cannot decode undefined, should be string
└─ required property \\"password\\"
└─ cannot decode undefined, should be string"
"BadArgument:required property \\"database\\"
└─ cannot decode undefined, should be string
required property \\"password\\"
└─ cannot decode undefined, should be string"
`;

exports[`should throw because of missing variables 3`] = `
"BadArgument:
├─ required property \\"database\\"
│ └─ cannot refine \\"\\", should be NonEmptyString
├─ required property \\"password\\"
│ └─ cannot refine \\"\\", should be NonEmptyString
└─ required property \\"user\\"
└─ cannot refine \\"\\", should be NonEmptyString"
"BadArgument:required property \\"database\\"
└─ cannot decode \\"\\", should be NonEmptyString
required property \\"password\\"
└─ cannot decode \\"\\", should be NonEmptyString
required property \\"user\\"
└─ cannot decode \\"\\", should be NonEmptyString"
`;

exports[`should use custom extensions 1`] = `
Expand Down
11 changes: 7 additions & 4 deletions src/components/azure-db/params.ts
Expand Up @@ -3,17 +3,20 @@ import {
NamedComponentEnvironment,
} from "@socialgouv/kosko-charts/types";
import { NonEmptyString } from "@socialgouv/kosko-charts/utils/NonEmptyString";
import { pipe } from "fp-ts/lib/pipeable";
import * as D from "io-ts/lib/Decoder";

export const CreateDbComponentParams = D.intersection(
export const CreateDbComponentParams = pipe(
D.type({
database: NonEmptyString,
password: NonEmptyString,
user: NonEmptyString,
}),
D.partial({
extensions: NonEmptyString,
})
D.intersect(
D.partial({
extensions: NonEmptyString,
})
)
);

export interface CreateDbJobParameters extends NamedComponentEnvironment {
Expand Down
12 changes: 5 additions & 7 deletions src/components/namespace/__snapshots__/index.test.ts.snap
Expand Up @@ -38,14 +38,12 @@ Object {
`;

exports[`should throw because of missing variables 1`] = `
"BadArgument:
└─ required property \\"namespace\\"
└─ cannot decode undefined, should be Record<string, unknown>"
"BadArgument:required property \\"namespace\\"
└─ cannot decode undefined, should be object"
`;

exports[`should throw because the namespace name is emptys 1`] = `
"BadArgument:
└─ required property \\"namespace\\"
└─ required property \\"name\\"
└─ cannot refine \\"\\", should be NonEmptyString"
"BadArgument:required property \\"namespace\\"
└─ required property \\"name\\"
└─ cannot decode \\"\\", should be NonEmptyString"
`;
12 changes: 7 additions & 5 deletions src/components/namespace/index.ts
Expand Up @@ -11,16 +11,18 @@ import { Namespace } from "kubernetes-models/v1/Namespace";

export type Params = NamespaceComponentEnvironment & GlobalEnvironment;

const NamespaceComponentParams = D.intersection(
const NamespaceComponentParams = pipe(
D.type({
namespace: D.type({
name: NonEmptyString,
}),
}),
D.partial({
annotations: D.record(D.string),
labels: D.record(D.string),
})
D.intersect(
D.partial({
annotations: D.record(D.string),
labels: D.record(D.string),
})
)
);
type NamespaceComponentParams = D.TypeOf<typeof NamespaceComponentParams>;

Expand Down
43 changes: 20 additions & 23 deletions src/components/pg-secret/__snapshots__/index.test.ts.snap
Expand Up @@ -26,33 +26,30 @@ Object {
`;

exports[`should throw because of missing variables 1`] = `
"BadArgument:
├─ required property \\"database\\"
│ └─ cannot decode undefined, should be string
├─ required property \\"host\\"
│ └─ cannot decode undefined, should be string
├─ required property \\"password\\"
│ └─ cannot decode undefined, should be string
└─ required property \\"user\\"
└─ cannot decode undefined, should be string"
"BadArgument:required property \\"database\\"
└─ cannot decode undefined, should be string
required property \\"host\\"
└─ cannot decode undefined, should be string
required property \\"password\\"
└─ cannot decode undefined, should be string
required property \\"user\\"
└─ cannot decode undefined, should be string"
`;

exports[`should throw because of missing variables 2`] = `
"BadArgument:
├─ required property \\"database\\"
│ └─ cannot decode undefined, should be string
├─ required property \\"host\\"
│ └─ cannot decode undefined, should be string
└─ required property \\"password\\"
└─ cannot decode undefined, should be string"
"BadArgument:required property \\"database\\"
└─ cannot decode undefined, should be string
required property \\"host\\"
└─ cannot decode undefined, should be string
required property \\"password\\"
└─ cannot decode undefined, should be string"
`;

exports[`should throw because of missing variables 3`] = `
"BadArgument:
├─ required property \\"database\\"
│ └─ cannot refine \\"\\", should be NonEmptyString
├─ required property \\"password\\"
│ └─ cannot refine \\"\\", should be NonEmptyString
└─ required property \\"user\\"
└─ cannot refine \\"\\", should be NonEmptyString"
"BadArgument:required property \\"database\\"
└─ cannot decode \\"\\", should be NonEmptyString
required property \\"password\\"
└─ cannot decode \\"\\", should be NonEmptyString
required property \\"user\\"
└─ cannot decode \\"\\", should be NonEmptyString"
`;
11 changes: 7 additions & 4 deletions src/components/pg-secret/params.ts
Expand Up @@ -3,18 +3,21 @@ import {
NamedComponentEnvironment,
} from "@socialgouv/kosko-charts/types";
import { NonEmptyString } from "@socialgouv/kosko-charts/utils/NonEmptyString";
import { pipe } from "fp-ts/lib/pipeable";
import * as D from "io-ts/lib/Decoder";

export const PostresSecretParameters = D.intersection(
export const PostresSecretParameters = pipe(
D.type({
database: NonEmptyString,
host: NonEmptyString,
password: NonEmptyString,
user: NonEmptyString,
}),
D.partial({
sslmode: NonEmptyString,
})
D.intersect(
D.partial({
sslmode: NonEmptyString,
})
)
);

export interface PostresSecretParameters extends NamedComponentEnvironment {
Expand Down
54 changes: 26 additions & 28 deletions src/environments/gitlab/__snapshots__/index.test.ts.snap
Expand Up @@ -61,35 +61,33 @@ Object {
`;

exports[`should throw because of empty strings 1`] = `
"BadArgument:
├─ required property \\"CI_ENVIRONMENT_NAME\\"
│ └─ cannot refine \\"\\", should be NonEmptyString
├─ required property \\"CI_ENVIRONMENT_SLUG\\"
│ └─ cannot refine \\"\\", should be NonEmptyString
├─ required property \\"CI_PROJECT_NAME\\"
│ └─ cannot refine \\"\\", should be NonEmptyString
├─ required property \\"CI_PROJECT_PATH_SLUG\\"
│ └─ cannot refine \\"\\", should be NonEmptyString
├─ required property \\"KUBE_INGRESS_BASE_DOMAIN\\"
│ └─ cannot refine \\"\\", should be NonEmptyString
├─ required property \\"KUBE_NAMESPACE\\"
│ └─ cannot refine \\"\\", should be NonEmptyString
└─ optional property \\"CI_COMMIT_TAG\\"
└─ cannot refine \\"\\", should be NonEmptyString"
"BadArgument:required property \\"CI_ENVIRONMENT_NAME\\"
└─ cannot decode \\"\\", should be NonEmptyString
required property \\"CI_ENVIRONMENT_SLUG\\"
└─ cannot decode \\"\\", should be NonEmptyString
required property \\"CI_PROJECT_NAME\\"
└─ cannot decode \\"\\", should be NonEmptyString
required property \\"CI_PROJECT_PATH_SLUG\\"
└─ cannot decode \\"\\", should be NonEmptyString
required property \\"KUBE_INGRESS_BASE_DOMAIN\\"
└─ cannot decode \\"\\", should be NonEmptyString
required property \\"KUBE_NAMESPACE\\"
└─ cannot decode \\"\\", should be NonEmptyString
optional property \\"CI_COMMIT_TAG\\"
└─ cannot decode \\"\\", should be NonEmptyString"
`;

exports[`should throw because of missing variables 1`] = `
"BadArgument:
├─ required property \\"CI_ENVIRONMENT_NAME\\"
│ └─ cannot decode undefined, should be string
├─ required property \\"CI_ENVIRONMENT_SLUG\\"
│ └─ cannot decode undefined, should be string
├─ required property \\"CI_PROJECT_NAME\\"
│ └─ cannot decode undefined, should be string
├─ required property \\"CI_PROJECT_PATH_SLUG\\"
│ └─ cannot decode undefined, should be string
├─ required property \\"KUBE_INGRESS_BASE_DOMAIN\\"
│ └─ cannot decode undefined, should be string
└─ required property \\"KUBE_NAMESPACE\\"
└─ cannot decode undefined, should be string"
"BadArgument:required property \\"CI_ENVIRONMENT_NAME\\"
└─ cannot decode undefined, should be string
required property \\"CI_ENVIRONMENT_SLUG\\"
└─ cannot decode undefined, should be string
required property \\"CI_PROJECT_NAME\\"
└─ cannot decode undefined, should be string
required property \\"CI_PROJECT_PATH_SLUG\\"
└─ cannot decode undefined, should be string
required property \\"KUBE_INGRESS_BASE_DOMAIN\\"
└─ cannot decode undefined, should be string
required property \\"KUBE_NAMESPACE\\"
└─ cannot decode undefined, should be string"
`;
15 changes: 8 additions & 7 deletions src/environments/gitlab/index.ts
Expand Up @@ -5,7 +5,7 @@ import { fold } from "fp-ts/lib/Either";
import { pipe } from "fp-ts/lib/pipeable";
import * as D from "io-ts/lib/Decoder";

const GitlabProcessEnv = D.intersection(
const GitlabProcessEnv = pipe(
D.type({
CI_ENVIRONMENT_NAME: NonEmptyString,
CI_ENVIRONMENT_SLUG: NonEmptyString,
Expand All @@ -14,10 +14,12 @@ const GitlabProcessEnv = D.intersection(
KUBE_INGRESS_BASE_DOMAIN: NonEmptyString,
KUBE_NAMESPACE: NonEmptyString,
}),
D.partial({
CI_COMMIT_TAG: NonEmptyString,
PRODUCTION: D.string,
})
D.intersect(
D.partial({
CI_COMMIT_TAG: NonEmptyString,
PRODUCTION: D.string,
})
)
);

type GitlabProcessEnv = D.TypeOf<typeof GitlabProcessEnv>;
Expand All @@ -33,10 +35,9 @@ const mapper = ({
PRODUCTION,
}: GitlabProcessEnv): GlobalEnvironment => {
const isProductionCluster = Boolean(PRODUCTION);

const application = isProductionCluster
? CI_PROJECT_NAME
: CI_COMMIT_TAG !== undefined
: CI_COMMIT_TAG
? `${CI_COMMIT_TAG.replace(/\./g, "-")}-${CI_PROJECT_NAME}`
: `${CI_ENVIRONMENT_SLUG}-${CI_PROJECT_NAME}`;

Expand Down
11 changes: 7 additions & 4 deletions src/utils/Integer.ts
@@ -1,7 +1,10 @@
import { Decoder, number, refinement } from "io-ts/lib/Decoder";
import { pipe } from "fp-ts/lib/pipeable";
import { Decoder, number, refine } from "io-ts/lib/Decoder";

export const Integer: Decoder<number> = refinement(
export const Integer: Decoder<number> = pipe(
number,
(n: unknown): n is number => typeof n === "number" && Number.isInteger(n),
"Integer"
refine(
(n: unknown): n is number => typeof n === "number" && Number.isInteger(n),
"Integer"
)
);

0 comments on commit 400704b

Please sign in to comment.