mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-20 09:22:49 +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.
25 lines
683 B
JavaScript
25 lines
683 B
JavaScript
const ValueObject = require('../../shared/ValueObject');
|
|
const InvalidOfferName = require('../../errors').InvalidOfferName;
|
|
|
|
/** @extends ValueObject<string> */
|
|
class OfferName extends ValueObject {
|
|
/** @param {unknown} name */
|
|
static create(name) {
|
|
if (!name || typeof name !== 'string') {
|
|
throw new InvalidOfferName({
|
|
message: 'Offer `name` must be a string.'
|
|
});
|
|
}
|
|
|
|
if (name.length > 191) {
|
|
throw new InvalidOfferName({
|
|
message: 'Offer `name` can be a maximum of 191 characters.'
|
|
});
|
|
}
|
|
|
|
return new OfferName(name);
|
|
}
|
|
}
|
|
|
|
module.exports = OfferName;
|