Uecko_ERP/packages/rdx-ddd/src/value-objects/url-address.ts

33 lines
785 B
TypeScript
Raw Normal View History

2025-09-01 14:07:59 +00:00
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);
}
2025-09-04 10:02:24 +00:00
getProps(): string {
2025-09-01 14:07:59 +00:00
return this.props.value;
}
toPrimitive() {
2025-09-04 10:02:24 +00:00
return this.getProps();
2025-09-01 14:07:59 +00:00
}
}