@mparticle/web-sdk 2.15.5 → 2.16.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.
@@ -0,0 +1,163 @@
1
+ import Types from './types';
2
+ import { isObject } from './utils';
3
+ import Constants from './constants';
4
+ import { IdentityApiData } from '@mparticle/web-sdk';
5
+ import { valueof } from './utils';
6
+
7
+ type IdentityAPIMethod = 'login' | 'logout' | 'identify' | 'modify';
8
+
9
+ type ValidationIdentitiesReturn = {
10
+ valid: boolean;
11
+ error?: valueof<typeof Constants.Messages.ValidationMessages>;
12
+ };
13
+
14
+ const Validators = {
15
+ isValidAttributeValue: function(value: any): boolean {
16
+ return value !== undefined && !isObject(value) && !Array.isArray(value);
17
+ },
18
+
19
+ // Neither null nor undefined can be a valid Key
20
+ isValidKeyValue: function(key: any): boolean {
21
+ return Boolean(
22
+ key &&
23
+ !isObject(key) &&
24
+ !Array.isArray(key) &&
25
+ !this.isFunction(key)
26
+ );
27
+ },
28
+
29
+ isStringOrNumber: function(value: any): boolean {
30
+ return typeof value === 'string' || typeof value === 'number';
31
+ },
32
+
33
+ isNumber: function(value: any): boolean {
34
+ return typeof value === 'number';
35
+ },
36
+
37
+ isFunction: (fn: any): boolean => {
38
+ return typeof fn === 'function';
39
+ },
40
+
41
+ validateIdentities: function(
42
+ identityApiData: IdentityApiData,
43
+ method?: IdentityAPIMethod
44
+ ): ValidationIdentitiesReturn {
45
+ var validIdentityRequestKeys = {
46
+ userIdentities: 1,
47
+ onUserAlias: 1,
48
+ copyUserAttributes: 1,
49
+ };
50
+ if (identityApiData) {
51
+ if (method === 'modify') {
52
+ if (
53
+ (isObject(identityApiData.userIdentities) &&
54
+ !Object.keys(identityApiData.userIdentities).length) ||
55
+ !isObject(identityApiData.userIdentities)
56
+ ) {
57
+ return {
58
+ valid: false,
59
+ error:
60
+ Constants.Messages.ValidationMessages
61
+ .ModifyIdentityRequestUserIdentitiesPresent,
62
+ };
63
+ }
64
+ }
65
+ for (var key in identityApiData) {
66
+ if (identityApiData.hasOwnProperty(key)) {
67
+ if (!validIdentityRequestKeys[key]) {
68
+ return {
69
+ valid: false,
70
+ error:
71
+ Constants.Messages.ValidationMessages
72
+ .IdentityRequesetInvalidKey,
73
+ };
74
+ }
75
+ if (
76
+ key === 'onUserAlias' &&
77
+ !Validators.isFunction(identityApiData[key])
78
+ ) {
79
+ return {
80
+ valid: false,
81
+ error:
82
+ Constants.Messages.ValidationMessages
83
+ .OnUserAliasType,
84
+ };
85
+ }
86
+ }
87
+ }
88
+ if (Object.keys(identityApiData).length === 0) {
89
+ return {
90
+ valid: true,
91
+ };
92
+ } else {
93
+ // identityApiData.userIdentities can't be undefined
94
+ if (identityApiData.userIdentities === undefined) {
95
+ return {
96
+ valid: false,
97
+ error:
98
+ Constants.Messages.ValidationMessages
99
+ .UserIdentities,
100
+ };
101
+ // identityApiData.userIdentities can be null, but if it isn't null or undefined (above conditional), it must be an object
102
+ } else if (
103
+ identityApiData.userIdentities !== null &&
104
+ !isObject(identityApiData.userIdentities)
105
+ ) {
106
+ return {
107
+ valid: false,
108
+ error:
109
+ Constants.Messages.ValidationMessages
110
+ .UserIdentities,
111
+ };
112
+ }
113
+ if (
114
+ isObject(identityApiData.userIdentities) &&
115
+ Object.keys(identityApiData.userIdentities).length
116
+ ) {
117
+ for (var identityType in identityApiData.userIdentities) {
118
+ if (
119
+ identityApiData.userIdentities.hasOwnProperty(
120
+ identityType
121
+ )
122
+ ) {
123
+ if (
124
+ Types.IdentityType.getIdentityType(
125
+ identityType
126
+ ) === false
127
+ ) {
128
+ return {
129
+ valid: false,
130
+ error:
131
+ Constants.Messages.ValidationMessages
132
+ .UserIdentitiesInvalidKey,
133
+ };
134
+ }
135
+ if (
136
+ !(
137
+ typeof identityApiData.userIdentities[
138
+ identityType
139
+ ] === 'string' ||
140
+ identityApiData.userIdentities[
141
+ identityType
142
+ ] === null
143
+ )
144
+ ) {
145
+ return {
146
+ valid: false,
147
+ error:
148
+ Constants.Messages.ValidationMessages
149
+ .UserIdentitiesInvalidValues,
150
+ };
151
+ }
152
+ }
153
+ }
154
+ }
155
+ }
156
+ }
157
+ return {
158
+ valid: true,
159
+ };
160
+ },
161
+ };
162
+
163
+ export default Validators;