mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-22 10:21:36 +03:00
581f0b34b4
closes https://github.com/TryGhost/Team/issues/2380 - improved offer validation for `amount` field to cover all type/amount cases - added validate-on-blur to the amount field to match our standard validation behaviour - added re-validation of the amount field when the type is changed and the amount gets reset - removed the internal parsing of a decimal trial days entry to an integer so the field value matches what is set internally and we let the user know that partial trial days are not supported Non-user-facing refactors: - renamed `_saveOfferProperty` to `_updateOfferProperty` to better reflect what it does - fixed missing indentation for conditional blocks in the offer template
82 lines
2.6 KiB
JavaScript
82 lines
2.6 KiB
JavaScript
import BaseValidator from './base';
|
|
import validator from 'validator';
|
|
|
|
export default BaseValidator.create({
|
|
properties: ['name', 'amount', 'displayTitle', 'displayDescription', 'code', 'durationInMonths'],
|
|
|
|
name(model) {
|
|
if (!model.name) {
|
|
model.errors.add('name', 'Please enter a name.');
|
|
this.invalidate();
|
|
}
|
|
if (!validator.isLength(model.name || '', 0, 40)) {
|
|
model.errors.add('name', 'Name cannot be longer than 40 characters.');
|
|
this.invalidate();
|
|
}
|
|
},
|
|
|
|
amount(model) {
|
|
if (!model.amount) {
|
|
if (model.type === 'trial') {
|
|
model.errors.add('amount', 'Free trial must be at least 1 day.');
|
|
} else {
|
|
model.errors.add('amount', 'Please enter the amount.');
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
},
|
|
|
|
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) {
|
|
model.errors.add('durationInMonths', 'Please enter the duration in months.');
|
|
this.invalidate();
|
|
}
|
|
},
|
|
|
|
code(model) {
|
|
if (!model.code) {
|
|
model.errors.add('code', 'Please enter an offer code.');
|
|
this.invalidate();
|
|
}
|
|
}
|
|
});
|