Uecko_ERP/packages/rdx-ddd/src/value-objects/city.ts

44 lines
1000 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";
2025-09-16 11:29:45 +00:00
import { translateZodValidationError } from "../helpers";
2025-09-01 14:07:59 +00:00
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) {
2025-09-16 11:29:45 +00:00
return Result.fail(translateZodValidationError("City creation failed", valueIsValid.error));
2025-09-01 14:07:59 +00:00
}
return Result.ok(new City({ 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
}
2025-09-10 18:14:19 +00:00
toString() {
return String(this.props.value);
}
2025-09-01 14:07:59 +00:00
}