@wepublish/api
Version:
API core for we.publish.
40 lines • 1.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkSettingRestrictions = void 0;
const common_1 = require("@nestjs/common");
class InvalidSettingValueError extends common_1.UnprocessableEntityException {
constructor() {
super('Invalid Setting Data', 'INVALID_SETTING_DATA'); // todo what to do with this?
}
}
function checkSettingRestrictions(val, currentVal, restriction) {
var _a, _b;
if (!restriction) {
return;
}
if (typeof val !== typeof currentVal) {
throw new InvalidSettingValueError();
}
if (((_a = restriction.allowedValues) === null || _a === void 0 ? void 0 : _a.boolChoice) && typeof val !== 'boolean') {
throw new InvalidSettingValueError();
}
if (typeof val === 'number') {
if (restriction.maxValue && val > restriction.maxValue) {
throw new InvalidSettingValueError();
}
if (restriction.minValue && val < restriction.minValue) {
throw new InvalidSettingValueError();
}
}
if (typeof val === 'string') {
if (restriction.inputLength && val.length > restriction.inputLength) {
throw new InvalidSettingValueError();
}
if (((_b = restriction.allowedValues) === null || _b === void 0 ? void 0 : _b.stringChoice) &&
!restriction.allowedValues.stringChoice.includes(val)) {
throw new InvalidSettingValueError();
}
}
}
exports.checkSettingRestrictions = checkSettingRestrictions;
//# sourceMappingURL=settings-utils.js.map