2024-04-23 15:29:38 +00:00
|
|
|
import Joi from "joi";
|
|
|
|
|
import { UndefinedOr } from "../../../../utilities";
|
2024-07-17 18:10:07 +00:00
|
|
|
import { DomainError, handleDomainError } from "../errors";
|
2024-04-23 15:29:38 +00:00
|
|
|
import { RuleValidator } from "../RuleValidator";
|
|
|
|
|
import { Result } from "./Result";
|
2024-07-17 18:10:07 +00:00
|
|
|
import { IStringValueObjectOptions, StringValueObject } from "./StringValueObject";
|
2024-04-23 15:29:38 +00:00
|
|
|
|
|
|
|
|
export class Description extends StringValueObject {
|
2024-07-17 18:10:07 +00:00
|
|
|
protected static validate(value: UndefinedOr<string>, options: IStringValueObjectOptions) {
|
2024-04-23 15:29:38 +00:00
|
|
|
const ruleIsEmpty = Joi.string()
|
|
|
|
|
.optional()
|
2024-07-17 18:10:07 +00:00
|
|
|
.allow(null)
|
|
|
|
|
.allow("")
|
2024-04-23 15:29:38 +00:00
|
|
|
.default("")
|
|
|
|
|
.label(String(options.label));
|
|
|
|
|
|
|
|
|
|
return RuleValidator.validate<string>(ruleIsEmpty, value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-17 18:10:07 +00:00
|
|
|
public static create(value: UndefinedOr<string>, options: IStringValueObjectOptions = {}) {
|
2024-04-23 15:29:38 +00:00
|
|
|
const _options = {
|
|
|
|
|
label: "description",
|
|
|
|
|
...options,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const validationResult = Description.validate(value, _options);
|
|
|
|
|
|
|
|
|
|
if (validationResult.isFailure) {
|
2024-07-17 18:10:07 +00:00
|
|
|
return Result.fail(
|
|
|
|
|
handleDomainError(DomainError.INVALID_INPUT_DATA, validationResult.error.message, _options)
|
|
|
|
|
);
|
2024-04-23 15:29:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result.ok(new Description(validationResult.object));
|
|
|
|
|
}
|
|
|
|
|
}
|