1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-08-04 13:48:56 +02:00

feat: add validation fields for constraint

This commit is contained in:
Fredrik Oseberg 2022-02-24 15:29:45 +01:00
parent 6c8a228d01
commit c3f1203a1f
5 changed files with 54 additions and 9 deletions

View File

@ -1,5 +1,9 @@
import { constraintNumberTypeSchema } from './constraint-value-types';
import {
constraintNumberTypeSchema,
constraintStringTypeSchema,
} from './constraint-value-types';
/* Number type */
test('should require number', async () => {
try {
await constraintNumberTypeSchema.validateAsync('test');
@ -23,3 +27,20 @@ test('should allow numbers', async () => {
test('should allow negative numbers', async () => {
await constraintNumberTypeSchema.validateAsync(-5);
});
/* String types */
test('should require a list of strings', async () => {
try {
await constraintStringTypeSchema.validateAsync(['test', 1]);
} catch (error) {
expect(error.details[0].message).toEqual('"[1]" must be a string');
}
});
test('should succeed with a list of strings', async () => {
await constraintStringTypeSchema.validateAsync([
'test',
'another-test',
'supervalue',
]);
});

View File

@ -1,3 +1,5 @@
import joi from 'joi';
export const constraintNumberTypeSchema = joi.number();
export const constraintStringTypeSchema = joi.array().items(joi.string());

View File

@ -10,6 +10,9 @@ export const constraintSchema = joi.object().keys({
contextName: joi.string(),
operator: joi.string(),
values: joi.array().items(joi.string().min(1).max(100)).min(1).optional(),
value: joi.optional(),
caseInsensitive: joi.boolean().optional(),
inverted: joi.boolean().optional(),
});
export const strategiesSchema = joi.object().keys({

View File

@ -6,6 +6,7 @@ import NameExistsError from '../error/name-exists-error';
import InvalidOperationError from '../error/invalid-operation-error';
import { FOREIGN_KEY_VIOLATION } from '../error/db-error';
import {
constraintSchema,
featureMetadataSchema,
nameSchema,
variantsArraySchema,
@ -60,6 +61,7 @@ import {
} from '../util/constants';
import { applyPatch, deepClone, Operation } from 'fast-json-patch';
import { OperationDeniedError } from '../error/operation-denied-error';
import { validateNumber } from 'lib/util/validators/constraint-types';
interface IFeatureContext {
featureName: string;
@ -151,23 +153,33 @@ class FeatureToggleService {
}
}
validateConstraint(constraint: IConstraint): void {
async validateConstraint(constraint: IConstraint): Promise<void> {
const { operator } = constraint;
await constraintSchema.validateAsync(constraint);
if (oneOf(NUM_OPERATORS, operator)) {
// Validate number value
await validateNumber(constraint.value);
// 1. Retrieve context defintion based on constraint contextName
// 2. Check if value is a valid number / value type
// 3. Check the value against predefined legalValues if specified on the
// context definition
}
if (oneOf(STRING_OPERATORS, operator)) {
// validate string values array
await validateString(constraint.values);
}
if (oneOf(SEMVER_OPERATORS, operator)) {
// validate semver
}
// if (oneOf(SEMVER_OPERATORS, operator)) {
// // validate semver
// validateSemver(constraint.value)
// }
if (oneOf(DATE_OPERATORS, operator)) {
// validate dates
}
// if (oneOf(DATE_OPERATORS, operator)) {
// // validate dates
// validateDate(constraint.value);
// }
}
async patchFeature(

View File

@ -1,5 +1,12 @@
import { constraintNumberTypeSchema } from 'lib/schema/constraint-value-types';
import {
constraintNumberTypeSchema,
constraintStringTypeSchema,
} from 'lib/schema/constraint-value-types';
export const validateNumber = async (value: unknown): Promise<void> => {
await constraintNumberTypeSchema.validateAsync(value);
};
export const validateString = async (value: unknown): Promise<void> => {
await constraintStringTypeSchema.validateAsync(value);
};