1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-02-14 00:19:16 +01:00

fix: patch constraint schema to always default values to empty array

This commit is contained in:
sighphyre 2022-03-17 11:34:43 +02:00
parent 957ec1c16d
commit 99d2bbdb73
2 changed files with 24 additions and 11 deletions

View File

@ -10,8 +10,13 @@ export const nameSchema = joi
export const constraintSchema = joi.object().keys({ export const constraintSchema = joi.object().keys({
contextName: joi.string(), contextName: joi.string(),
operator: joi.string().valid(...ALL_OPERATORS), operator: joi.string().valid(...ALL_OPERATORS),
values: joi.array().items(joi.string().min(1).max(100)).min(1).optional(), values: joi
value: joi.optional(), .array()
.items(joi.string().min(1).max(100))
.min(0)
.default([])
.optional(),
value: joi.optional().default(''),
caseInsensitive: joi.boolean().optional(), caseInsensitive: joi.boolean().optional(),
inverted: joi.boolean().optional(), inverted: joi.boolean().optional(),
}); });

View File

@ -165,17 +165,19 @@ class FeatureToggleService {
} }
} }
async validateConstraints(constraints: IConstraint[]): Promise<void> { async validateConstraints(
constraints: IConstraint[],
): Promise<IConstraint[]> {
const validations = constraints.map((constraint) => { const validations = constraints.map((constraint) => {
return this.validateConstraint(constraint); return this.validateConstraint(constraint);
}); });
await Promise.all(validations); return Promise.all(validations);
} }
async validateConstraint(constraint: IConstraint): Promise<void> { async validateConstraint(constraint: IConstraint): Promise<IConstraint> {
const { operator } = constraint; const { operator } = constraint;
await constraintSchema.validateAsync(constraint); const scrubbedSchema = await constraintSchema.validateAsync(constraint);
const contextDefinition = await this.contextFieldStore.get( const contextDefinition = await this.contextFieldStore.get(
constraint.contextName, constraint.contextName,
); );
@ -218,6 +220,7 @@ class FeatureToggleService {
); );
} }
} }
return scrubbedSchema;
} }
async patchFeature( async patchFeature(
@ -282,7 +285,9 @@ class FeatureToggleService {
await this.validateFeatureContext(context); await this.validateFeatureContext(context);
if (strategyConfig.constraints?.length > 0) { if (strategyConfig.constraints?.length > 0) {
await this.validateConstraints(strategyConfig.constraints); strategyConfig.constraints = await this.validateConstraints(
strategyConfig.constraints,
);
} }
try { try {
@ -342,15 +347,18 @@ class FeatureToggleService {
this.validateFeatureStrategyContext(existingStrategy, context); this.validateFeatureStrategyContext(existingStrategy, context);
if (existingStrategy.id === id) { if (existingStrategy.id === id) {
if (updates.constraints?.length > 0) {
const scrubbedConstraints = await this.validateConstraints(
updates.constraints,
);
updates.constraints = scrubbedConstraints;
}
const strategy = await this.featureStrategiesStore.updateStrategy( const strategy = await this.featureStrategiesStore.updateStrategy(
id, id,
updates, updates,
); );
if (updates.constraints?.length > 0) {
await this.validateConstraints(updates.constraints);
}
// Store event! // Store event!
const tags = await this.tagStore.getAllTagsForFeature(featureName); const tags = await this.tagStore.getAllTagsForFeature(featureName);
const data = this.featureStrategyToPublic(strategy); const data = this.featureStrategyToPublic(strategy);