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

39 lines
893 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 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) {
return Result.fail(new Error(valueIsValid.error.issues[0].message));
}
return Result.ok(new Province({ value }));
}
getValue(): string {
return this.props.value;
}
toPrimitive() {
return this.getValue();
}
}