51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
|
|
import { ValueObject } from "@repo/rdx-ddd";
|
||
|
|
import { Maybe, Result } from "@repo/rdx-utils";
|
||
|
|
import { z } from "zod";
|
||
|
|
|
||
|
|
interface ICustomerInvoiceItemDescriptionProps {
|
||
|
|
value: string;
|
||
|
|
}
|
||
|
|
|
||
|
|
export class CustomerInvoiceItemDescription extends ValueObject<ICustomerInvoiceItemDescriptionProps> {
|
||
|
|
private static readonly MAX_LENGTH = 255;
|
||
|
|
|
||
|
|
protected static validate(value: string) {
|
||
|
|
const schema = z
|
||
|
|
.string()
|
||
|
|
.trim()
|
||
|
|
.max(CustomerInvoiceItemDescription.MAX_LENGTH, {
|
||
|
|
message: `Description must be at most ${CustomerInvoiceItemDescription.MAX_LENGTH} characters long`,
|
||
|
|
});
|
||
|
|
return schema.safeParse(value);
|
||
|
|
}
|
||
|
|
|
||
|
|
static create(value: string) {
|
||
|
|
const valueIsValid = CustomerInvoiceItemDescription.validate(value);
|
||
|
|
|
||
|
|
if (!valueIsValid.success) {
|
||
|
|
return Result.fail(new Error(valueIsValid.error.errors[0].message));
|
||
|
|
}
|
||
|
|
return Result.ok(new CustomerInvoiceItemDescription({ value }));
|
||
|
|
}
|
||
|
|
|
||
|
|
static createNullable(value?: string): Result<Maybe<CustomerInvoiceItemDescription>, Error> {
|
||
|
|
if (!value || value.trim() === "") {
|
||
|
|
return Result.ok(Maybe.none<CustomerInvoiceItemDescription>());
|
||
|
|
}
|
||
|
|
|
||
|
|
return CustomerInvoiceItemDescription.create(value).map((value) => Maybe.some(value));
|
||
|
|
}
|
||
|
|
|
||
|
|
getValue(): string {
|
||
|
|
return this.props.value;
|
||
|
|
}
|
||
|
|
|
||
|
|
toString(): string {
|
||
|
|
return this.getValue();
|
||
|
|
}
|
||
|
|
|
||
|
|
toPrimitive() {
|
||
|
|
return this.getValue();
|
||
|
|
}
|
||
|
|
}
|