2021-09-15 20:28:10 +02:00
|
|
|
import joi from 'joi';
|
|
|
|
import { ALL, ApiTokenType } from '../types/models/api-token';
|
2021-09-24 13:55:00 +02:00
|
|
|
import { DEFAULT_ENV } from '../util/constants';
|
2021-09-15 20:28:10 +02:00
|
|
|
|
|
|
|
export const createApiToken = joi
|
|
|
|
.object()
|
|
|
|
.keys({
|
|
|
|
username: joi.string().required(),
|
|
|
|
type: joi
|
|
|
|
.string()
|
|
|
|
.lowercase()
|
|
|
|
.required()
|
|
|
|
.valid(ApiTokenType.ADMIN, ApiTokenType.CLIENT),
|
|
|
|
expiresAt: joi.date().optional(),
|
2022-04-06 08:11:41 +02:00
|
|
|
project: joi.when('projects', {
|
|
|
|
not: joi.required(),
|
|
|
|
then: joi.string().optional().default(ALL),
|
|
|
|
}),
|
|
|
|
projects: joi.array().min(0).optional(),
|
2021-09-24 13:55:00 +02:00
|
|
|
environment: joi.when('type', {
|
|
|
|
is: joi.string().valid(ApiTokenType.CLIENT),
|
|
|
|
then: joi.string().optional().default(DEFAULT_ENV),
|
|
|
|
otherwise: joi.string().optional().default(ALL),
|
|
|
|
}),
|
2021-09-15 20:28:10 +02:00
|
|
|
})
|
2022-04-06 08:11:41 +02:00
|
|
|
.nand('project', 'projects')
|
2021-09-15 20:28:10 +02:00
|
|
|
.options({ stripUnknown: true, allowUnknown: false, abortEarly: false });
|