Uecko_ERP/packages/rdx-ddd/src/value-objects/text-value.ts

40 lines
968 B
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 TextValueProps {
value: string;
}
export class TextValue extends ValueObject<TextValueProps> {
private static readonly MAX_LENGTH = 4096;
protected static validate(value: string) {
const schema = z
.string()
.trim()
.nonempty({ message: "Text must not be empty" })
.max(TextValue.MAX_LENGTH, {
message: `Text must be at most ${TextValue.MAX_LENGTH} characters long`,
});
return schema.safeParse(value);
}
static create(value: string) {
const valueIsValid = TextValue.validate(value);
if (!valueIsValid.success) {
return Result.fail(new Error(valueIsValid.error.issues[0].message));
}
return Result.ok(new TextValue({ value }));
}
2025-09-04 10:02:24 +00:00
getProps(): string {
2025-09-01 14:07:59 +00:00
return this.props.value;
}
toPrimitive(): string {
2025-09-05 11:23:45 +00:00
return String(this.getProps());
2025-09-01 14:07:59 +00:00
}
}