feed-common 1.36.1 → 1.37.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
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';
@@ -34,6 +34,48 @@ export function validateIfNoMacro(value: string | number, validator: ZodType<any
34
34
  return validator.safeParse(value);
35
35
  }
36
36
 
37
+ export const dateRangeSchema = z
38
+ .string()
39
+ .refine(v => !v || v.split('/').length === 2, {
40
+ message: 'The date range start and end should be separated by a "/"',
41
+ })
42
+ .refine(v => !v || z.string().datetime(v.split('/')[0]).safeParse(v).success, {
43
+ message: 'Date from should be a valid date',
44
+ })
45
+ .refine(v => !v || z.string().datetime(v.split('/')[1]).safeParse(v).success, {
46
+ message: 'Date to should be a valid date',
47
+ })
48
+ .refine(v => !v || new Date(v.split('/')[1]) > new Date(v.split('/')[0]), {
49
+ message: 'Date to should be greater than date from',
50
+ });
51
+
52
+ const unitMeasureRegex = /^(?<quantity>[\d.,]+)\s+(?<units>\w+)$/;
53
+
54
+ export const measureUnitsSchema = (units: string[]) =>
55
+ z.string().superRefine((v, ctx) => {
56
+ if (!v) {
57
+ return true;
58
+ }
59
+
60
+ const match = unitMeasureRegex.exec(v);
61
+
62
+ if (!match) {
63
+ ctx.addIssue({
64
+ code: z.ZodIssueCode.custom,
65
+ message: 'Invalid format',
66
+ });
67
+ } else if (!units.map(u => u.toLowerCase()).includes(match.groups?.units?.toLowerCase() as string)) {
68
+ ctx.addIssue({
69
+ code: z.ZodIssueCode.custom,
70
+ message: `Invalid unit. Valid units are: ${units.join(', ')}`,
71
+ });
72
+ }
73
+ });
74
+
75
+ export const optionalUrlSchema = z
76
+ .string()
77
+ .refine(v => !v || z.string().url().safeParse(v).success, { message: 'invalid URL' });
78
+
37
79
  export function getTemplate(type: XMLFeedType): XmlFeedTemplateType {
38
80
  switch (type) {
39
81
  case XMLFeedType.Custom: