UNPKG

unleash-server

Version:

Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.

55 lines 2.35 kB
import { constraintSchema } from '../../schema/feature-schema.js'; import { DATE_OPERATORS, NUM_OPERATORS, REGEX, SEMVER_OPERATORS, STRING_OPERATORS, } from '../../util/index.js'; import { validateDate, validateLegalValues, validateNumber, validateSemver, validateString, validateRegex, } from '../../util/validators/constraint-types.js'; const oneOf = (values, match) => { return values.some((value) => value === match); }; export class ConstraintsReadModel { constructor(contextFieldStore) { this.contextFieldStore = contextFieldStore; } async validateConstraints(constraints) { const validations = constraints.map((constraint) => { return this.validateConstraint(constraint); }); return Promise.all(validations); } async validateConstraint(input) { const constraint = await constraintSchema.validateAsync(input); const { operator } = constraint; if (oneOf(NUM_OPERATORS, operator)) { await validateNumber(constraint.value); } if (oneOf(STRING_OPERATORS, operator)) { await validateString(constraint.values); } if (oneOf(SEMVER_OPERATORS, operator)) { // Semver library is not asynchronous, so we do not // need to await here. validateSemver(constraint.value); } if (oneOf(DATE_OPERATORS, operator)) { await validateDate(constraint.value); } if (operator === REGEX) { validateRegex(constraint.value, constraint.inverted); } if (await this.contextFieldStore.exists(constraint.contextName)) { const contextDefinition = await this.contextFieldStore.get(constraint.contextName); if (contextDefinition?.legalValues && contextDefinition.legalValues.length > 0) { const valuesToValidate = oneOf([ ...DATE_OPERATORS, ...SEMVER_OPERATORS, ...NUM_OPERATORS, REGEX, ], operator) ? constraint.value : constraint.values; validateLegalValues(contextDefinition.legalValues, valuesToValidate); } } return constraint; } } //# sourceMappingURL=constraints-read-model.js.map