-
Notifications
You must be signed in to change notification settings - Fork 4
/
targets-model.ts
83 lines (76 loc) · 2.52 KB
/
targets-model.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
import Joi, { AnySchema } from "joi"
import { CommandContext } from "../context/command-context.js"
import {
createSchemaRegistry,
defaultSchema,
SchemaName,
SchemaRegistry,
} from "../takomo-stacks-model/schemas.js"
import { TakomoError } from "../utils/errors.js"
import { TkmLogger } from "../utils/logging.js"
export type DeploymentGroupName = string
export type DeploymentTargetName = string
export type DeploymentTargetNamePattern = string
export type DeploymentGroupPath = string
export type DeploymentStatus = "active" | "disabled"
export type Label = string
export interface DeploymentTargetsSchemaRegistry extends SchemaRegistry {
readonly initDeploymentTargetsSchema: (
ctx: CommandContext,
deploymentGroupPath: DeploymentGroupPath,
name: SchemaName,
props: Record<string, unknown>,
) => Promise<AnySchema>
}
export const createDeploymentTargetsSchemaRegistry = (
logger: TkmLogger,
): DeploymentTargetsSchemaRegistry => {
const schemaRegistry = createSchemaRegistry(logger)
return {
...schemaRegistry,
initDeploymentTargetsSchema: async (
ctx: CommandContext,
deploymentGroupPath: DeploymentGroupPath,
name: SchemaName,
props: Record<string, unknown>,
): Promise<AnySchema> => {
logger.debug(
`Initialize schema '${name}' for deployment group '${deploymentGroupPath}'`,
)
const provider = schemaRegistry.getProvider(name)
if (!provider) {
throw new Error(`Provider not found for schema '${name}'`)
}
if (provider.schema) {
const schema = provider.schema({
ctx,
joi: Joi.defaults((schema) => schema),
base: defaultSchema(name),
})
if (!Joi.isSchema(schema)) {
throw new TakomoError(
`Error initializing schema '${name}' for deployment group '${deploymentGroupPath}':\n\n` +
` - value returned from schema function is not a Joi schema object`,
)
}
const { error } = schema.validate(props, {
abortEarly: false,
convert: false,
})
if (error) {
const details = error.details
.map((d) => ` - ${d.message}`)
.join("\n")
throw new TakomoError(
`${error.details.length} validation error(s) in schema '${name}' configuration for deployment group '${deploymentGroupPath}':\n\n${details}`,
)
}
}
return provider.init({
joi: Joi.defaults((schema) => schema),
ctx,
props,
})
},
}
}