Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

1.1 KB
Raw Blame
import i18n from 'i18next'

/**
 * Lazy "<Field> is required" message for Zod v4 schemas. Wrap in `{ error: ... }`
 * so the message is resolved per validation (not at module load) — keeps the
 * literal out of the schema and lets locale changes pick up.
 *
 * ```ts
 * z.string().min(1, { error: requiredMessage('name') })
 * z.string().min(1, { error: requiredMessage('payment_method.type') })
 * ```
 *
 * `field` is a dot-path under `admin.fields.*` resolving to a `.label` key —
 * `'name'` → `admin.fields.name.label`, `'payment_method.type'` →
 * `admin.fields.payment_method.type.label`.
 */
export function requiredMessage(field: string): () => string {
  return () => i18n.t('admin.validation.required', { field: i18n.t(`admin.fields.${field}.label`) })
}

/**
 * Lazy "<Field> can have at most <count> decimal places" message, for amounts
 * and rates the server keeps to a fixed number of decimals.
 */
export function decimalPlacesMessage(field: string, count: number): () => string {
  return () =>
    i18n.t('admin.validation.too_many_decimals', {
      field: i18n.t(`admin.fields.${field}.label`),
      count,
    })
}