From 99d2bbdb7346a25415e64b30c50d08c96ab4ea20 Mon Sep 17 00:00:00 2001 From: sighphyre Date: Thu, 17 Mar 2022 11:34:43 +0200 Subject: [PATCH] fix: patch constraint schema to always default values to empty array --- src/lib/schema/feature-schema.ts | 9 ++++++-- src/lib/services/feature-toggle-service.ts | 26 ++++++++++++++-------- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/lib/schema/feature-schema.ts b/src/lib/schema/feature-schema.ts index 93bd22f152..8b702afeee 100644 --- a/src/lib/schema/feature-schema.ts +++ b/src/lib/schema/feature-schema.ts @@ -10,8 +10,13 @@ export const nameSchema = joi export const constraintSchema = joi.object().keys({ contextName: joi.string(), operator: joi.string().valid(...ALL_OPERATORS), - values: joi.array().items(joi.string().min(1).max(100)).min(1).optional(), - value: joi.optional(), + values: joi + .array() + .items(joi.string().min(1).max(100)) + .min(0) + .default([]) + .optional(), + value: joi.optional().default(''), caseInsensitive: joi.boolean().optional(), inverted: joi.boolean().optional(), }); diff --git a/src/lib/services/feature-toggle-service.ts b/src/lib/services/feature-toggle-service.ts index 80f4c8d984..8aad0909c5 100644 --- a/src/lib/services/feature-toggle-service.ts +++ b/src/lib/services/feature-toggle-service.ts @@ -165,17 +165,19 @@ class FeatureToggleService { } } - async validateConstraints(constraints: IConstraint[]): Promise { + async validateConstraints( + constraints: IConstraint[], + ): Promise { const validations = constraints.map((constraint) => { return this.validateConstraint(constraint); }); - await Promise.all(validations); + return Promise.all(validations); } - async validateConstraint(constraint: IConstraint): Promise { + async validateConstraint(constraint: IConstraint): Promise { const { operator } = constraint; - await constraintSchema.validateAsync(constraint); + const scrubbedSchema = await constraintSchema.validateAsync(constraint); const contextDefinition = await this.contextFieldStore.get( constraint.contextName, ); @@ -218,6 +220,7 @@ class FeatureToggleService { ); } } + return scrubbedSchema; } async patchFeature( @@ -282,7 +285,9 @@ class FeatureToggleService { await this.validateFeatureContext(context); if (strategyConfig.constraints?.length > 0) { - await this.validateConstraints(strategyConfig.constraints); + strategyConfig.constraints = await this.validateConstraints( + strategyConfig.constraints, + ); } try { @@ -342,15 +347,18 @@ class FeatureToggleService { this.validateFeatureStrategyContext(existingStrategy, context); 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( id, updates, ); - if (updates.constraints?.length > 0) { - await this.validateConstraints(updates.constraints); - } - // Store event! const tags = await this.tagStore.getAllTagsForFeature(featureName); const data = this.featureStrategyToPublic(strategy);