unleash-server
Version:
Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.
37 lines • 1.58 kB
JavaScript
import { constraintDateTypeSchema, constraintNumberTypeSchema, constraintStringTypeSchema, } from '../../schema/constraint-value-types.js';
import BadDataError from '../../error/bad-data-error.js';
import { parseStrictSemVer } from '../semver.js';
export const validateNumber = async (value) => {
await constraintNumberTypeSchema.validateAsync(value);
};
export const validateString = async (value) => {
await constraintStringTypeSchema.validateAsync(value);
};
export const validateSemver = (value) => {
if (typeof value !== 'string') {
throw new BadDataError(`the provided value is not a string.`);
}
if (!parseStrictSemVer(value)) {
throw new BadDataError(`the provided value is not a valid semver format. The value provided was: ${value}`);
}
};
export const validateDate = async (value) => {
await constraintDateTypeSchema.validateAsync(value);
};
export const validateLegalValues = (legalValues, match) => {
const legalStrings = legalValues.map((legalValue) => {
return legalValue.value;
});
if (Array.isArray(match)) {
// Compare arrays to arrays
const valid = match.every((value) => legalStrings.includes(value));
if (!valid)
throw new BadDataError(`input values are not specified as a legal value on this context field`);
}
else {
const valid = legalStrings.includes(match);
if (!valid)
throw new BadDataError(`${match} is not specified as a legal value on this context field`);
}
};
//# sourceMappingURL=constraint-types.js.map