@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/dist/browser-analytics.cjs +1 -1
- package/dist/browser-analytics.cjs.map +1 -1
- package/dist/browser-analytics.esm.js +1 -6
- package/dist/browser-analytics.esm.js.map +1 -1
- package/dist/browser-analytics.iife.js +1 -1
- package/dist/browser-analytics.iife.js.map +1 -1
- package/package.json +1 -1
- package/src/events/identify/identifySchema.ts +1 -1
- package/src/services/validator/index.ts +4 -0
- package/src/services/validator/validator.test.ts +41 -0
- package/src/services/{validator.ts → validator/validator.ts} +4 -8
package/package.json
CHANGED
|
@@ -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
|
|
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
|
-
|
|
166
|
+
export { InitConfigSchema, eventSchemas };
|
|
171
167
|
export default validatorService;
|