47 lines
1.1 KiB
TypeScript
47 lines
1.1 KiB
TypeScript
import { Result } from "@repo/rdx-utils";
|
|
import { z } from "zod/v4";
|
|
import { translateZodValidationError } from "../helpers";
|
|
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(
|
|
translateZodValidationError("TextValue creation failed", valueIsValid.error)
|
|
);
|
|
}
|
|
return Result.ok(new TextValue({ value }));
|
|
}
|
|
|
|
getProps(): string {
|
|
return this.props.value;
|
|
}
|
|
|
|
toPrimitive(): string {
|
|
return this.toString();
|
|
}
|
|
|
|
toString(): string {
|
|
return String(this.getProps());
|
|
}
|
|
}
|