2025-09-01 14:07:59 +00:00
|
|
|
import { Result } from "@repo/rdx-utils";
|
|
|
|
|
import * as z from "zod/v4";
|
2025-09-16 11:29:45 +00:00
|
|
|
import { translateZodValidationError } from "../helpers";
|
2025-09-01 14:07:59 +00:00
|
|
|
import { ValueObject } from "./value-object";
|
|
|
|
|
|
|
|
|
|
interface CurrencyCodeProps {
|
|
|
|
|
value: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ISO 4217 Currency Codes
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
export class CurrencyCode extends ValueObject<CurrencyCodeProps> {
|
|
|
|
|
private static readonly MIN_LENGTH = 3;
|
|
|
|
|
private static readonly MAX_LENGTH = 3;
|
|
|
|
|
|
|
|
|
|
protected static validate(value: string) {
|
|
|
|
|
const schema = z
|
|
|
|
|
.string()
|
|
|
|
|
.trim()
|
|
|
|
|
.uppercase()
|
|
|
|
|
.min(CurrencyCode.MIN_LENGTH, {
|
|
|
|
|
message: `CurrencyCode must be at least ${CurrencyCode.MIN_LENGTH} characters long`,
|
|
|
|
|
})
|
|
|
|
|
.max(CurrencyCode.MAX_LENGTH, {
|
|
|
|
|
message: `CurrencyCode must be at most ${CurrencyCode.MAX_LENGTH} characters long`,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return schema.safeParse(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static create(value: string): Result<CurrencyCode, Error> {
|
|
|
|
|
const valueIsValid = CurrencyCode.validate(value);
|
|
|
|
|
|
|
|
|
|
if (!valueIsValid.success) {
|
2025-09-16 11:29:45 +00:00
|
|
|
return Result.fail(
|
|
|
|
|
translateZodValidationError("CurrencyCode creation failed", valueIsValid.error)
|
|
|
|
|
);
|
2025-09-01 14:07:59 +00:00
|
|
|
}
|
|
|
|
|
return Result.ok(new CurrencyCode({ value: valueIsValid.data }));
|
|
|
|
|
}
|
|
|
|
|
|
2025-09-05 11:23:45 +00:00
|
|
|
get code(): string {
|
|
|
|
|
return this.props.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 {
|
|
|
|
|
return this.props.value;
|
|
|
|
|
}
|
2025-09-10 16:06:29 +00:00
|
|
|
|
|
|
|
|
toString(): string {
|
|
|
|
|
return String(this.props.value);
|
|
|
|
|
}
|
2025-09-01 14:07:59 +00:00
|
|
|
}
|