Ghost/ghost/offers/lib/domain/models/OfferCode.js
Fabien O'Carroll 35f150bcf4 Moved errors to domain/errors
no-issue

More cleanup to pull files into their appropriate responsibility
2021-10-08 12:31:11 +02:00

29 lines
770 B
JavaScript

const {slugify} = require('@tryghost/string');
const ValueObject = require('./shared/ValueObject');
const InvalidOfferCode = require('../errors').InvalidOfferCode;
/** @extends ValueObject<string> */
class OfferCode extends ValueObject {
/** @param {unknown} code */
static create(code) {
if (!code || typeof code !== 'string') {
throw new InvalidOfferCode({
message: 'Offer `code` must be a string.'
});
}
const slugged = slugify(code);
if (slugged.length > 191) {
throw new InvalidOfferCode({
message: 'Offer `code` can be a maximum of 191 characters.'
});
}
return new OfferCode(slugged);
}
}
module.exports = OfferCode;