2021-10-06 17:29:15 +03:00
|
|
|
import BaseValidator from './base';
|
|
|
|
import validator from 'validator';
|
|
|
|
|
|
|
|
export default BaseValidator.create({
|
2021-10-19 10:35:18 +03:00
|
|
|
properties: ['name', 'amount', 'displayTitle', 'displayDescription', 'code', 'durationInMonths'],
|
2021-10-06 17:29:15 +03:00
|
|
|
|
|
|
|
name(model) {
|
|
|
|
if (!model.name) {
|
2021-10-22 13:13:15 +03:00
|
|
|
model.errors.add('name', 'Please enter a name.');
|
2021-10-06 17:29:15 +03:00
|
|
|
this.invalidate();
|
|
|
|
}
|
2021-11-24 15:48:22 +03:00
|
|
|
if (!validator.isLength(model.name || '', 0, 40)) {
|
|
|
|
model.errors.add('name', 'Name cannot be longer than 40 characters.');
|
2021-10-06 17:29:15 +03:00
|
|
|
this.invalidate();
|
|
|
|
}
|
2021-10-19 10:35:18 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
amount(model) {
|
|
|
|
if (!model.amount) {
|
2022-08-10 13:17:10 +03:00
|
|
|
if (model.type === 'trial') {
|
2022-08-17 22:27:02 +03:00
|
|
|
model.errors.add('amount', 'Free trial must be at least 1 day.');
|
2022-08-10 13:17:10 +03:00
|
|
|
} else {
|
|
|
|
model.errors.add('amount', 'Please enter the amount.');
|
|
|
|
}
|
2023-01-03 12:23:11 +03:00
|
|
|
|
|
|
|
return this.invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (model.type === 'trial') {
|
|
|
|
if (model.amount < 1) {
|
|
|
|
model.errors.add('amount', 'Free trial must be at least 1 day.');
|
|
|
|
return this.invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!model.amount.toString().match(/^\d+$/)) {
|
|
|
|
model.errors.add('amount', 'Trial days must be a whole number.');
|
|
|
|
return this.invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (model.type === 'percent') {
|
|
|
|
if (model.amount < 0 || model.amount > 100) {
|
|
|
|
model.errors.add('amount', 'Amount must be between 0 and 100%.');
|
|
|
|
return this.invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!model.amount.toString().match(/^\d+$/)) {
|
|
|
|
model.errors.add('amount', 'Amount must be a whole number.');
|
|
|
|
return this.invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (model.type === 'fixed') {
|
|
|
|
if (model.amount < 0) {
|
|
|
|
model.errors.add('amount', 'Amount must be greater than 0.');
|
|
|
|
return this.invalidate();
|
|
|
|
}
|
2021-10-19 10:35:18 +03:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
displayDescription(model) {
|
|
|
|
if (!validator.isLength(model.displayDescription || '', 0, 191)) {
|
|
|
|
model.errors.add('displayDescription', 'Display description cannot be longer than 191 characters.');
|
|
|
|
this.invalidate();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
durationInMonths(model) {
|
|
|
|
if (model.duration === 'repeating' && !model.durationInMonths) {
|
2021-10-22 13:13:15 +03:00
|
|
|
model.errors.add('durationInMonths', 'Please enter the duration in months.');
|
2021-10-19 10:35:18 +03:00
|
|
|
this.invalidate();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
code(model) {
|
|
|
|
if (!model.code) {
|
2021-10-22 13:13:15 +03:00
|
|
|
model.errors.add('code', 'Please enter an offer code.');
|
2021-10-19 10:35:18 +03:00
|
|
|
this.invalidate();
|
|
|
|
}
|
2021-10-06 17:29:15 +03:00
|
|
|
}
|
|
|
|
});
|