/
index.ts
129 lines (107 loc) · 3.17 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { Region } from "@takomo/aws-model"
import Joi, { AnySchema, ArraySchema, ObjectSchema, StringSchema } from "joi"
export interface AwsSchemas {
region: StringSchema
regions: (StringSchema | ArraySchema)[]
stackName: StringSchema
iamRoleName: StringSchema
iamRoleArn: StringSchema
accountIds: ArraySchema
accountId: StringSchema
parameterName: StringSchema
tags: ObjectSchema
stackOutputName: StringSchema
stackCapability: StringSchema
stackCapabilities: (StringSchema | ArraySchema)[]
tagName: StringSchema
tagValue: AnySchema
accountEmail: StringSchema
accountName: StringSchema
accountAlias: StringSchema
}
interface CreateAwsSchemasProps {
readonly regions: ReadonlyArray<Region>
}
export const createAwsSchemas = (props: CreateAwsSchemasProps): AwsSchemas => {
const stackCapability = Joi.string().valid(
"CAPABILITY_IAM",
"CAPABILITY_NAMED_IAM",
"CAPABILITY_AUTO_EXPAND",
)
const stackCapabilities = [
stackCapability,
Joi.array().items(stackCapability).unique(),
]
const tagName = Joi.string().min(1).max(127)
const tagValue = Joi.any()
.custom((value, helpers) => {
const type = typeof value
if (!["string", "number", "boolean"].includes(type)) {
return helpers.error("invalidType")
}
const stringValue = `${value}`
if (stringValue === "") {
return helpers.error("empty")
}
const limit = 255
if (stringValue.length > limit) {
return helpers.error("maxLength", { limit })
}
return stringValue
})
.messages({
invalidType: "{{#label}} must be a string, number or boolean",
empty: "{{#label}} is not allowed to be empty",
maxLength:
"{{#label}} length must be less than or equal to {{#limit}} characters long",
})
const tags = Joi.object().pattern(tagName, tagValue)
// TODO: Validate parameter name allowed characters
const parameterName = Joi.string()
.min(1)
.max(255)
.regex(/^[a-zA-Z0-9]+$/)
const stackOutputName = Joi.string()
.min(1)
.max(255)
.regex(/^[a-zA-Z0-9]+$/)
const stackName = Joi.string()
.min(1)
.max(128)
.regex(/^[a-zA-Z][a-zA-Z0-9-]*$/)
const accountId = Joi.string().regex(/^\d{12}$/)
const accountIds = Joi.array().items(accountId).unique()
const iamRoleName = Joi.string()
.min(1)
.max(64)
.regex(/^[\w+=/,.@-]+$/)
const iamRoleArn = Joi.string().regex(/^arn:aws:iam::\d{12}:role\/.+$/)
const region = Joi.string().valid(...props.regions)
const regions = [region, Joi.array().items(region).unique()]
const accountName = Joi.string().min(1).max(50)
const accountEmail = Joi.string().email()
// For account alias requirements, see https://docs.aws.amazon.com/IAM/latest/APIReference/API_CreateAccountAlias.html
const accountAlias = Joi.string()
.min(3)
.max(63)
.regex(/^[a-z0-9](([a-z0-9]|-(?!-))*[a-z0-9])?$/)
return {
region,
regions,
stackName,
iamRoleName,
iamRoleArn,
accountIds,
accountId,
parameterName,
tags,
stackOutputName,
stackCapability,
stackCapabilities,
tagName,
tagValue,
accountName,
accountEmail,
accountAlias,
}
}