@reservamos/browser-analytics 1.0.2 → 1.0.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reservamos/browser-analytics",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/reservamos/reservamos-browser-analytics.git"
@@ -6,7 +6,7 @@ const IdentifySchema = z.object({
6
6
  email: z.string().email().optional(),
7
7
  phone: z
8
8
  .string()
9
- .regex(/^\+?[1-9]\d{1,14}$/)
9
+ .regex(/^(\+?[1-9]\d{1,14})?$/)
10
10
  .optional(),
11
11
  });
12
12
 
@@ -0,0 +1,4 @@
1
+ import validatorService, { eventSchemas, InitConfigSchema } from './validator';
2
+
3
+ export { InitConfigSchema, eventSchemas };
4
+ export default validatorService;
@@ -0,0 +1,41 @@
1
+ import { describe, expect, it } from 'vitest';
2
+ import { customEventSchema } from '@/events/customEvent';
3
+ import validatorService, { eventSchemas } from './validator';
4
+
5
+ describe('parseEventProps', () => {
6
+ /**
7
+ * This test supposes that unknownEvent is not a known event.
8
+ * And that the customEventSchema does not support objects as values for a field.
9
+ */
10
+ it('Should use the customEventSchema if the event name is not a known event.', () => {
11
+ const customEventName = 'unknownEvent';
12
+ const invalidCustomEventData = {
13
+ test: {
14
+ test: 'test',
15
+ },
16
+ };
17
+ expect(Object.keys(eventSchemas).includes(customEventName)).toBe(false);
18
+ expect(() =>
19
+ validatorService.validateProps(invalidCustomEventData, customEventSchema),
20
+ ).toThrow();
21
+ expect(() =>
22
+ validatorService.parseEventProps(customEventName, invalidCustomEventData),
23
+ ).toThrow();
24
+ });
25
+
26
+ /**
27
+ * This test supposes that all known event schema validations
28
+ * will fail if the event data is empty.
29
+ */
30
+ it('Should use the known event schema if the event name is a known event.', () => {
31
+ const invalidKnownEventData = {};
32
+ Object.entries(eventSchemas).forEach(([eventName, eventSchema]) => {
33
+ expect(() =>
34
+ validatorService.validateProps(invalidKnownEventData, eventSchema),
35
+ ).toThrow();
36
+ expect(() =>
37
+ validatorService.parseEventProps(eventName, invalidKnownEventData),
38
+ ).toThrow();
39
+ });
40
+ });
41
+ });
@@ -11,8 +11,7 @@ import { searchSchema } from '@/events/search';
11
11
  import { seatChangeSchema } from '@/events/seatChange';
12
12
  import { viewResultsSchema } from '@/events/viewResults';
13
13
 
14
- const TrackTestEventSchema = z.object({}); // Allow empty object for track test event
15
- export const InitConfigSchema = z.object({
14
+ const InitConfigSchema = z.object({
16
15
  /**
17
16
  * The Mixpanel token used for authenticating API requests.
18
17
  */
@@ -40,6 +39,7 @@ export const InitConfigSchema = z.object({
40
39
  */
41
40
  identifyProxyUrl: z.string().optional(),
42
41
  });
42
+
43
43
  interface CustomError {
44
44
  field: string;
45
45
  error_type: string;
@@ -48,6 +48,7 @@ interface CustomError {
48
48
  message: string;
49
49
  suggestion: string;
50
50
  }
51
+
51
52
  // Error formatting
52
53
  const SchemaErrorFormatter = (error: ZodError): CustomError[] => {
53
54
  return error.issues.map((issue) => {
@@ -84,7 +85,6 @@ const SchemaErrorFormatter = (error: ZodError): CustomError[] => {
84
85
 
85
86
  // Mapping event names to Zod schemas
86
87
  const eventSchemas: Record<string, z.ZodSchema> = {
87
- 'Track Test': TrackTestEventSchema,
88
88
  'Search': searchSchema,
89
89
  'Seat Change': seatChangeSchema,
90
90
  'Interest In Home': interestInHomeSchema,
@@ -109,10 +109,6 @@ type EventDataSchema = z.infer<
109
109
  function parseEventProps(eventName: string, eventData: EventDataSchema): void {
110
110
  const eventSchema = eventSchemas[eventName] || customEventSchema;
111
111
 
112
- if (!eventSchema) {
113
- throw { message: `Event ${eventName} not found` };
114
- }
115
-
116
112
  try {
117
113
  eventSchema.parse(eventData);
118
114
  } catch (error) {
@@ -167,5 +163,5 @@ const validatorService = {
167
163
  validateProps,
168
164
  };
169
165
 
170
- // Export the validator object
166
+ export { InitConfigSchema, eventSchemas };
171
167
  export default validatorService;