Uecko_ERP/packages/rdx-ddd/src/value-objects/street.ts
2025-09-10 20:14:19 +02:00

43 lines
934 B
TypeScript

import { Result } from "@repo/rdx-utils";
import * as z from "zod/v4";
import { ValueObject } from "./value-object";
interface StreetProps {
value: string;
}
export class Street extends ValueObject<StreetProps> {
private static readonly MAX_LENGTH = 255;
protected static validate(value: string) {
const schema = z
.string()
.trim()
.max(Street.MAX_LENGTH, {
message: `Street must be at most ${Street.MAX_LENGTH} characters long`,
});
return schema.safeParse(value);
}
static create(value: string) {
const valueIsValid = Street.validate(value);
if (!valueIsValid.success) {
return Result.fail(new Error(valueIsValid.error.issues[0].message));
}
return Result.ok(new Street({ value }));
}
getProps(): string {
return this.props.value;
}
toPrimitive() {
return this.getProps();
}
toString() {
return String(this.props.value);
}
}