Ghost/ghost/offers/lib/domain/models/OfferCadence.js
Fabien O'Carroll 504fb1bfa1 Used Value Objects to validate outside of Offer factory
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.
2021-10-07 16:46:08 +02:00

25 lines
757 B
JavaScript

const ValueObject = require('../../shared/ValueObject');
const InvalidOfferCadence = require('../../errors').InvalidOfferCadence;
/**
* @extends ValueObject<'month'|'year'>
*/
class OfferCadence extends ValueObject {
/** @param {unknown} cadence */
static create(cadence) {
if (!cadence || typeof cadence !== 'string') {
throw new InvalidOfferCadence({
message: 'Offer `cadence` must be a string.'
});
}
if (cadence !== 'month' && cadence !== 'year') {
throw new InvalidOfferCadence({
message: 'Offer `cadence` must be one of "month" or "year".'
});
}
return new OfferCadence(cadence);
}
}
module.exports = OfferCadence;