Uecko_ERP/packages/rdx-ddd/src/value-objects/url-address.ts
2025-09-04 12:02:24 +02:00

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();
}
}