feed-common 1.77.1 → 1.77.2
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.
- package/.vscode/settings.json +1 -1
- package/CHANGELOG.md +7 -0
- package/dist/constants/profile.constants.d.ts +12 -0
- package/dist/constants/profile.constants.d.ts.map +1 -1
- package/dist/constants/profile.constants.js +39 -1
- package/dist/constants/profile.constants.js.map +1 -1
- package/dist/index.d.ts +11 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +11 -1
- package/dist/index.js.map +1 -1
- package/dist/sources/country.source.d.ts +5 -0
- package/dist/sources/country.source.d.ts.map +1 -0
- package/dist/sources/country.source.js +78 -0
- package/dist/sources/country.source.js.map +1 -0
- package/dist/sources/currency.source.d.ts.map +1 -1
- package/dist/sources/currency.source.js +0 -1
- package/dist/sources/currency.source.js.map +1 -1
- package/dist/sources/ebay-gallery-type.source.d.ts +5 -0
- package/dist/sources/ebay-gallery-type.source.d.ts.map +1 -0
- package/dist/sources/ebay-gallery-type.source.js +5 -0
- package/dist/sources/ebay-gallery-type.source.js.map +1 -0
- package/dist/sources/ebay-inventory-tracking.source.d.ts +3 -0
- package/dist/sources/ebay-inventory-tracking.source.d.ts.map +1 -0
- package/dist/sources/ebay-inventory-tracking.source.js +11 -0
- package/dist/sources/ebay-inventory-tracking.source.js.map +1 -0
- package/dist/sources/ebay-listing-type.source.d.ts +5 -0
- package/dist/sources/ebay-listing-type.source.d.ts.map +1 -0
- package/dist/sources/ebay-listing-type.source.js +5 -0
- package/dist/sources/ebay-listing-type.source.js.map +1 -0
- package/dist/sources/ebay-yes-no.source.d.ts +5 -0
- package/dist/sources/ebay-yes-no.source.d.ts.map +1 -0
- package/dist/sources/ebay-yes-no.source.js +2 -0
- package/dist/sources/ebay-yes-no.source.js.map +1 -0
- package/dist/sources/google-availability.source.d.ts +5 -0
- package/dist/sources/google-availability.source.d.ts.map +1 -0
- package/dist/sources/google-availability.source.js +7 -0
- package/dist/sources/google-availability.source.js.map +1 -0
- package/dist/sources/language.source.d.ts +5 -0
- package/dist/sources/language.source.d.ts.map +1 -0
- package/dist/sources/language.source.js +31 -0
- package/dist/sources/language.source.js.map +1 -0
- package/dist/utils/feed-templates/{index.d.ts → common-schemas.d.ts} +24 -29
- package/dist/utils/feed-templates/common-schemas.d.ts.map +1 -0
- package/dist/utils/feed-templates/{index.js → common-schemas.js} +668 -176
- package/dist/utils/feed-templates/common-schemas.js.map +1 -0
- package/dist/utils/feed-templates/common.d.ts +20 -0
- package/dist/utils/feed-templates/common.d.ts.map +1 -0
- package/dist/utils/feed-templates/common.js +60 -0
- package/dist/utils/feed-templates/common.js.map +1 -0
- package/dist/utils/feed-templates/ebay.template.d.ts +1 -1
- package/dist/utils/feed-templates/ebay.template.d.ts.map +1 -1
- package/dist/utils/feed-templates/ebay.template.js +189 -2
- package/dist/utils/feed-templates/ebay.template.js.map +1 -1
- package/dist/utils/feed-templates/facebook.template.d.ts.map +1 -1
- package/dist/utils/feed-templates/facebook.template.js +2 -1
- package/dist/utils/feed-templates/facebook.template.js.map +1 -1
- package/dist/utils/feed-templates/google-api.template.d.ts.map +1 -1
- package/dist/utils/feed-templates/google-api.template.js +19 -7
- package/dist/utils/feed-templates/google-api.template.js.map +1 -1
- package/dist/utils/feed-templates/google.template.d.ts.map +1 -1
- package/dist/utils/feed-templates/google.template.js +7 -15
- package/dist/utils/feed-templates/google.template.js.map +1 -1
- package/dist/utils/feed-templates/microsoft.template.d.ts.map +1 -1
- package/dist/utils/feed-templates/microsoft.template.js +11 -7
- package/dist/utils/feed-templates/microsoft.template.js.map +1 -1
- package/dist/utils/feed-templates/template-utils.d.ts +4 -0
- package/dist/utils/feed-templates/template-utils.d.ts.map +1 -0
- package/dist/utils/feed-templates/template-utils.js +29 -0
- package/dist/utils/feed-templates/template-utils.js.map +1 -0
- package/dist/utils/feed-templates/tiktok.template.d.ts.map +1 -1
- package/dist/utils/feed-templates/tiktok.template.js +2 -1
- package/dist/utils/feed-templates/tiktok.template.js.map +1 -1
- package/dist/utils/profile.d.ts +2 -1
- package/dist/utils/profile.d.ts.map +1 -1
- package/dist/utils/profile.js +82 -1
- package/dist/utils/profile.js.map +1 -1
- package/dist/utils/utils.d.ts +2 -0
- package/dist/utils/utils.d.ts.map +1 -1
- package/dist/utils/utils.js +23 -3
- package/dist/utils/utils.js.map +1 -1
- package/package.json +5 -4
- package/src/constants/profile.constants.ts +39 -1
- package/src/index.ts +11 -1
- package/src/sources/country.source.ts +77 -0
- package/src/sources/currency.source.ts +0 -1
- package/src/sources/ebay-gallery-type.source.ts +4 -0
- package/src/sources/ebay-inventory-tracking.source.ts +12 -0
- package/src/sources/ebay-listing-type.source.ts +4 -0
- package/src/sources/ebay-yes-no.source.ts +1 -0
- package/src/sources/google-availability.source.ts +6 -0
- package/src/sources/language.source.ts +30 -0
- package/src/utils/feed-templates/common-schemas.ts +1014 -0
- package/src/utils/feed-templates/common.ts +82 -0
- package/src/utils/feed-templates/ebay.template.ts +215 -4
- package/src/utils/feed-templates/facebook.template.ts +2 -1
- package/src/utils/feed-templates/google-api.template.ts +54 -29
- package/src/utils/feed-templates/google.template.ts +46 -52
- package/src/utils/feed-templates/microsoft.template.ts +26 -21
- package/src/utils/feed-templates/template-utils.ts +30 -0
- package/src/utils/feed-templates/tiktok.template.ts +4 -2
- package/src/utils/profile.ts +116 -2
- package/src/utils/utils.ts +25 -5
- package/tests/company.spec.ts +39 -39
- package/tests/profile-utils.spec.ts +38 -0
- package/tests/validator.spec.ts +1 -5
- package/dist/utils/feed-templates/index.d.ts.map +0 -1
- package/dist/utils/feed-templates/index.js.map +0 -1
- package/src/utils/feed-templates/index.ts +0 -514
@@ -0,0 +1,1014 @@
|
|
1
|
+
import { SafeParseReturnType, z, ZodType } from 'zod';
|
2
|
+
import { SelectItem } from '../../types/profile.types';
|
3
|
+
import { resolveSourceMacro } from '../utils';
|
4
|
+
|
5
|
+
export const dateRangeSchema = z
|
6
|
+
.string()
|
7
|
+
.refine(v => !v || v.split('/').length === 2, {
|
8
|
+
message: 'The date range start and end should be separated by a "/"',
|
9
|
+
})
|
10
|
+
.refine(v => !v || z.string().datetime({ offset: true }).safeParse(v.split('/')[0]).success, {
|
11
|
+
message: 'Date from should be a valid date',
|
12
|
+
})
|
13
|
+
.refine(v => !v || z.string().datetime({ offset: true }).safeParse(v.split('/')[1]).success, {
|
14
|
+
message: 'Date to should be a valid date',
|
15
|
+
})
|
16
|
+
.refine(v => !v || new Date(v.split('/')[1]) > new Date(v.split('/')[0]), {
|
17
|
+
message: 'Date to should be greater than date from',
|
18
|
+
});
|
19
|
+
|
20
|
+
export const measureUnitsSchema = (units: string[]) =>
|
21
|
+
z.string().superRefine((v, ctx) => {
|
22
|
+
if (!v) {
|
23
|
+
return true;
|
24
|
+
}
|
25
|
+
|
26
|
+
const match = unitMeasureRegex.exec(v);
|
27
|
+
|
28
|
+
if (!match) {
|
29
|
+
ctx.addIssue({
|
30
|
+
code: z.ZodIssueCode.custom,
|
31
|
+
message: 'Invalid format',
|
32
|
+
});
|
33
|
+
} else if (!units.map(u => u.toLowerCase()).includes(match.groups?.units?.toLowerCase() as string)) {
|
34
|
+
ctx.addIssue({
|
35
|
+
code: z.ZodIssueCode.custom,
|
36
|
+
message: `Invalid unit. Valid units are: ${units.join(', ')}`,
|
37
|
+
});
|
38
|
+
}
|
39
|
+
});
|
40
|
+
|
41
|
+
export const sameUnitScheme = (other: string, errorMessage: string) =>
|
42
|
+
z.string().superRefine((v, ctx) => {
|
43
|
+
if (!v || !other) {
|
44
|
+
return true;
|
45
|
+
}
|
46
|
+
|
47
|
+
const match = unitMeasureRegex.exec(v);
|
48
|
+
const otherMatch = unitMeasureRegex.exec(other);
|
49
|
+
|
50
|
+
if (!match || !otherMatch) {
|
51
|
+
return true;
|
52
|
+
}
|
53
|
+
|
54
|
+
if (match.groups?.units !== otherMatch.groups?.units) {
|
55
|
+
ctx.addIssue({
|
56
|
+
code: z.ZodIssueCode.custom,
|
57
|
+
message: errorMessage,
|
58
|
+
});
|
59
|
+
}
|
60
|
+
});
|
61
|
+
|
62
|
+
export const optionalUrlSchema = z
|
63
|
+
.string()
|
64
|
+
.refine(v => !v || z.string().url().safeParse(v).success, { message: 'invalid URL' });
|
65
|
+
|
66
|
+
export const currencySchema = z.enum([
|
67
|
+
'AED',
|
68
|
+
'AFN',
|
69
|
+
'ALL',
|
70
|
+
'AMD',
|
71
|
+
'ANG',
|
72
|
+
'AOA',
|
73
|
+
'ARS',
|
74
|
+
'AUD',
|
75
|
+
'AWG',
|
76
|
+
'AZN',
|
77
|
+
'BAM',
|
78
|
+
'BBD',
|
79
|
+
'BDT',
|
80
|
+
'BGN',
|
81
|
+
'BHD',
|
82
|
+
'BIF',
|
83
|
+
'BMD',
|
84
|
+
'BND',
|
85
|
+
'BOB',
|
86
|
+
'BOV',
|
87
|
+
'BRL',
|
88
|
+
'BSD',
|
89
|
+
'BTN',
|
90
|
+
'BWP',
|
91
|
+
'BYN',
|
92
|
+
'BZD',
|
93
|
+
'CAD',
|
94
|
+
'CDF',
|
95
|
+
'CHE',
|
96
|
+
'CHF',
|
97
|
+
'CHW',
|
98
|
+
'CLF',
|
99
|
+
'CLP',
|
100
|
+
'CNY',
|
101
|
+
'COP',
|
102
|
+
'COU',
|
103
|
+
'CRC',
|
104
|
+
'CUC',
|
105
|
+
'CUP',
|
106
|
+
'CVE',
|
107
|
+
'CZK',
|
108
|
+
'DJF',
|
109
|
+
'DKK',
|
110
|
+
'DOP',
|
111
|
+
'DZD',
|
112
|
+
'EGP',
|
113
|
+
'ERN',
|
114
|
+
'ETB',
|
115
|
+
'EUR',
|
116
|
+
'FJD',
|
117
|
+
'FKP',
|
118
|
+
'FOK',
|
119
|
+
'GBP',
|
120
|
+
'GEL',
|
121
|
+
'GGP',
|
122
|
+
'GHS',
|
123
|
+
'GIP',
|
124
|
+
'GMD',
|
125
|
+
'GNF',
|
126
|
+
'GTQ',
|
127
|
+
'GYD',
|
128
|
+
'HKD',
|
129
|
+
'HNL',
|
130
|
+
'HRK',
|
131
|
+
'HTG',
|
132
|
+
'HUF',
|
133
|
+
'IDR',
|
134
|
+
'ILS',
|
135
|
+
'IMP',
|
136
|
+
'INR',
|
137
|
+
'IQD',
|
138
|
+
'IRR',
|
139
|
+
'ISK',
|
140
|
+
'JMD',
|
141
|
+
'JOD',
|
142
|
+
'JPY',
|
143
|
+
'KES',
|
144
|
+
'KGS',
|
145
|
+
'KHR',
|
146
|
+
'KID',
|
147
|
+
'KMF',
|
148
|
+
'KRW',
|
149
|
+
'KWD',
|
150
|
+
'KYD',
|
151
|
+
'KZT',
|
152
|
+
'LAK',
|
153
|
+
'LBP',
|
154
|
+
'LKR',
|
155
|
+
'LRD',
|
156
|
+
'LSL',
|
157
|
+
'LYD',
|
158
|
+
'MAD',
|
159
|
+
'MDL',
|
160
|
+
'MGA',
|
161
|
+
'MKD',
|
162
|
+
'MMK',
|
163
|
+
'MNT',
|
164
|
+
'MOP',
|
165
|
+
'MRU',
|
166
|
+
'MUR',
|
167
|
+
'MVR',
|
168
|
+
'MWK',
|
169
|
+
'MXN',
|
170
|
+
'MXV',
|
171
|
+
'MYR',
|
172
|
+
'MZN',
|
173
|
+
'NAD',
|
174
|
+
'NGN',
|
175
|
+
'NIO',
|
176
|
+
'NOK',
|
177
|
+
'NPR',
|
178
|
+
'NZD',
|
179
|
+
'OMR',
|
180
|
+
'PAB',
|
181
|
+
'PEN',
|
182
|
+
'PGK',
|
183
|
+
'PHP',
|
184
|
+
'PKR',
|
185
|
+
'PLN',
|
186
|
+
'PYG',
|
187
|
+
'QAR',
|
188
|
+
'RON',
|
189
|
+
'RSD',
|
190
|
+
'RUB',
|
191
|
+
'RWF',
|
192
|
+
'SAR',
|
193
|
+
'SBD',
|
194
|
+
'SCR',
|
195
|
+
'SDG',
|
196
|
+
'SEK',
|
197
|
+
'SGD',
|
198
|
+
'SHP',
|
199
|
+
'SLE',
|
200
|
+
'SLL',
|
201
|
+
'SOS',
|
202
|
+
'SRD',
|
203
|
+
'SSP',
|
204
|
+
'STN',
|
205
|
+
'SYP',
|
206
|
+
'SZL',
|
207
|
+
'THB',
|
208
|
+
'TJS',
|
209
|
+
'TMT',
|
210
|
+
'TND',
|
211
|
+
'TOP',
|
212
|
+
'TRY',
|
213
|
+
'TTD',
|
214
|
+
'TVD',
|
215
|
+
'TWD',
|
216
|
+
'TZS',
|
217
|
+
'UAH',
|
218
|
+
'UGX',
|
219
|
+
'USD',
|
220
|
+
'USN',
|
221
|
+
'UYI',
|
222
|
+
'UYU',
|
223
|
+
'UYW',
|
224
|
+
'UZS',
|
225
|
+
'VED',
|
226
|
+
'VES',
|
227
|
+
'VND',
|
228
|
+
'VUV',
|
229
|
+
'WST',
|
230
|
+
'XAF',
|
231
|
+
'XAG',
|
232
|
+
'XAU',
|
233
|
+
'XBA',
|
234
|
+
'XBB',
|
235
|
+
'XBC',
|
236
|
+
'XBD',
|
237
|
+
'XCD',
|
238
|
+
'XDR',
|
239
|
+
'XOF',
|
240
|
+
'XPD',
|
241
|
+
'XPF',
|
242
|
+
'XPT',
|
243
|
+
'XSU',
|
244
|
+
'XTS',
|
245
|
+
'XUA',
|
246
|
+
'XXX',
|
247
|
+
'YER',
|
248
|
+
'ZAR',
|
249
|
+
'ZMW',
|
250
|
+
'ZWL',
|
251
|
+
]);
|
252
|
+
|
253
|
+
export const languageSchema = z
|
254
|
+
.string()
|
255
|
+
.refine(
|
256
|
+
v =>
|
257
|
+
!v ||
|
258
|
+
[
|
259
|
+
'aa',
|
260
|
+
'ab',
|
261
|
+
'ae',
|
262
|
+
'af',
|
263
|
+
'ak',
|
264
|
+
'am',
|
265
|
+
'an',
|
266
|
+
'ar',
|
267
|
+
'as',
|
268
|
+
'av',
|
269
|
+
'ay',
|
270
|
+
'az',
|
271
|
+
'ba',
|
272
|
+
'be',
|
273
|
+
'bg',
|
274
|
+
'bh',
|
275
|
+
'bi',
|
276
|
+
'bm',
|
277
|
+
'bn',
|
278
|
+
'bo',
|
279
|
+
'br',
|
280
|
+
'bs',
|
281
|
+
'ca',
|
282
|
+
'ce',
|
283
|
+
'ch',
|
284
|
+
'co',
|
285
|
+
'cr',
|
286
|
+
'cs',
|
287
|
+
'cu',
|
288
|
+
'cv',
|
289
|
+
'cy',
|
290
|
+
'da',
|
291
|
+
'de',
|
292
|
+
'dv',
|
293
|
+
'dz',
|
294
|
+
'ee',
|
295
|
+
'el',
|
296
|
+
'en',
|
297
|
+
'eo',
|
298
|
+
'es',
|
299
|
+
'et',
|
300
|
+
'eu',
|
301
|
+
'fa',
|
302
|
+
'ff',
|
303
|
+
'fi',
|
304
|
+
'fj',
|
305
|
+
'fo',
|
306
|
+
'fr',
|
307
|
+
'fy',
|
308
|
+
'ga',
|
309
|
+
'gd',
|
310
|
+
'gl',
|
311
|
+
'gn',
|
312
|
+
'gu',
|
313
|
+
'gv',
|
314
|
+
'ha',
|
315
|
+
'he',
|
316
|
+
'hi',
|
317
|
+
'ho',
|
318
|
+
'hr',
|
319
|
+
'ht',
|
320
|
+
'hu',
|
321
|
+
'hy',
|
322
|
+
'hz',
|
323
|
+
'ia',
|
324
|
+
'id',
|
325
|
+
'ie',
|
326
|
+
'ig',
|
327
|
+
'ii',
|
328
|
+
'ik',
|
329
|
+
'io',
|
330
|
+
'is',
|
331
|
+
'it',
|
332
|
+
'iu',
|
333
|
+
'ja',
|
334
|
+
'jv',
|
335
|
+
'ka',
|
336
|
+
'kg',
|
337
|
+
'ki',
|
338
|
+
'kj',
|
339
|
+
'kk',
|
340
|
+
'kl',
|
341
|
+
'km',
|
342
|
+
'kn',
|
343
|
+
'ko',
|
344
|
+
'kr',
|
345
|
+
'ks',
|
346
|
+
'ku',
|
347
|
+
'kv',
|
348
|
+
'kw',
|
349
|
+
'ky',
|
350
|
+
'la',
|
351
|
+
'lb',
|
352
|
+
'lg',
|
353
|
+
'li',
|
354
|
+
'ln',
|
355
|
+
'lo',
|
356
|
+
'lt',
|
357
|
+
'lu',
|
358
|
+
'lv',
|
359
|
+
'mg',
|
360
|
+
'mh',
|
361
|
+
'mi',
|
362
|
+
'mk',
|
363
|
+
'ml',
|
364
|
+
'mn',
|
365
|
+
'mr',
|
366
|
+
'ms',
|
367
|
+
'mt',
|
368
|
+
'my',
|
369
|
+
'na',
|
370
|
+
'nb',
|
371
|
+
'nd',
|
372
|
+
'ne',
|
373
|
+
'ng',
|
374
|
+
'nl',
|
375
|
+
'nn',
|
376
|
+
'no',
|
377
|
+
'nr',
|
378
|
+
'nv',
|
379
|
+
'ny',
|
380
|
+
'oc',
|
381
|
+
'oj',
|
382
|
+
'om',
|
383
|
+
'or',
|
384
|
+
'os',
|
385
|
+
'pa',
|
386
|
+
'pi',
|
387
|
+
'pl',
|
388
|
+
'ps',
|
389
|
+
'pt',
|
390
|
+
'qu',
|
391
|
+
'rm',
|
392
|
+
'rn',
|
393
|
+
'ro',
|
394
|
+
'ru',
|
395
|
+
'rw',
|
396
|
+
'sa',
|
397
|
+
'sc',
|
398
|
+
'sd',
|
399
|
+
'se',
|
400
|
+
'sg',
|
401
|
+
'si',
|
402
|
+
'sk',
|
403
|
+
'sl',
|
404
|
+
'sm',
|
405
|
+
'sn',
|
406
|
+
'so',
|
407
|
+
'sq',
|
408
|
+
'sr',
|
409
|
+
'ss',
|
410
|
+
'st',
|
411
|
+
'su',
|
412
|
+
'sv',
|
413
|
+
'sw',
|
414
|
+
'ta',
|
415
|
+
'te',
|
416
|
+
'tg',
|
417
|
+
'th',
|
418
|
+
'ti',
|
419
|
+
'tk',
|
420
|
+
'tl',
|
421
|
+
'tn',
|
422
|
+
'to',
|
423
|
+
'tr',
|
424
|
+
'ts',
|
425
|
+
'tt',
|
426
|
+
'tw',
|
427
|
+
'ty',
|
428
|
+
'ug',
|
429
|
+
'uk',
|
430
|
+
'ur',
|
431
|
+
'uz',
|
432
|
+
've',
|
433
|
+
'vi',
|
434
|
+
'vo',
|
435
|
+
'wa',
|
436
|
+
'wo',
|
437
|
+
'xh',
|
438
|
+
'yi',
|
439
|
+
'yo',
|
440
|
+
'za',
|
441
|
+
'zh',
|
442
|
+
'zu',
|
443
|
+
].includes(v.toLowerCase()),
|
444
|
+
{ message: 'Invalid language code' }
|
445
|
+
);
|
446
|
+
|
447
|
+
export const countrySchema = z
|
448
|
+
.string()
|
449
|
+
.refine(
|
450
|
+
v =>
|
451
|
+
!v ||
|
452
|
+
[
|
453
|
+
'AD',
|
454
|
+
'AE',
|
455
|
+
'AF',
|
456
|
+
'AG',
|
457
|
+
'AI',
|
458
|
+
'AL',
|
459
|
+
'AM',
|
460
|
+
'AO',
|
461
|
+
'AQ',
|
462
|
+
'AR',
|
463
|
+
'AS',
|
464
|
+
'AT',
|
465
|
+
'AU',
|
466
|
+
'AW',
|
467
|
+
'AX',
|
468
|
+
'AZ',
|
469
|
+
'BA',
|
470
|
+
'BB',
|
471
|
+
'BD',
|
472
|
+
'BE',
|
473
|
+
'BF',
|
474
|
+
'BG',
|
475
|
+
'BH',
|
476
|
+
'BI',
|
477
|
+
'BJ',
|
478
|
+
'BL',
|
479
|
+
'BM',
|
480
|
+
'BN',
|
481
|
+
'BO',
|
482
|
+
'BQ',
|
483
|
+
'BR',
|
484
|
+
'BS',
|
485
|
+
'BT',
|
486
|
+
'BV',
|
487
|
+
'BW',
|
488
|
+
'BY',
|
489
|
+
'BZ',
|
490
|
+
'CA',
|
491
|
+
'CC',
|
492
|
+
'CD',
|
493
|
+
'CF',
|
494
|
+
'CG',
|
495
|
+
'CH',
|
496
|
+
'CI',
|
497
|
+
'CK',
|
498
|
+
'CL',
|
499
|
+
'CM',
|
500
|
+
'CN',
|
501
|
+
'CO',
|
502
|
+
'CR',
|
503
|
+
'CU',
|
504
|
+
'CV',
|
505
|
+
'CW',
|
506
|
+
'CX',
|
507
|
+
'CY',
|
508
|
+
'CZ',
|
509
|
+
'DE',
|
510
|
+
'DJ',
|
511
|
+
'DK',
|
512
|
+
'DM',
|
513
|
+
'DO',
|
514
|
+
'DZ',
|
515
|
+
'EC',
|
516
|
+
'EE',
|
517
|
+
'EG',
|
518
|
+
'EH',
|
519
|
+
'ER',
|
520
|
+
'ES',
|
521
|
+
'ET',
|
522
|
+
'FI',
|
523
|
+
'FJ',
|
524
|
+
'FM',
|
525
|
+
'FO',
|
526
|
+
'FR',
|
527
|
+
'GA',
|
528
|
+
'GB',
|
529
|
+
'GD',
|
530
|
+
'GE',
|
531
|
+
'GF',
|
532
|
+
'GG',
|
533
|
+
'GH',
|
534
|
+
'GI',
|
535
|
+
'GL',
|
536
|
+
'GM',
|
537
|
+
'GN',
|
538
|
+
'GP',
|
539
|
+
'GQ',
|
540
|
+
'GR',
|
541
|
+
'GT',
|
542
|
+
'GU',
|
543
|
+
'GW',
|
544
|
+
'GY',
|
545
|
+
'HK',
|
546
|
+
'HM',
|
547
|
+
'HN',
|
548
|
+
'HR',
|
549
|
+
'HT',
|
550
|
+
'HU',
|
551
|
+
'ID',
|
552
|
+
'IE',
|
553
|
+
'IL',
|
554
|
+
'IM',
|
555
|
+
'IN',
|
556
|
+
'IO',
|
557
|
+
'IQ',
|
558
|
+
'IR',
|
559
|
+
'IS',
|
560
|
+
'IT',
|
561
|
+
'JE',
|
562
|
+
'JM',
|
563
|
+
'JO',
|
564
|
+
'JP',
|
565
|
+
'KE',
|
566
|
+
'KG',
|
567
|
+
'KH',
|
568
|
+
'KI',
|
569
|
+
'KM',
|
570
|
+
'KN',
|
571
|
+
'KP',
|
572
|
+
'KR',
|
573
|
+
'KW',
|
574
|
+
'KY',
|
575
|
+
'KZ',
|
576
|
+
'LA',
|
577
|
+
'LB',
|
578
|
+
'LC',
|
579
|
+
'LI',
|
580
|
+
'LK',
|
581
|
+
'LR',
|
582
|
+
'LS',
|
583
|
+
'LT',
|
584
|
+
'LU',
|
585
|
+
'LV',
|
586
|
+
'LY',
|
587
|
+
'MA',
|
588
|
+
'MC',
|
589
|
+
'MD',
|
590
|
+
'ME',
|
591
|
+
'MF',
|
592
|
+
'MG',
|
593
|
+
'MH',
|
594
|
+
'MK',
|
595
|
+
'ML',
|
596
|
+
'MM',
|
597
|
+
'MN',
|
598
|
+
'MO',
|
599
|
+
'MP',
|
600
|
+
'MQ',
|
601
|
+
'MR',
|
602
|
+
'MS',
|
603
|
+
'MT',
|
604
|
+
'MU',
|
605
|
+
'MV',
|
606
|
+
'MW',
|
607
|
+
'MX',
|
608
|
+
'MY',
|
609
|
+
'MZ',
|
610
|
+
'NA',
|
611
|
+
'NC',
|
612
|
+
'NE',
|
613
|
+
'NF',
|
614
|
+
'NG',
|
615
|
+
'NI',
|
616
|
+
'NL',
|
617
|
+
'NO',
|
618
|
+
'NP',
|
619
|
+
'NR',
|
620
|
+
'NU',
|
621
|
+
'NZ',
|
622
|
+
'OM',
|
623
|
+
'PA',
|
624
|
+
'PE',
|
625
|
+
'PF',
|
626
|
+
'PG',
|
627
|
+
'PH',
|
628
|
+
'PK',
|
629
|
+
'PL',
|
630
|
+
'PM',
|
631
|
+
'PN',
|
632
|
+
'PR',
|
633
|
+
'PT',
|
634
|
+
'PW',
|
635
|
+
'PY',
|
636
|
+
'QA',
|
637
|
+
'RE',
|
638
|
+
'RO',
|
639
|
+
'RS',
|
640
|
+
'RU',
|
641
|
+
'RW',
|
642
|
+
'SA',
|
643
|
+
'SB',
|
644
|
+
'SC',
|
645
|
+
'SD',
|
646
|
+
'SE',
|
647
|
+
'SG',
|
648
|
+
'SH',
|
649
|
+
'SI',
|
650
|
+
'SJ',
|
651
|
+
'SK',
|
652
|
+
'SL',
|
653
|
+
'SM',
|
654
|
+
'SN',
|
655
|
+
'SO',
|
656
|
+
'SR',
|
657
|
+
'SS',
|
658
|
+
'ST',
|
659
|
+
'SV',
|
660
|
+
'SX',
|
661
|
+
'SY',
|
662
|
+
'SZ',
|
663
|
+
'TC',
|
664
|
+
'TD',
|
665
|
+
'TF',
|
666
|
+
'TG',
|
667
|
+
'TH',
|
668
|
+
'TJ',
|
669
|
+
'TK',
|
670
|
+
'TL',
|
671
|
+
'TM',
|
672
|
+
'TN',
|
673
|
+
'TO',
|
674
|
+
'TR',
|
675
|
+
'TT',
|
676
|
+
'TV',
|
677
|
+
'TZ',
|
678
|
+
'UA',
|
679
|
+
'UG',
|
680
|
+
'UM',
|
681
|
+
'US',
|
682
|
+
'UY',
|
683
|
+
'UZ',
|
684
|
+
'VA',
|
685
|
+
'VC',
|
686
|
+
'VE',
|
687
|
+
'VG',
|
688
|
+
'VI',
|
689
|
+
'VN',
|
690
|
+
'VU',
|
691
|
+
'WF',
|
692
|
+
'WS',
|
693
|
+
'YE',
|
694
|
+
'YT',
|
695
|
+
'ZA',
|
696
|
+
'ZM',
|
697
|
+
'ZW',
|
698
|
+
].includes(v.toUpperCase()),
|
699
|
+
{ message: 'Invalid country code' }
|
700
|
+
);
|
701
|
+
|
702
|
+
export const priceSchema = ({ min = 0, max = 1000000 } = {}) =>
|
703
|
+
z.string().superRefine((v, ctx) => {
|
704
|
+
if (!v) {
|
705
|
+
return true;
|
706
|
+
}
|
707
|
+
|
708
|
+
try {
|
709
|
+
parsePriceString({ price: v, min, max });
|
710
|
+
return true;
|
711
|
+
} catch (e: any) {
|
712
|
+
ctx.addIssue({
|
713
|
+
code: z.ZodIssueCode.custom,
|
714
|
+
message: e.message,
|
715
|
+
});
|
716
|
+
}
|
717
|
+
});
|
718
|
+
|
719
|
+
export const salePriceSchema = ({ min = 0, max = 1000000, price = '' } = {}) =>
|
720
|
+
z.string().superRefine((v, ctx) => {
|
721
|
+
if (!v) {
|
722
|
+
return true;
|
723
|
+
}
|
724
|
+
|
725
|
+
try {
|
726
|
+
const { currency, value } = parsePriceString({ price: v, min, max });
|
727
|
+
|
728
|
+
if (price) {
|
729
|
+
try {
|
730
|
+
const { currency: priceCurrency, value: priceAmount } = parsePriceString({ price, min, max });
|
731
|
+
|
732
|
+
if (currency !== priceCurrency) {
|
733
|
+
ctx.addIssue({
|
734
|
+
code: z.ZodIssueCode.custom,
|
735
|
+
message: 'Sale price currency should be the same as the price currency',
|
736
|
+
});
|
737
|
+
return;
|
738
|
+
}
|
739
|
+
|
740
|
+
if (Number(value) >= Number(priceAmount)) {
|
741
|
+
ctx.addIssue({
|
742
|
+
code: z.ZodIssueCode.custom,
|
743
|
+
message: 'Sale price should be lower than the price',
|
744
|
+
});
|
745
|
+
return;
|
746
|
+
}
|
747
|
+
} catch (e: any) {}
|
748
|
+
}
|
749
|
+
return true;
|
750
|
+
} catch (e: any) {
|
751
|
+
ctx.addIssue({
|
752
|
+
code: z.ZodIssueCode.custom,
|
753
|
+
message: e.message,
|
754
|
+
});
|
755
|
+
}
|
756
|
+
});
|
757
|
+
|
758
|
+
export const energyEfficiencyClassSchema = (
|
759
|
+
{ minEfficiency = '', maxEfficiency } = {} as { minEfficiency?: string; maxEfficiency?: string }
|
760
|
+
) => {
|
761
|
+
return z.string().superRefine((v, ctx) => {
|
762
|
+
if (!v) {
|
763
|
+
if (maxEfficiency || minEfficiency) {
|
764
|
+
ctx.addIssue({
|
765
|
+
code: z.ZodIssueCode.custom,
|
766
|
+
message: 'Energy efficiency class is required',
|
767
|
+
});
|
768
|
+
return false;
|
769
|
+
}
|
770
|
+
return true;
|
771
|
+
}
|
772
|
+
|
773
|
+
if (!ee.includes(v)) {
|
774
|
+
ctx.addIssue({
|
775
|
+
code: z.ZodIssueCode.custom,
|
776
|
+
message: 'Invalid energy efficiency class',
|
777
|
+
});
|
778
|
+
return false;
|
779
|
+
}
|
780
|
+
|
781
|
+
if (minEfficiency && !compareEfficiency(minEfficiency, v)) {
|
782
|
+
ctx.addIssue({
|
783
|
+
code: z.ZodIssueCode.custom,
|
784
|
+
message: 'Energy efficiency class should be greater or equal to the minimum energy efficiency class',
|
785
|
+
});
|
786
|
+
return false;
|
787
|
+
}
|
788
|
+
|
789
|
+
if (maxEfficiency && !compareEfficiency(v, maxEfficiency)) {
|
790
|
+
ctx.addIssue({
|
791
|
+
code: z.ZodIssueCode.custom,
|
792
|
+
message: 'Energy efficiency class should be lower or equal to the maximum energy efficiency class',
|
793
|
+
});
|
794
|
+
return false;
|
795
|
+
}
|
796
|
+
return true;
|
797
|
+
});
|
798
|
+
};
|
799
|
+
|
800
|
+
export const minEnergyEfficiencyClassSchema = (
|
801
|
+
{ efficiency = '', maxEfficiency } = {} as { efficiency?: string; maxEfficiency?: string }
|
802
|
+
) => {
|
803
|
+
return z.string().superRefine((v, ctx) => {
|
804
|
+
if (!v) {
|
805
|
+
if (maxEfficiency || efficiency) {
|
806
|
+
ctx.addIssue({
|
807
|
+
code: z.ZodIssueCode.custom,
|
808
|
+
message: 'Minimum energy efficiency class is required',
|
809
|
+
});
|
810
|
+
return false;
|
811
|
+
}
|
812
|
+
return true;
|
813
|
+
}
|
814
|
+
|
815
|
+
if (!ee.includes(v)) {
|
816
|
+
ctx.addIssue({
|
817
|
+
code: z.ZodIssueCode.custom,
|
818
|
+
message: 'Invalid energy efficiency class',
|
819
|
+
});
|
820
|
+
return false;
|
821
|
+
}
|
822
|
+
|
823
|
+
if (efficiency && !compareEfficiency(v, efficiency)) {
|
824
|
+
ctx.addIssue({
|
825
|
+
code: z.ZodIssueCode.custom,
|
826
|
+
message: 'Minimum energy efficiency class should be lower or equal to the energy efficiency class',
|
827
|
+
});
|
828
|
+
return false;
|
829
|
+
}
|
830
|
+
|
831
|
+
if (maxEfficiency && !compareEfficiency(v, maxEfficiency)) {
|
832
|
+
ctx.addIssue({
|
833
|
+
code: z.ZodIssueCode.custom,
|
834
|
+
message:
|
835
|
+
'Minimum energy efficiency class should be lower or equal to the maximum energy efficiency class',
|
836
|
+
});
|
837
|
+
return false;
|
838
|
+
}
|
839
|
+
return true;
|
840
|
+
});
|
841
|
+
};
|
842
|
+
|
843
|
+
export const maxEnergyEfficiencyClassSchema = (
|
844
|
+
{ efficiency = '', minEfficiency } = {} as { efficiency?: string; minEfficiency?: string }
|
845
|
+
) => {
|
846
|
+
return z.string().superRefine((v, ctx) => {
|
847
|
+
if (!v) {
|
848
|
+
if (minEfficiency || efficiency) {
|
849
|
+
ctx.addIssue({
|
850
|
+
code: z.ZodIssueCode.custom,
|
851
|
+
message: 'Maximum energy efficiency class is required',
|
852
|
+
});
|
853
|
+
return false;
|
854
|
+
}
|
855
|
+
return true;
|
856
|
+
}
|
857
|
+
|
858
|
+
if (!ee.includes(v)) {
|
859
|
+
ctx.addIssue({
|
860
|
+
code: z.ZodIssueCode.custom,
|
861
|
+
message: 'Invalid energy efficiency class',
|
862
|
+
});
|
863
|
+
return false;
|
864
|
+
}
|
865
|
+
|
866
|
+
if (efficiency && !compareEfficiency(efficiency, v)) {
|
867
|
+
ctx.addIssue({
|
868
|
+
code: z.ZodIssueCode.custom,
|
869
|
+
message: 'Maximum energy efficiency class should be greater or equal to the energy efficiency class',
|
870
|
+
});
|
871
|
+
return false;
|
872
|
+
}
|
873
|
+
|
874
|
+
if (minEfficiency && !compareEfficiency(minEfficiency, v)) {
|
875
|
+
ctx.addIssue({
|
876
|
+
code: z.ZodIssueCode.custom,
|
877
|
+
message:
|
878
|
+
'Maximum energy efficiency class should be greater or equal to the minimum energy efficiency class',
|
879
|
+
});
|
880
|
+
return false;
|
881
|
+
}
|
882
|
+
return true;
|
883
|
+
});
|
884
|
+
};
|
885
|
+
|
886
|
+
export const gtinSchema = z.string().refine(
|
887
|
+
gtin => {
|
888
|
+
if (!gtin) {
|
889
|
+
return true;
|
890
|
+
}
|
891
|
+
|
892
|
+
const length = gtin.length;
|
893
|
+
|
894
|
+
if (![8, 12, 13, 14].includes(length)) {
|
895
|
+
return false;
|
896
|
+
}
|
897
|
+
|
898
|
+
const digits = gtin.split('').map(Number);
|
899
|
+
const checkDigit = digits.pop();
|
900
|
+
const sum = digits.reverse().reduce((acc, digit, idx) => {
|
901
|
+
return acc + digit * (idx % 2 === 0 ? 3 : 1);
|
902
|
+
}, 0);
|
903
|
+
|
904
|
+
const calculatedCheckDigit = (10 - (sum % 10)) % 10;
|
905
|
+
return checkDigit === calculatedCheckDigit;
|
906
|
+
},
|
907
|
+
{
|
908
|
+
message: 'Invalid GTIN',
|
909
|
+
}
|
910
|
+
);
|
911
|
+
|
912
|
+
export const requiredIfOtherPresentSchema = (other: string[], errorMessage: string) =>
|
913
|
+
z.string().superRefine((v, ctx) => {
|
914
|
+
if (!v && other.some(o => o)) {
|
915
|
+
ctx.addIssue({
|
916
|
+
code: z.ZodIssueCode.custom,
|
917
|
+
message: errorMessage,
|
918
|
+
});
|
919
|
+
}
|
920
|
+
});
|
921
|
+
|
922
|
+
export const oneOfSchema = (values: SelectItem[]) =>
|
923
|
+
z.string().refine(v => values.map(v => v.value).includes(v), {
|
924
|
+
message: `Invalid value. Valid values are: ${values.map(v => v.label).join(', ')}`,
|
925
|
+
});
|
926
|
+
|
927
|
+
export const oneOfOptionalSchema = (values: SelectItem[]) => z.string().refine(v => !v || oneOfSchema(values).parse(v));
|
928
|
+
|
929
|
+
export function parsePriceString({ price, min = 0, max = 1000000 }: { price: string; min?: number; max?: number }): {
|
930
|
+
value: string;
|
931
|
+
currency: string;
|
932
|
+
} {
|
933
|
+
const match = priceRegex.exec(price);
|
934
|
+
|
935
|
+
if (!match) {
|
936
|
+
throw new Error('Invalid format');
|
937
|
+
} else if (
|
938
|
+
!Number.isFinite(Number(match.groups?.value)) ||
|
939
|
+
Number(match.groups?.value) < min ||
|
940
|
+
Number(match.groups?.value) > max
|
941
|
+
) {
|
942
|
+
throw new Error(`Value should be between ${min} and ${max}`);
|
943
|
+
} else if (!currencySchema.safeParse(match.groups?.currency).success) {
|
944
|
+
throw new Error('Invalid currency');
|
945
|
+
}
|
946
|
+
|
947
|
+
return { value: match.groups?.value as string, currency: match.groups?.currency as string };
|
948
|
+
}
|
949
|
+
export const unitMeasureRegex = /^(?<quantity>[\d.,]+)\s*(?<units>\w+)$/;
|
950
|
+
|
951
|
+
export const priceRegex = /^(?<value>\d+(?:\.\d{1,2})?)\s*(?<currency>\w+)$/;
|
952
|
+
|
953
|
+
export const hasMacro = (v: string) => /{{[^}]+}}/.test(v.replaceAll(/{{source\..+?}}/g, ''));
|
954
|
+
|
955
|
+
export const removeMacro = (v: string) => v.replaceAll(/{{.+?}}/g, '');
|
956
|
+
|
957
|
+
export const dimensionRegex = /^(?<value>[\d.,]+)\s*(?<unit>\w+)$/;
|
958
|
+
|
959
|
+
export const ee = ['A+++', 'A++', 'A+', 'A', 'B', 'C', 'D', 'E', 'F', 'G'];
|
960
|
+
/**
|
961
|
+
* Check if minEfficiency is less or equal to maxEfficiency
|
962
|
+
* @param minEfficiency
|
963
|
+
* @param maxEfficiency
|
964
|
+
* @returns
|
965
|
+
*/
|
966
|
+
|
967
|
+
export const compareEfficiency = (minEfficiency: string, maxEfficiency: string): boolean => {
|
968
|
+
const minIndex = ee.findIndex(e => e === minEfficiency);
|
969
|
+
const maxIndex = ee.findIndex(e => e === maxEfficiency);
|
970
|
+
|
971
|
+
if (minIndex === -1 || maxIndex === -1) {
|
972
|
+
return false;
|
973
|
+
}
|
974
|
+
|
975
|
+
return minIndex >= maxIndex;
|
976
|
+
};
|
977
|
+
export function validate(
|
978
|
+
value: string | number,
|
979
|
+
validator: ZodType<any, any>,
|
980
|
+
skipEmpty = true
|
981
|
+
): SafeParseReturnType<any, any> {
|
982
|
+
let valueToValidate = value;
|
983
|
+
|
984
|
+
if (typeof value === 'string') {
|
985
|
+
valueToValidate = removeMacro(value);
|
986
|
+
}
|
987
|
+
|
988
|
+
if (!value && skipEmpty) {
|
989
|
+
return { success: true, data: value };
|
990
|
+
}
|
991
|
+
|
992
|
+
return validator.safeParse(valueToValidate);
|
993
|
+
}
|
994
|
+
|
995
|
+
export function validateIfNoMacro(
|
996
|
+
value: string | number,
|
997
|
+
validator: ZodType<any, any> | ZodType<any, any>[]
|
998
|
+
): SafeParseReturnType<any, any> {
|
999
|
+
if (typeof value === 'string' && hasMacro(value)) {
|
1000
|
+
return { success: true, data: value };
|
1001
|
+
}
|
1002
|
+
|
1003
|
+
if (Array.isArray(validator)) {
|
1004
|
+
for (const v of validator) {
|
1005
|
+
const result = v.safeParse(resolveSourceMacro(value));
|
1006
|
+
if (!result.success) {
|
1007
|
+
return result;
|
1008
|
+
}
|
1009
|
+
}
|
1010
|
+
return { success: true, data: value };
|
1011
|
+
}
|
1012
|
+
|
1013
|
+
return validator.safeParse(resolveSourceMacro(value));
|
1014
|
+
}
|