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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
import type { TaxRateCreateParams } from '@spree/admin-sdk'
import { compareMoney, isDecimalString } from '@spree/core'
import { blankToNull, countryCodeSchema } from '@spree/dashboard-core'
import { decimalPlacesMessage, requiredMessage } from '@spree/dashboard-ui'
import { z } from 'zod/v4'
export const taxRateFormSchema = z.object({
name: z.string().min(1, { error: requiredMessage('name') }),
// The merchant types a percentage (20); the API converts it to the fraction it stores.
rate_percent: z
.string()
.refine((value) => isDecimalString(value) && !value.startsWith('-'), {
error: requiredMessage('tax_rate.amount'),
})
// Stored as a fraction to five decimals, so a percentage keeps three.
.refine((value) => (value.split('.')[1] ?? '').length <= 3, {
error: decimalPlacesMessage('tax_rate.amount', 3),
})
.refine((value) => !isDecimalString(value) || compareMoney(value, '100') <= 0, {
error: requiredMessage('tax_rate.amount'),
}),
// Required: a rate always taxes exactly one category.
tax_category_id: z.string().min(1, { error: requiredMessage('tax_rate.tax_category_id') }),
// Empty means the rate applies everywhere, so neither field is required.
country_code: countryCodeSchema.or(z.literal('')).optional(),
state_code: z.string().optional(),
show_rate_in_label: z.boolean(),
})
export type TaxRateFormValues = z.infer<typeof taxRateFormSchema>
export const TAX_RATE_DEFAULTS: TaxRateFormValues = {
name: '',
rate_percent: '0',
tax_category_id: '',
country_code: '',
state_code: '',
show_rate_in_label: false,
}
/**
* A blank jurisdiction field is sent as null rather than omitted: clearing the
* country on an existing rate has to widen it back to everywhere, which an
* omitted key would not do.
*/
export function taxRateValuesToParams(values: TaxRateFormValues): TaxRateCreateParams {
return {
name: values.name,
rate_percent: values.rate_percent,
tax_category_id: values.tax_category_id,
country_code: values.country_code || null,
state_code: blankToNull(values.state_code),
show_rate_in_label: values.show_rate_in_label,
}
}