@open-mercato/core 0.4.8-develop-4e71d95aba → 0.4.8-develop-2acbd97ec3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,57 @@
1
+ export const CATALOG_PRICE_MAX_INTEGER_DIGITS = 12
2
+ export const CATALOG_PRICE_MAX_FRACTION_DIGITS = 4
3
+
4
+ export type CatalogPriceAmountValidationReason =
5
+ | 'invalid_format'
6
+ | 'not_finite'
7
+ | 'negative'
8
+ | 'too_many_integer_digits'
9
+ | 'too_many_fraction_digits'
10
+
11
+ export type CatalogPriceAmountValidationResult =
12
+ | { ok: true; numeric: number }
13
+ | { ok: false; reason: CatalogPriceAmountValidationReason }
14
+
15
+ function normalizeCatalogPriceRawValue(value: unknown): string | null {
16
+ if (typeof value === 'number') {
17
+ if (!Number.isFinite(value)) return null
18
+ return String(value)
19
+ }
20
+ if (typeof value !== 'string') return null
21
+ const normalized = value.trim().replace(/\s+/g, '')
22
+ return normalized.length ? normalized : null
23
+ }
24
+
25
+ export function validateCatalogPriceAmountInput(
26
+ value: unknown,
27
+ ): CatalogPriceAmountValidationResult {
28
+ const raw = normalizeCatalogPriceRawValue(value)
29
+ if (!raw) return { ok: false, reason: 'invalid_format' }
30
+ if (raw.startsWith('-')) return { ok: false, reason: 'negative' }
31
+ if (!/^\d+(?:\.\d+)?$/.test(raw)) {
32
+ return { ok: false, reason: 'invalid_format' }
33
+ }
34
+
35
+ const numeric = Number(raw)
36
+ if (!Number.isFinite(numeric)) return { ok: false, reason: 'not_finite' }
37
+ if (numeric < 0) return { ok: false, reason: 'negative' }
38
+
39
+ const [integerPartRaw, fractionPart = ''] = raw.split('.')
40
+ const integerPart = integerPartRaw.replace(/^0+(?=\d)/, '')
41
+ if (integerPart.length > CATALOG_PRICE_MAX_INTEGER_DIGITS) {
42
+ return { ok: false, reason: 'too_many_integer_digits' }
43
+ }
44
+ if (fractionPart.length > CATALOG_PRICE_MAX_FRACTION_DIGITS) {
45
+ return { ok: false, reason: 'too_many_fraction_digits' }
46
+ }
47
+
48
+ return { ok: true, numeric }
49
+ }
50
+
51
+ export function isCatalogPriceAmountInputValid(value: unknown): boolean {
52
+ return validateCatalogPriceAmountInput(value).ok
53
+ }
54
+
55
+ export function getCatalogPriceAmountValidationMessage(): string {
56
+ return `Price must be a valid non-negative amount with at most ${CATALOG_PRICE_MAX_INTEGER_DIGITS} digits before the decimal point and ${CATALOG_PRICE_MAX_FRACTION_DIGITS} decimal places.`
57
+ }