1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/lib/services/feature-schema.js

112 lines
2.7 KiB
JavaScript
Raw Normal View History

'use strict';
2020-07-31 22:15:09 +02:00
const joi = require('joi');
const { nameType } = require('../routes/admin-api/util');
const nameSchema = joi
.object()
.keys({ name: nameType })
.options({ stripUnknown: true, allowUnknown: false, abortEarly: false });
const constraintSchema = joi.object().keys({
contextName: joi.string(),
operator: joi.string(),
values: joi
.array()
.items(
joi
.string()
.min(1)
.max(100),
)
.min(1)
.optional(),
});
const strategiesSchema = joi.object().keys({
name: nameType,
constraints: joi
.array()
.allow(null)
.items(constraintSchema),
parameters: joi.object(),
});
2018-01-30 15:57:12 +01:00
const variantsSchema = joi.object().keys({
name: nameType,
2019-01-24 11:26:07 +01:00
weight: joi
.number()
.min(0)
2020-02-13 17:53:09 +01:00
.max(1000)
2019-01-24 11:26:07 +01:00
.required(),
weightType: joi
.string()
.valid('variable', 'fix')
.default('variable'),
2019-01-24 11:26:07 +01:00
payload: joi
.object()
.keys({
type: joi.string().required(),
value: joi.string().required(),
})
.optional(),
overrides: joi.array().items(
joi
.object()
.keys({
contextName: joi.string().required(),
values: joi.array().items(joi.string()),
})
.optional(),
),
2018-01-30 15:57:12 +01:00
});
const featureSchema = joi
.object()
.keys({
name: nameType,
enabled: joi.boolean().default(false),
stale: joi.boolean().default(false),
type: joi.string().default('release'),
project: joi.string().default('default'),
description: joi
.string()
.allow('')
.allow(null)
.optional(),
strategies: joi
.array()
.required()
.min(1)
.items(strategiesSchema),
2018-01-30 15:57:12 +01:00
variants: joi
.array()
.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()
.items(variantsSchema),
})
.options({ allowUnknown: false, stripUnknown: true, abortEarly: false });
const querySchema = joi
.object()
.keys({
tag: joi
.array()
.allow(null)
.items(joi.string().pattern(/\w+:.+/, { name: 'tag' }))
.optional(),
project: joi
.array()
.allow(null)
.items(joi.string().alphanum())
.optional(),
namePrefix: joi
.string()
.allow(null)
.optional(),
})
.options({ allowUnknown: false, stripUnknown: true, abortEarly: false });
module.exports = { featureSchema, strategiesSchema, nameSchema, querySchema };