17 lines
528 B
TypeScript
17 lines
528 B
TypeScript
|
|
import { ValueObject } from "@repo/rdx-ddd";
|
||
|
|
import { Result } from "@repo/rdx-utils";
|
||
|
|
|
||
|
|
export class PhoneNumber extends ValueObject<string> {
|
||
|
|
static create(value: string): Result<PhoneNumber, Error> {
|
||
|
|
const normalized = value.trim();
|
||
|
|
if (
|
||
|
|
normalized.length > 50 ||
|
||
|
|
!/^\+?[0-9 ().-]{3,50}(?: ?(?:x|ext\.?) ?[0-9]{1,8})?$/.test(normalized) ||
|
||
|
|
!/[0-9]/.test(normalized)
|
||
|
|
) {
|
||
|
|
return Result.fail(new Error("Teléfono inválido."));
|
||
|
|
}
|
||
|
|
return Result.ok(new PhoneNumber(normalized));
|
||
|
|
}
|
||
|
|
}
|