33 lines
785 B
TypeScript
33 lines
785 B
TypeScript
import { Result } from "@repo/rdx-utils";
|
|
import * as z from "zod/v4";
|
|
import { ValueObject } from "./value-object";
|
|
|
|
interface URLAddressProps {
|
|
value: string;
|
|
}
|
|
|
|
export class URLAddress extends ValueObject<URLAddressProps> {
|
|
static create(value: string): Result<URLAddress, Error> {
|
|
const valueIsValid = URLAddress.validate(value);
|
|
|
|
if (!valueIsValid.success) {
|
|
return Result.fail(new Error(valueIsValid.error.issues[0].message));
|
|
}
|
|
|
|
return Result.ok(new URLAddress({ value: valueIsValid.data }));
|
|
}
|
|
|
|
private static validate(value: string) {
|
|
const schema = z.url({ message: "Invalid URL format" });
|
|
return schema.safeParse(value);
|
|
}
|
|
|
|
getProps(): string {
|
|
return this.props.value;
|
|
}
|
|
|
|
toPrimitive() {
|
|
return this.getProps();
|
|
}
|
|
}
|