Uecko_ERP/packages/rdx-ddd/src/value-objects/tax-code.ts

47 lines
1.3 KiB
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 TaxCodeProps {
value: string;
}
export class TaxCode extends ValueObject<TaxCodeProps> {
protected static readonly MIN_LENGTH = 1;
protected static readonly MAX_LENGTH = 10;
protected static validate(value: string) {
const schema = z
.string()
.trim()
.regex(/^[a-z0-9]+([_-][a-z0-9]+)*$/, {
message: "TaxCode must contain only lowercase letters, numbers, and underscores",
})
.min(TaxCode.MIN_LENGTH, {
message: `TaxCode must be at least ${TaxCode.MIN_LENGTH} characters long`,
})
.max(TaxCode.MAX_LENGTH, {
message: `TaxCode must be at most ${TaxCode.MAX_LENGTH} characters long`,
});
return schema.safeParse(value);
}
static create(value: string) {
const valueIsValid = TaxCode.validate(value);
if (!valueIsValid.success) {
return Result.fail(new Error(valueIsValid.error.issues[0].message));
}
// biome-ignore lint/style/noNonNullAssertion: <explanation>
return Result.ok(new TaxCode({ value: valueIsValid.data! }));
}
getValue(): string {
return this.props.value;
}
toPrimitive(): string {
return this.getValue();
}
}