mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-04 12:44:57 +03:00
504fb1bfa1
no-issue This adds the concept of "Value Objects" to an Offers properties, allowing us to move validation out and ensure that an Offer will only ever have valid properties, without having to duplicate checks - or leave them to the persistent layer. This means we can fail early, as well as write unit tests for all of our validation.
29 lines
777 B
JavaScript
29 lines
777 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;
|