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

46 lines
1.0 KiB
TypeScript
Raw Normal View History

2025-09-01 14:07:59 +00:00
import { Result } from "@repo/rdx-utils";
2025-09-24 17:30:35 +00:00
import { 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 ProvinceProps {
value: string;
}
export class Province extends ValueObject<ProvinceProps> {
private static readonly MAX_LENGTH = 255;
protected static validate(value: string) {
const schema = z
.string()
.trim()
.max(Province.MAX_LENGTH, {
message: `Province must be at most ${Province.MAX_LENGTH} characters long`,
});
return schema.safeParse(value);
}
static create(value: string) {
const valueIsValid = Province.validate(value);
if (!valueIsValid.success) {
2025-09-16 11:29:45 +00:00
return Result.fail(
translateZodValidationError("Province creation failed", valueIsValid.error)
);
2025-09-01 14:07:59 +00:00
}
return Result.ok(new Province({ 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
}