2021-07-07 10:46:50 +02:00
|
|
|
import joi from 'joi';
|
2022-03-04 17:29:42 +01:00
|
|
|
import { ALL_OPERATORS } from '../util/constants';
|
2021-08-13 10:36:19 +02:00
|
|
|
import { nameType } from '../routes/util';
|
2022-06-14 13:08:38 +02:00
|
|
|
import { validateJsonString } from '../util/validateJsonString';
|
2018-12-01 12:03:47 +01:00
|
|
|
|
2021-07-07 10:46:50 +02:00
|
|
|
export const nameSchema = joi
|
2021-01-18 12:32:19 +01:00
|
|
|
.object()
|
|
|
|
.keys({ name: nameType })
|
|
|
|
.options({ stripUnknown: true, allowUnknown: false, abortEarly: false });
|
2018-12-01 12:03:47 +01:00
|
|
|
|
2021-07-07 10:46:50 +02:00
|
|
|
export const constraintSchema = joi.object().keys({
|
2022-03-29 14:59:14 +02:00
|
|
|
contextName: joi.string().required(),
|
|
|
|
operator: joi
|
|
|
|
.string()
|
|
|
|
.valid(...ALL_OPERATORS)
|
|
|
|
.required(),
|
2022-03-17 12:32:04 +01:00
|
|
|
// Constraints must have a values array to support legacy SDKs.
|
|
|
|
values: joi.array().items(joi.string().min(1).max(100)).default([]),
|
2022-03-04 17:29:42 +01:00
|
|
|
value: joi.optional(),
|
|
|
|
caseInsensitive: joi.boolean().optional(),
|
|
|
|
inverted: joi.boolean().optional(),
|
2019-11-20 22:11:30 +01:00
|
|
|
});
|
|
|
|
|
2021-07-07 10:46:50 +02:00
|
|
|
export const strategiesSchema = joi.object().keys({
|
|
|
|
id: joi.string().optional(),
|
2018-12-12 10:09:38 +01:00
|
|
|
name: nameType,
|
2021-08-12 15:04:37 +02:00
|
|
|
constraints: joi.array().allow(null).items(constraintSchema),
|
2018-12-01 12:03:47 +01:00
|
|
|
parameters: joi.object(),
|
|
|
|
});
|
|
|
|
|
2022-11-21 10:37:16 +01:00
|
|
|
export const variantValueSchema = joi
|
2022-06-14 13:08:38 +02:00
|
|
|
.string()
|
|
|
|
.required()
|
|
|
|
// perform additional validation
|
|
|
|
// when provided 'type' is 'json'
|
|
|
|
.when('type', {
|
|
|
|
is: 'json',
|
|
|
|
then: joi.custom((val, helper) => {
|
|
|
|
const isValidJsonString = validateJsonString(val);
|
|
|
|
if (isValidJsonString === false) {
|
|
|
|
return helper.error('invalidJsonString');
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
.messages({
|
|
|
|
invalidJsonString:
|
|
|
|
"'value' must be a valid json string when 'type' is json",
|
|
|
|
});
|
|
|
|
|
2021-07-07 10:46:50 +02:00
|
|
|
export const variantsSchema = joi.object().keys({
|
2018-01-30 15:57:12 +01:00
|
|
|
name: nameType,
|
2023-01-05 12:39:18 +01:00
|
|
|
weight: joi
|
|
|
|
.number()
|
|
|
|
.integer()
|
|
|
|
.message('Weight only supports 1 decimal')
|
|
|
|
.min(0)
|
|
|
|
.max(1000)
|
|
|
|
.required(),
|
2021-08-12 15:04:37 +02:00
|
|
|
weightType: joi.string().valid('variable', 'fix').default('variable'),
|
2019-01-24 11:26:07 +01:00
|
|
|
payload: joi
|
|
|
|
.object()
|
|
|
|
.keys({
|
|
|
|
type: joi.string().required(),
|
2022-06-14 13:08:38 +02:00
|
|
|
value: variantValueSchema,
|
2019-01-24 11:26:07 +01:00
|
|
|
})
|
|
|
|
.optional(),
|
2021-02-11 17:59:16 +01:00
|
|
|
stickiness: joi.string().default('default'),
|
2019-01-29 09:21:54 +01:00
|
|
|
overrides: joi.array().items(
|
|
|
|
joi
|
|
|
|
.object()
|
|
|
|
.keys({
|
2019-02-04 14:04:58 +01:00
|
|
|
contextName: joi.string().required(),
|
2019-01-29 09:21:54 +01:00
|
|
|
values: joi.array().items(joi.string()),
|
|
|
|
})
|
2020-04-14 22:29:11 +02:00
|
|
|
.optional(),
|
2019-01-29 09:21:54 +01:00
|
|
|
),
|
2018-01-30 15:57:12 +01:00
|
|
|
});
|
|
|
|
|
2021-11-24 14:22:28 +01:00
|
|
|
export const variantsArraySchema = joi
|
|
|
|
.array()
|
|
|
|
.min(0)
|
|
|
|
.items(variantsSchema)
|
|
|
|
.unique((a, b) => a.name === b.name);
|
2021-11-24 13:08:04 +01:00
|
|
|
|
2021-07-07 10:46:50 +02:00
|
|
|
export const featureMetadataSchema = joi
|
|
|
|
.object()
|
|
|
|
.keys({
|
|
|
|
name: nameType,
|
|
|
|
stale: joi.boolean().default(false),
|
|
|
|
archived: joi.boolean().default(false),
|
|
|
|
type: joi.string().default('release'),
|
2021-08-12 15:04:37 +02:00
|
|
|
description: joi.string().allow('').allow(null).optional(),
|
2022-02-03 11:06:51 +01:00
|
|
|
impressionData: joi
|
|
|
|
.boolean()
|
|
|
|
.allow(true)
|
|
|
|
.allow(false)
|
|
|
|
.default(false)
|
|
|
|
.optional(),
|
2021-08-12 15:04:37 +02:00
|
|
|
createdAt: joi.date().optional().allow(null),
|
2022-06-28 08:54:09 +02:00
|
|
|
variants: joi
|
|
|
|
.array()
|
|
|
|
.allow(null)
|
|
|
|
.unique((a, b) => a.name === b.name)
|
|
|
|
.optional()
|
|
|
|
.items(variantsSchema),
|
2021-07-07 10:46:50 +02:00
|
|
|
})
|
|
|
|
.options({ allowUnknown: false, stripUnknown: true, abortEarly: false });
|
|
|
|
|
|
|
|
export const featureSchema = joi
|
2018-12-01 12:03:47 +01:00
|
|
|
.object()
|
|
|
|
.keys({
|
2018-12-12 10:09:38 +01:00
|
|
|
name: nameType,
|
2018-12-01 12:03:47 +01:00
|
|
|
enabled: joi.boolean().default(false),
|
2020-08-07 10:46:35 +02:00
|
|
|
stale: joi.boolean().default(false),
|
2021-07-07 10:46:50 +02:00
|
|
|
archived: joi.boolean().default(false),
|
2020-08-06 11:18:52 +02:00
|
|
|
type: joi.string().default('release'),
|
2020-09-28 21:54:44 +02:00
|
|
|
project: joi.string().default('default'),
|
2021-08-12 15:04:37 +02:00
|
|
|
description: joi.string().allow('').allow(null).optional(),
|
2022-02-03 11:06:51 +01:00
|
|
|
impressionData: joi
|
|
|
|
.boolean()
|
|
|
|
.allow(true)
|
|
|
|
.allow(false)
|
|
|
|
.default(false)
|
|
|
|
.optional(),
|
2018-12-01 12:03:47 +01:00
|
|
|
strategies: joi
|
|
|
|
.array()
|
2021-07-07 10:46:50 +02:00
|
|
|
.min(0)
|
|
|
|
.allow(null)
|
|
|
|
.optional()
|
2018-12-01 12:03:47 +01:00
|
|
|
.items(strategiesSchema),
|
2018-01-30 15:57:12 +01:00
|
|
|
variants: joi
|
|
|
|
.array()
|
2019-03-14 17:56:02 +01:00
|
|
|
.allow(null)
|
2019-01-24 11:26:07 +01:00
|
|
|
.unique((a, b) => a.name === b.name)
|
2018-01-30 15:57:12 +01:00
|
|
|
.optional()
|
2019-03-14 17:56:02 +01:00
|
|
|
.items(variantsSchema),
|
2018-12-01 12:03:47 +01:00
|
|
|
})
|
2021-01-18 12:32:19 +01:00
|
|
|
.options({ allowUnknown: false, stripUnknown: true, abortEarly: false });
|
2018-12-01 12:03:47 +01:00
|
|
|
|
2021-07-07 10:46:50 +02:00
|
|
|
export const querySchema = joi
|
2021-01-22 13:39:42 +01:00
|
|
|
.object()
|
|
|
|
.keys({
|
|
|
|
tag: joi
|
|
|
|
.array()
|
|
|
|
.allow(null)
|
|
|
|
.items(joi.string().pattern(/\w+:.+/, { name: 'tag' }))
|
|
|
|
.optional(),
|
2021-08-12 15:04:37 +02:00
|
|
|
project: joi.array().allow(null).items(nameType).optional(),
|
|
|
|
namePrefix: joi.string().allow(null).optional(),
|
|
|
|
environment: joi.string().allow(null).optional(),
|
2022-06-02 14:07:46 +02:00
|
|
|
inlineSegmentConstraints: joi.boolean().optional(),
|
2021-01-22 13:39:42 +01:00
|
|
|
})
|
|
|
|
.options({ allowUnknown: false, stripUnknown: true, abortEarly: false });
|
|
|
|
|
2021-07-07 10:46:50 +02:00
|
|
|
export const featureTagSchema = joi.object().keys({
|
2021-03-12 11:08:10 +01:00
|
|
|
featureName: nameType,
|
2021-07-07 10:46:50 +02:00
|
|
|
tagType: nameType.optional(),
|
2021-03-12 11:08:10 +01:00
|
|
|
tagValue: joi.string(),
|
2021-07-07 10:46:50 +02:00
|
|
|
type: nameType.optional(),
|
|
|
|
value: joi.string().optional(),
|
2021-03-12 11:08:10 +01:00
|
|
|
});
|