Presupuestador_web/shared/lib/contexts/common/domain/entities/Email.ts

55 lines
1.3 KiB
TypeScript
Raw Normal View History

2024-04-23 15:29:38 +00:00
import Joi from "joi";
import { UndefinedOr } from "../../../../utilities";
import { RuleValidator } from "../RuleValidator";
import { DomainError, handleDomainError } from "../errors";
import { Result } from "./Result";
import {
IStringValueObjectOptions,
StringValueObject,
} from "./StringValueObject";
export class Email extends StringValueObject {
protected static validate(
value: UndefinedOr<string>,
options: IStringValueObjectOptions
) {
const rule = Joi.string()
.allow(null)
.allow("")
.default("")
.email({
tlds: { allow: false },
})
.label(options.label ? options.label : "value");
return RuleValidator.validate<string>(rule, value);
}
public static create(
value: UndefinedOr<string>,
options: IStringValueObjectOptions = {}
) {
const _options = {
label: "email",
...options,
};
const validationResult = Email.validate(value, _options);
if (validationResult.isFailure) {
return Result.fail(
handleDomainError(
DomainError.INVALID_INPUT_DATA,
2024-04-24 10:48:49 +00:00
validationResult.error.message,
_options
2024-04-23 15:29:38 +00:00
)
);
}
return Result.ok(new Email(validationResult.object));
}
}
export class Email_ValidationError extends Joi.ValidationError {}