feed-common 1.36.1 → 1.37.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,9 +1,10 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
- import { SafeParseReturnType, ZodType } from 'zod';
2
+ import { SafeParseReturnType, z, ZodType } from 'zod';
3
3
  import { XMLFeedType } from '../../constants/profile.constants.js';
4
4
  import { customFeedTemplate } from './custom.template.js';
5
5
  import { facebookFeedTemplate } from './facebook.template.js';
6
6
  import { XmlFeedTemplateType } from '../../types/profile.types.js';
7
+ import { googleFeedTemplate } from './google.template.js';
7
8
 
8
9
  export const hasMacro = (v: string) => /{{[^}]+}}/.test(v);
9
10
  export const removeMacro = (v: string) => v.replaceAll(/{{[^}]+}}/g, '');
@@ -34,12 +35,56 @@ export function validateIfNoMacro(value: string | number, validator: ZodType<any
34
35
  return validator.safeParse(value);
35
36
  }
36
37
 
38
+ export const dateRangeSchema = z
39
+ .string()
40
+ .refine(v => !v || v.split('/').length === 2, {
41
+ message: 'The date range start and end should be separated by a "/"',
42
+ })
43
+ .refine(v => !v || z.string().datetime(v.split('/')[0]).safeParse(v).success, {
44
+ message: 'Date from should be a valid date',
45
+ })
46
+ .refine(v => !v || z.string().datetime(v.split('/')[1]).safeParse(v).success, {
47
+ message: 'Date to should be a valid date',
48
+ })
49
+ .refine(v => !v || new Date(v.split('/')[1]) > new Date(v.split('/')[0]), {
50
+ message: 'Date to should be greater than date from',
51
+ });
52
+
53
+ const unitMeasureRegex = /^(?<quantity>[\d.,]+)\s+(?<units>\w+)$/;
54
+
55
+ export const measureUnitsSchema = (units: string[]) =>
56
+ z.string().superRefine((v, ctx) => {
57
+ if (!v) {
58
+ return true;
59
+ }
60
+
61
+ const match = unitMeasureRegex.exec(v);
62
+
63
+ if (!match) {
64
+ ctx.addIssue({
65
+ code: z.ZodIssueCode.custom,
66
+ message: 'Invalid format',
67
+ });
68
+ } else if (!units.map(u => u.toLowerCase()).includes(match.groups?.units?.toLowerCase() as string)) {
69
+ ctx.addIssue({
70
+ code: z.ZodIssueCode.custom,
71
+ message: `Invalid unit. Valid units are: ${units.join(', ')}`,
72
+ });
73
+ }
74
+ });
75
+
76
+ export const optionalUrlSchema = z
77
+ .string()
78
+ .refine(v => !v || z.string().url().safeParse(v).success, { message: 'invalid URL' });
79
+
37
80
  export function getTemplate(type: XMLFeedType): XmlFeedTemplateType {
38
81
  switch (type) {
39
82
  case XMLFeedType.Custom:
40
83
  return customFeedTemplate;
41
84
  case XMLFeedType.Facebook:
42
85
  return facebookFeedTemplate;
86
+ case XMLFeedType.Google:
87
+ return googleFeedTemplate;
43
88
  default:
44
89
  throw new Error(`Invalid feed type: ${type}`);
45
90
  }