Uecko_ERP/packages/rdx-ddd/src/value-objects/city.ts
2025-09-16 13:29:45 +02:00

44 lines
1000 B
TypeScript

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