This repository has been archived on 2026-01-22. You can view files and clone it, but cannot push or open issues or pull requests.
factuges_2025/modules/invoices/src/server/domain/value-objects/invoice-number.ts

43 lines
974 B
TypeScript
Raw Normal View History

2025-04-27 20:47:47 +00:00
import { ValueObject } from "@rdx/ddd-domain";
import { Result } from "@rdx/utils";
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();
}
toPrimitive() {
return this.getValue();
}
}