This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate.ts
490 lines (461 loc) Β· 15.2 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
import * as _ from 'lodash';
import { Visitor } from './ast';
import { Path, Error, ErrorFn, ResourceSpecificationError } from './types';
import {
PropertyTypes,
PropertyValueType,
PrimitiveType,
Type,
Exclusive,
OnlyOne,
AtLeastOne,
isMultiplePropertyType,
} from './spec';
import * as yaml from './yaml';
import { isNoValue, isStringNumber, isStringBoolean, cfnFnName, withSuggestion } from './util';
// `_.forEach` which tracks the `Path` and understands how to step into `CfnFn`s
export function forEach(
path: Path,
as: Array<any>,
fn: (path: Path, a: any, i: number | string) => void)
: void {
if (as instanceof yaml.If) {
if (_.isArray(as.data) && as.data.length === 3) {
_.forEach(as.data.slice(1, 3), (value, i) => {
if (_.isObject(value) && !isNoValue(value)) {
const p = path.concat([cfnFnName(as), (i + 1).toString()]);
forEach(p, value, fn);
}
});
}
} else if (as instanceof yaml.CfnFn) {
// Pull the *ItemType off of the CfnFn (if it has it) and create a
// `CfnFnData` that represents the structure of this CfnFn's items This
// allows us to validate things like `Fn::Split` which return a list of
// strings and be able to step into the `Fn::Split` rather than cutting off
// validation at the list level.
let returnSpecs: PropertyValueType[];
if (_.isFunction(as.returnSpec)) {
returnSpecs = as.returnSpec();
} else {
returnSpecs = as.returnSpec;
}
const dataReturnSpecs = _.reduce(returnSpecs, (acc, spec) => {
if (spec.PrimitiveItemType) {
acc.push({ PrimitiveType: spec.PrimitiveItemType })
} else if (spec.ItemType) {
acc.push({ Type: spec.ItemType })
}
return acc;
}, [] as PropertyValueType[]);
const data = new yaml.CfnFnData(as.data, as.style, dataReturnSpecs);
fn(path, data, cfnFnName(as));
} else {
_.forEach(as, (a, i) => {
fn(path.concat(i.toString()), a, i);
});
}
}
// Validation functions
//
// These functions are designed to be chained so that only one error message is
// created for each element. For example:
//
// validate.required(value) && validate.string(value);
//
// This will create only check if `value` is a string if it is considered to be
// present by the `requried` validation function. The functions return `false`
// when a violation is found. They can be used within a conditional to perform
// more complex validations, for example:
//
// if (validate.required(value) && validate.string(value)) {
// if(_.includes([...], value)) ...
// }
//
export interface ValidationFn {
(path: Path, o: any, addError: ErrorFn): boolean
}
export function cfnFn(cfnFn: yaml.CfnFn, spec: PropertyValueType): boolean {
let returnSpec: PropertyValueType[];
if (_.isFunction(cfnFn.returnSpec)) {
returnSpec = cfnFn.returnSpec();
} else {
returnSpec = cfnFn.returnSpec;
}
// Test if any of the `returnSpec`'s match
return _.some(returnSpec, (rs) => {
// Test if `rs` is a superset of spec. This it to support testing
// { Type: 'List' } without checking the ItemType.
// I can't explain why this works, but https://stackoverflow.com/a/26127030
return _.some([rs], spec);
});
}
// Used to short-circuit validation checks. For example:
//
// validate.optional(value) && validate.string(value);
//
export function optional(_path: Path, o: any, _addError: ErrorFn): boolean {
return !_.isUndefined(o);
}
export function required(path: Path, o: any, addError: ErrorFn): boolean {
if (_.isNil(o) || isNoValue(o)) {
addError(path, 'is required');
return false;
} else {
return true;
}
}
// Validate an object
//
// Optionally validate the object's properties. For example:
//
// validate.object(path, value, errors, {
// Name: [validate.required, validate.string],
// Age: [validate.optional, validate.number]
// });
//
export function object(path: Path,
o: any,
addError: ErrorFn,
properties: { [key: string]: ValidationFn[] } = {}): boolean {
if (o instanceof yaml.CfnFn && cfnFn(o, { PrimitiveType: 'Json' })) {
return true;
} else if (_.isPlainObject(o)) {
let valid = true;
// Validate the objects properties
_.forEach(properties, (fns, key) => {
const p = path.concat(key);
const hasError = _.find(fns, (fn) => !fn(p, _.get(o, key), addError));
if(hasError) {
valid = false;
}
});
// Check for unknown keys
const validKeys = _.keys(properties);
if (!_.isEmpty(validKeys)) {
const invalidKeys = _.difference(_.keys(o), validKeys);
if (!_.isEmpty(invalidKeys)) {
addError(path, `invalid properties: ${invalidKeys.join(', ')}`);
valid = false;
}
}
return valid;
} else {
addError(path, `must be an Object, got ${JSON.stringify(o)}`);
return false;
}
}
// Validate a list
//
// Optionally validate the list's items. For example:
//
// validate.list(path, value, errors, [validate.string, validate.number]);
//
// will check that the list is of length two, with the first element a string
// and the second a number. Lists that are of one type with a variable number of
// items can be validated by passing a single validation function:
//
// validate.list(path, value, errors, validate.string);
//
// which will check that all items in the list are of type string.
//
export function list(
path: Path,
o: any,
addError: ErrorFn,
items?: ValidationFn[] | ValidationFn): o is Array<any> {
if (o instanceof yaml.CfnFn && cfnFn(o, { Type: 'List' })) {
return true
} else if (_.isArray(o)) {
let valid = true;
if (_.isArray(items)) {
if (items.length === o.length) {
_.forEach(items, (fn, i) => {
const s = fn(path.concat(i.toString()), o[i], addError);
if (!s) {
valid = s;
}
});
} else {
addError(path, `must be a List with ${items.length} items`);
valid = false;
}
} else if (items) {
_.forEach(o, (item, i) => {
const s = items(path.concat(i.toString()), item, addError);
if(!s) {
valid = s;
}
});
}
return valid;
} else {
addError(path, `must be a List, got ${JSON.stringify(o)}`);
return false;
}
}
export function string(path: Path, o: any, addError: ErrorFn, allowedValues: string[] = []): boolean {
if (o instanceof yaml.CfnFn
&& (cfnFn(o, { PrimitiveType: 'String' }) || cfnFn(o, { PrimitiveType: 'Number' }))) {
return true;
} else if (_.isString(o)
|| _.isBoolean(o) // TODO better support 'true'/'false' in string values
|| _.isNumber(o) // YAML interprets number only strings as numbers
) {
if(_.some(allowedValues)) {
const str = o.toString();
if(_.find(allowedValues, (v) => new RegExp(v, 'i').test(str))) {
return true;
} else {
addError(path, `must be one of ${allowedValues.join(', ')}, got ${JSON.stringify(o)}`);
return false;
}
} else {
return true;
}
} else {
addError(path, `must be a String, got ${JSON.stringify(o)}`);
return false;
}
}
export function number(path: Path, o: any, addError: ErrorFn): boolean {
if (o instanceof yaml.CfnFn && cfnFn(o, { PrimitiveType: 'Number' })) {
return true;
} else if (_.isNumber(o)) {
return true;
} else if (isStringNumber(o)) {
return true;
} else {
addError(path, `must be a Number, got ${o}`);
return false;
}
}
export function boolean(path: Path, o: any, addError: ErrorFn): boolean {
if (o instanceof yaml.CfnFn && cfnFn(o, { PrimitiveType: 'Boolean' })) {
return true;
} else if (isStringBoolean(o)) {
return true;
} else if (_.isBoolean(o)) {
return true;
} else {
addError(path, `must be a Boolean, got ${JSON.stringify(o)}`);
return false;
}
}
export function or(...fns: ValidationFn[]): ValidationFn {
return (path: Path, value: any, addError: ErrorFn) => {
const errs: Error[] = [];
const f = (path: Path, message: string) => {
errs.push({ path, message, source: '-unused-' });
}
const success = _.some(fns, (fn) => fn(path, value, f));
if (!success) {
const message = _.join(_.map(errs, (e) => e.message), ' or ');
addError(path, message);
}
return success;
}
}
export class Validator extends Visitor {
protected errors: Error[];
constructor(errors: Error[]) {
super();
this.errors = errors;
this.addError = this.addError.bind(this);
}
protected addError(path: Path, message: string) {
const source = this.constructor.name;
this.errors.push({ path, message, source });
}
protected forEachWithPath<T>(
path: Path,
as: Array<T>,
fn: (path: Path, a: T, i: number | string) => void)
: void {
_.forEach(as, (a, i) => {
fn(path.concat(i.toString()), a, i);
});
}
}
function primitiveType(path: Path, primitiveType: PrimitiveType, property: any, addError: ErrorFn) {
let validator: (path: Path, o: any, addError: ErrorFn) => boolean;
switch (primitiveType) {
case 'Boolean':
validator = boolean;
break;
case 'Double':
validator = number;
break;
case 'Integer':
validator = number;
break;
case 'Json':
validator = object;
break;
case 'Long':
validator = number;
break;
case 'String':
validator = string;
break;
case 'Timestamp':
validator = string; // TODO better check
default:
throw new ResourceSpecificationError(`Unknown PrimitiveType '${primitiveType}'`, path);
}
validator.call(undefined, path, property, addError);
}
function atLeastOnePropertySpec(
path: Path,
propertyName: string,
_property: object,
resourceType: string,
addError: ErrorFn) {
const propertySpec = AtLeastOne.PropertyTypes[`${resourceType}.${propertyName}`];
if (propertySpec) {
_.forEach(propertySpec, (propertyNames) => {
const present = _.filter(propertyNames, (property) => _.has(property, propertyName));
if (present.length === 0) {
addError(path, `at least one of ${propertyNames.join(', ')} must be provided`);
}
});
}
}
function onlyOnePropertySpec(
path: Path,
propertyName: string,
_property: object,
resourceType: string,
addError: ErrorFn) {
const propertySpec = OnlyOne.PropertyTypes[`${resourceType}.${propertyName}`];
if (propertySpec) {
_.forEach(propertySpec, (propertyNames) => {
const present = _.filter(propertyNames, (property) => _.has(property, propertyName));
if (present.length > 1) {
addError(path, `only one of ${propertyNames.join(', ')} can be provided`);
}
});
}
}
function exclusivePropertySpec(
path: Path,
propertyName: string,
property: object,
resourceType: string,
addError: ErrorFn) {
const propertySpec = Exclusive.PropertyTypes[`${resourceType}.${propertyName}`];
if (propertySpec) {
_.forEach(propertySpec, (forbiddenProperties, propertyName) => {
if (_.has(property, propertyName)) {
_.forEach(forbiddenProperties, (forbiddenPropertyName) => {
if (_.has(property, forbiddenPropertyName)) {
addError(path, `${forbiddenPropertyName} can not be set if ${propertyName} is set`);
}
});
}
});
}
}
function complexType(
path: Path,
propertyName: string,
resourceType: string,
type: Type,
properties: any,
addError: ErrorFn) {
// TODO check that resourceType is valid
const propertyType = _.get(PropertyTypes, `${resourceType}.${type}`) || _.get(PropertyTypes, type);
if (propertyType) {
if (object(path, properties, addError)) {
exclusivePropertySpec(path, propertyName, properties, resourceType, addError);
onlyOnePropertySpec(path, propertyName, properties, resourceType, addError);
atLeastOnePropertySpec(path, propertyName, properties, resourceType, addError);
forEach(path, properties, (path, property, name) => {
let s: PropertyValueType;
if (isMultiplePropertyType(propertyType)) {
s = _.get(propertyType.Properties, name);
} else {
s = propertyType;
}
if (s) {
if (s.PrimitiveType) {
primitiveType(path, s.PrimitiveType, property, addError);
} else if (s.Type === 'Map') {
if (object(path, property, addError)) {
forEach(path, property, (p, v, k) => {
if (s.PrimitiveItemType) {
primitiveType(p, s.PrimitiveItemType, v, addError);
} else if (s.ItemType) {
complexType(p, k.toString(), resourceType, s.ItemType, v, addError);
} else {
throw new ResourceSpecificationError(`Unknown Map Type '${s}'`, p)
}
});
}
} else if (s.Type === 'List') {
if (list(path, property, addError)) {
forEach(path, property, (p, v, k) => {
if (s.PrimitiveItemType) {
primitiveType(p, s.PrimitiveItemType, v, addError);
} else if (s.ItemType) {
complexType(p, propertyName, resourceType, s.ItemType, v, addError);
} else {
throw new ResourceSpecificationError(`Unknown List Type '${s}'`, p)
}
});
}
} else if (s.Type) {
complexType(path, propertyName, resourceType, s.Type, property, addError);
}
} else {
let message: string = `invalid property for ${type}`;
if (isMultiplePropertyType(propertyType)) {
message = withSuggestion(message, _.keys(propertyType.Properties), name as string);
}
addError(path, message);
}
});
}
} else {
throw new ResourceSpecificationError(`Unknown Type '${propertyType}'`, path);
}
}
export function spec(
path: Path,
propertyName: string,
resourceType: string,
propertyType: PropertyValueType,
property: any,
addError: ErrorFn) {
if (propertyType.PrimitiveType) {
primitiveType(path, propertyType.PrimitiveType, property, addError);
} else if (propertyType.Type === 'Map') {
if (object(path, property, addError)) {
forEach(path, property, (p, v, i) => {
if (propertyType.PrimitiveItemType) {
primitiveType(p, propertyType.PrimitiveItemType, v, addError);
} else if (propertyType.ItemType) {
complexType(p, i.toString(), resourceType, propertyType.ItemType, v, addError);
} else {
throw new ResourceSpecificationError('No property type', p);
}
});
}
} else if (propertyType.Type === 'List') {
if (list(path, property, addError)) {
forEach(path, property, (p, v, i) => {
if (propertyType.PrimitiveItemType) {
primitiveType(p, propertyType.PrimitiveItemType, v, addError);
} else if (propertyType.ItemType) {
complexType(p, propertyName, resourceType, propertyType.ItemType, v, addError);
} else {
throw new ResourceSpecificationError('No property type', p);
}
});
}
} else if (propertyType.Type) {
complexType(path, propertyName, resourceType, propertyType.Type, property, addError);
} else {
throw new ResourceSpecificationError('No property type', path);
}
}