/
validate.ts
194 lines (158 loc) · 5.53 KB
/
validate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import { flags } from '@oclif/command';
import Base from '../../../base';
import { JSONFileSync } from '../../../utils/JSONFileSync';
import { getObject } from '../../../utils/getObject';
import { cli } from 'cli-ux';
import { BaseEvent } from '@mparticle/event-models';
import { DataPlan, DataPlanVersion } from '@mparticle/data-planning-models';
import { expand } from '@mparticle/model-translation';
const pjson = require('../../../../package.json');
export default class DataPlanEventValidate extends Base {
static description = `Validates an Event
Data Plans are comprised of one or more Data Plan Versions and are used to validate an Event.
A Data Plan Version can be directly referenced by using either the --dataPlanVersion or --dataPlanVersionFile flags
Otherwise, a --dataPlan or --dataPlanFile must be accompanied by a --versionNumber.
For more information, visit: ${pjson.homepage}
`;
static aliases = ['plan:e:val'];
static examples = [
`$ mp planning:events:validate --event=[EVENT] --dataPlan=[DATA_PLAN] --versionNumber=[VERSION_NUMBER]`,
`$ mp planning:events:validate --event=[EVENT] --dataPlanVersion=[DATA_PLAN_VERSION]`,
`$ mp planning:events:validate --event=[EVENT] --dataPlanVersion=[DATA_PLAN_VERSION] --translateEvents`,
`$ mp planning:events:validate --eventFile=/path/to/event --dataPlanFile=/path/to/dataplan --versionNumber=[VERSION_NUMBER]`,
`$ mp planning:events:validate --eventFile=/path/to/event --dataPlanVersionFile=/path/to/dataplanversion`,
`$ mp planning:events:validate --eventFile=/path/to/event --dataPlanVersionFile=/path/to/dataplanversion --translateEvents`,
];
static flags = {
...Base.flags,
event: flags.string({
description: 'Event as Stringified JSON',
exclusive: ['eventFile'],
}),
eventFile: flags.string({
description: 'Path to saved JSON file of an Event',
exclusive: ['event'],
}),
dataPlan: flags.string({
description: 'Data Plan as Stringified JSON',
exclusive: ['dataPlanFile'],
dependsOn: ['versionNumber'],
}),
dataPlanFile: flags.string({
description: 'Path to saved JSON file of a Data Plan',
exclusive: ['dataPlan'],
dependsOn: ['versionNumber'],
}),
// required with data plan file str
versionNumber: flags.integer({
description: 'Data Plan Version Number',
}),
dataPlanVersion: flags.string({
description: 'Data Plan Version Document as Stringified JSON',
}),
dataPlanVersionFile: flags.string({
description: 'Path to saved JSON file of a Data Plan Version',
}),
translateEvents: flags.boolean({
description: 'Translate minified event into standard event',
}),
serverMode: flags.boolean({
description: 'Validate using mParticle Server-side validation',
}),
};
async run() {
const { flags } = this.parse(DataPlanEventValidate);
const {
outFile,
eventFile,
dataPlanFile,
serverMode,
versionNumber,
translateEvents,
} = flags;
const eventStr = flags.event;
const dataPlanStr = flags.dataPlan;
const dataPlanVersionStr = flags.dataPlanVersion;
let dataPlanVersionFile =
flags.dataPlanVersionFile ??
this.mPConfig.planningConfig?.dataPlanVersionFile;
if (!eventStr && !eventFile) {
this.error('Please provide an event');
}
if (
!dataPlanStr &&
!dataPlanFile &&
!dataPlanVersionStr &&
!dataPlanVersionFile
) {
this.error(
'Please provide a Data Plan or Version Document to Validate against'
);
}
let initialEvent;
try {
initialEvent = getObject<BaseEvent>(eventStr, eventFile);
} catch (error) {
this._debugLog('Error Fetching Event Object', error);
this.error(error);
}
let event;
if (initialEvent && translateEvents) {
this._debugLog('Event Before Expansion', initialEvent);
event = expand(initialEvent) as BaseEvent;
} else {
event = initialEvent;
}
this._debugLog('Event Received', event);
if (!event) {
this.error('Event is invalid');
}
const dataPlan = getObject<DataPlan>(dataPlanStr, dataPlanFile);
this._debugLog('Data Plan received', dataPlan);
let dataPlanVersion = getObject<DataPlanVersion>(
dataPlanVersionStr,
dataPlanVersionFile
);
this._debugLog('Data Plan Version received', dataPlanVersion);
if (dataPlan && versionNumber) {
dataPlanVersion = dataPlan.data_plan_versions?.find(
(planVersion: DataPlanVersion) => planVersion.version === versionNumber
);
}
if (!dataPlanVersion) {
this.error('Data Plan Version is Invalid');
}
cli.action.start('Validating Event');
const options = {
serverMode,
};
const dataPlanService = this.getDataPlanService(this.credentials);
let results;
try {
results = await dataPlanService.validateEvent(
event,
dataPlanVersion,
options
);
} catch (error) {
this._debugLog('Validation Service Error', error);
if (error.errors) {
const errorMessage = 'Server Validation Failed:';
this.error(this._generateErrorList(errorMessage, error.errors));
}
this.error(error);
}
if (outFile) {
try {
cli.action.start('Writing Results');
const writer = new JSONFileSync(outFile);
writer.write(results);
} catch (error) {
throw new Error(error.toString());
}
} else {
this.log(JSON.stringify(results, null, 4));
}
cli.action.stop();
}
}