Uecko_ERP/modules.bak/invoices/src/server/domain/value-objects/invoice-serie.ts
2025-06-24 20:38:57 +02:00

51 lines
1.2 KiB
TypeScript

import { ValueObject } from "core/common/domain";
import { Maybe, Result } from "core/common/helpers";
import * as z from "zod/v4";
interface IInvoiceSerieProps {
value: string;
}
export class InvoiceSerie extends ValueObject<IInvoiceSerieProps> {
private static readonly MAX_LENGTH = 255;
protected static validate(value: string) {
const schema = z
.string()
.trim()
.max(InvoiceSerie.MAX_LENGTH, {
message: `Name must be at most ${InvoiceSerie.MAX_LENGTH} characters long`,
});
return schema.safeParse(value);
}
static create(value: string) {
const valueIsValid = InvoiceSerie.validate(value);
if (!valueIsValid.success) {
return Result.fail(new Error(valueIsValid.error.errors[0].message));
}
return Result.ok(new InvoiceSerie({ value }));
}
static createNullable(value?: string): Result<Maybe<InvoiceSerie>, Error> {
if (!value || value.trim() === "") {
return Result.ok(Maybe.none<InvoiceSerie>());
}
return InvoiceSerie.create(value).map((value) => Maybe.some(value));
}
getValue(): string {
return this.props.value;
}
toString(): string {
return this.getValue();
}
toPrimitive() {
return this.getValue();
}
}