2021-12-09 21:50:06 +01:00
|
|
|
import joi from 'joi';
|
2023-02-15 09:13:32 +01:00
|
|
|
import { IMetricsBucket } from 'lib/types';
|
2021-12-09 21:50:06 +01:00
|
|
|
|
|
|
|
const countSchema = joi
|
|
|
|
.object()
|
|
|
|
.options({ stripUnknown: true })
|
|
|
|
.keys({
|
|
|
|
yes: joi.number().min(0).empty('').default(0),
|
|
|
|
no: joi.number().min(0).empty('').default(0),
|
|
|
|
variants: joi.object().pattern(joi.string(), joi.number().min(0)),
|
|
|
|
});
|
|
|
|
|
2023-02-15 09:13:32 +01:00
|
|
|
// validated type from client-metrics-schema.ts with default values
|
|
|
|
export type ValidatedClientMetrics = {
|
|
|
|
environment?: string;
|
|
|
|
appName: string;
|
|
|
|
instanceId: string;
|
|
|
|
bucket: IMetricsBucket;
|
|
|
|
};
|
|
|
|
|
2021-12-09 21:50:06 +01:00
|
|
|
export const clientMetricsSchema = joi
|
2023-02-15 09:13:32 +01:00
|
|
|
.object<ValidatedClientMetrics>()
|
2021-12-09 21:50:06 +01:00
|
|
|
.options({ stripUnknown: true })
|
|
|
|
.keys({
|
|
|
|
environment: joi.string().optional(),
|
|
|
|
appName: joi.string().required(),
|
2022-04-22 13:52:27 +02:00
|
|
|
instanceId: joi.string().empty(['', null]).default('default'),
|
2021-12-09 21:50:06 +01:00
|
|
|
bucket: joi
|
|
|
|
.object()
|
|
|
|
.required()
|
|
|
|
.keys({
|
|
|
|
start: joi.date().required(),
|
|
|
|
stop: joi.date().required(),
|
|
|
|
toggles: joi.object().pattern(/.*/, countSchema),
|
|
|
|
}),
|
|
|
|
});
|
|
|
|
|
2023-03-01 13:08:08 +01:00
|
|
|
export const clientMetricsEnvSchema = joi
|
|
|
|
.object()
|
|
|
|
.options({ stripUnknown: true })
|
|
|
|
.keys({
|
|
|
|
featureName: joi.string().required(),
|
|
|
|
environment: joi.string().required(),
|
|
|
|
appName: joi.string().required(),
|
|
|
|
yes: joi.number().default(0),
|
|
|
|
no: joi.number().default(0),
|
|
|
|
timestamp: joi.date(),
|
|
|
|
variants: joi.object().pattern(joi.string(), joi.number().min(0)),
|
|
|
|
});
|
|
|
|
export const clientMetricsEnvBulkSchema = joi
|
|
|
|
.array()
|
|
|
|
.items(clientMetricsEnvSchema)
|
|
|
|
.empty();
|
|
|
|
|
2021-12-09 21:50:06 +01:00
|
|
|
export const applicationSchema = joi
|
|
|
|
.object()
|
|
|
|
.options({ stripUnknown: false })
|
|
|
|
.keys({
|
|
|
|
appName: joi.string().required(),
|
|
|
|
sdkVersion: joi.string().optional(),
|
|
|
|
strategies: joi
|
|
|
|
.array()
|
|
|
|
.optional()
|
|
|
|
.items(joi.string(), joi.any().strip()),
|
|
|
|
description: joi.string().allow('').optional(),
|
|
|
|
url: joi.string().allow('').optional(),
|
|
|
|
color: joi.string().allow('').optional(),
|
|
|
|
icon: joi.string().allow('').optional(),
|
|
|
|
announced: joi.boolean().optional().default(false),
|
|
|
|
});
|
|
|
|
|
2023-03-01 13:08:08 +01:00
|
|
|
export const batchMetricsSchema = joi
|
|
|
|
.object()
|
|
|
|
.options({ stripUnknown: true })
|
|
|
|
.keys({
|
|
|
|
applications: joi.array().items(applicationSchema),
|
|
|
|
metrics: joi.array().items(clientMetricsEnvSchema),
|
|
|
|
});
|
|
|
|
|
2021-12-09 21:50:06 +01:00
|
|
|
export const clientRegisterSchema = joi
|
|
|
|
.object()
|
|
|
|
.options({ stripUnknown: true })
|
|
|
|
.keys({
|
|
|
|
appName: joi.string().required(),
|
2022-04-22 13:52:27 +02:00
|
|
|
instanceId: joi.string().empty(['', null]).default('default'),
|
2021-12-09 21:50:06 +01:00
|
|
|
sdkVersion: joi.string().optional(),
|
|
|
|
strategies: joi
|
|
|
|
.array()
|
|
|
|
.required()
|
|
|
|
.items(joi.string(), joi.any().strip()),
|
|
|
|
started: joi.date().required(),
|
|
|
|
interval: joi.number().required(),
|
|
|
|
environment: joi.string().optional(),
|
|
|
|
});
|