FactuGES_ERP/modules/invoices/src/api/domain/value-objects/invoice-number.ts

43 lines
984 B
TypeScript
Raw Normal View History

2025-05-02 21:43:51 +00:00
import { ValueObject } from "@/core/common/domain";
2025-05-09 10:45:32 +00:00
import { Result } from "@repo/rdx-utils";
2025-04-01 14:26:15 +00:00
import { z } from "zod";
interface IInvoiceNumberProps {
value: string;
}
export class InvoiceNumber extends ValueObject<IInvoiceNumberProps> {
private static readonly MAX_LENGTH = 255;
protected static validate(value: string) {
const schema = z
.string()
.trim()
.max(InvoiceNumber.MAX_LENGTH, {
message: `Name must be at most ${InvoiceNumber.MAX_LENGTH} characters long`,
});
return schema.safeParse(value);
}
static create(value: string) {
const valueIsValid = InvoiceNumber.validate(value);
if (!valueIsValid.success) {
return Result.fail(new Error(valueIsValid.error.errors[0].message));
}
return Result.ok(new InvoiceNumber({ value }));
}
getValue(): string {
return this.props.value;
}
toString(): string {
return this.getValue();
}
2025-04-01 15:32:53 +00:00
toPrimitive() {
return this.getValue();
}
2025-04-01 14:26:15 +00:00
}