-
-
Notifications
You must be signed in to change notification settings - Fork 97
/
resourcevalidator.js
573 lines (514 loc) · 20.8 KB
/
resourcevalidator.js
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const ClassDeclaration = require('../introspect/classdeclaration');
const Field = require('../introspect/field');
const RelationshipDeclaration = require('../introspect/relationshipdeclaration');
const EnumDeclaration = require('../introspect/enumdeclaration');
const Relationship = require('../model/relationship');
const Resource = require('../model/resource');
const Concept = require('../model/concept');
const Identifiable = require('../model/identifiable');
const Util = require('../util');
const ModelUtil = require('../modelutil');
const ValidationException = require('./validationexception');
const Globalize = require('../globalize');
const Moment = require('moment-mini');
/**
* <p>
* Validates a Resource or Field against the models defined in the ModelManager.
* This class is used with the Visitor pattern and visits the class declarations
* (etc) for the model, checking that the data in a Resource / Field is consistent
* with the model.
* </p>
* The parameters for the visit method must contain the following properties:
* <ul>
* <li> 'stack' - the TypedStack of objects being processed. It should
* start as [Resource] or [Field]</li>
* <li> 'rootResourceIdentifier' - the identifier of the resource being validated </li>
* <li> 'modelManager' - the ModelManager instance to use for type checking</li>
* </ul>
* @private
* @class
* @memberof module:concerto-core
*/
class ResourceValidator {
/**
* ResourceValidator constructor
* @param {Object} options - the optional serialization options.
* @param {boolean} options.validate - validate the structure of the Resource
* with its model prior to serialization (default to true)
* @param {boolean} options.convertResourcesToRelationships - Convert resources that
* are specified for relationship fields into relationships, false by default.
* @param {boolean} options.permitResourcesForRelationships - Permit resources in the
*/
constructor(options) {
this.options = options || {};
}
/**
* Visitor design pattern.
*
* @param {Object} thing - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visit(thing, parameters) {
if (thing instanceof EnumDeclaration) {
return this.visitEnumDeclaration(thing, parameters);
} else if (thing instanceof ClassDeclaration) {
return this.visitClassDeclaration(thing, parameters);
} else if (thing instanceof RelationshipDeclaration) {
return this.visitRelationshipDeclaration(thing, parameters);
} else if (thing instanceof Field) {
return this.visitField(thing, parameters);
}
}
/**
* Visitor design pattern
*
* @param {EnumDeclaration} enumDeclaration - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visitEnumDeclaration(enumDeclaration, parameters) {
const obj = parameters.stack.pop();
// now check that obj is one of the enum values
const properties = enumDeclaration.getProperties();
let found = false;
for(let n=0; n < properties.length; n++) {
const property = properties[n];
if(property.getName() === obj) {
found = true;
}
}
if(!found) {
ResourceValidator.reportInvalidEnumValue( parameters.rootResourceIdentifier, enumDeclaration, obj );
}
return null;
}
/**
* Visitor design pattern
* @param {ClassDeclaration} classDeclaration - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visitClassDeclaration(classDeclaration, parameters) {
const obj = parameters.stack.pop();
// are we dealing with a Resouce?
if(!((obj instanceof Resource) || (obj instanceof Concept))) {
ResourceValidator.reportNotResouceViolation(parameters.rootResourceIdentifier, classDeclaration, obj );
}
if(obj instanceof Identifiable) {
parameters.rootResourceIdentifier = obj.getFullyQualifiedIdentifier();
}
const toBeAssignedClassDeclaration = parameters.modelManager.getType(obj.getFullyQualifiedType());
const toBeAssignedClassDecName = toBeAssignedClassDeclaration.getFullyQualifiedName();
// is the type we are assigning to abstract?
// the only way this can happen is if the type is non-abstract
// and then gets redeclared as abstract
if(toBeAssignedClassDeclaration.isAbstract()) {
ResourceValidator.reportAbstractClass(toBeAssignedClassDeclaration);
}
// are there extra fields in the object?
let props = Object.getOwnPropertyNames(obj);
for (let n = 0; n < props.length; n++) {
let propName = props[n];
if(!this.isSystemProperty(propName)) {
const field = toBeAssignedClassDeclaration.getProperty(propName);
if (!field) {
if(obj instanceof Identifiable) {
ResourceValidator.reportUndeclaredField(obj.getIdentifier(), propName, toBeAssignedClassDecName);
}
else {
ResourceValidator.reportUndeclaredField(parameters.currentIdentifier, propName, toBeAssignedClassDecName);
}
}
}
}
if(obj instanceof Identifiable) {
const id = obj.getIdentifier();
// prevent empty identifiers
if(!id || id.trim().length === 0) {
ResourceValidator.reportEmptyIdentifier(parameters.rootResourceIdentifier);
}
parameters.currentIdentifier = obj.getFullyQualifiedIdentifier();
}
// now validate each property
const properties = toBeAssignedClassDeclaration.getProperties();
for(let n=0; n < properties.length; n++) {
const property = properties[n];
const value = obj[property.getName()];
if(!Util.isNull(value)) {
parameters.stack.push(value);
property.accept(this,parameters);
}
else {
if(!property.isOptional()) {
ResourceValidator.reportMissingRequiredProperty( parameters.rootResourceIdentifier, property);
}
}
}
return null;
}
/**
* Returns true if the property is a system property.
* System properties are not declared in the model.
* @param {String} propertyName - the name of the property
* @return {Boolean} true if the property is a system property
* @private
*/
isSystemProperty(propertyName) {
return propertyName.charAt(0) === '$';
}
/**
* Visitor design pattern
* @param {Field} field - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visitField(field, parameters) {
const obj = parameters.stack.pop();
let dataType = typeof(obj);
let propName = field.getName();
if (dataType === 'undefined' || dataType === 'symbol') {
ResourceValidator.reportFieldTypeViolation(parameters.rootResourceIdentifier, propName, obj, field);
}
if(field.isTypeEnum()) {
this.checkEnum(obj, field,parameters);
}
else {
if(field.isArray()) {
this.checkArray(obj, field,parameters);
}
else {
this.checkItem(obj, field,parameters);
}
}
return null;
}
/**
* Check a Field that is declared as an Array.
* @param {Object} obj - the object being validated
* @param {Field} field - the object being visited
* @param {Object} parameters - the parameter
* @private
*/
checkEnum(obj,field,parameters) {
if(field.isArray() && !(obj instanceof Array)) {
ResourceValidator.reportFieldTypeViolation(parameters.rootResourceIdentifier, field.getName(), obj, field);
}
const enumDeclaration = field.getParent().getModelFile().getType(field.getType());
if(field.isArray()) {
for(let n=0; n < obj.length; n++) {
const item = obj[n];
parameters.stack.push(item);
enumDeclaration.accept(this, parameters);
}
}
else {
const item = obj;
parameters.stack.push(item);
enumDeclaration.accept(this, parameters);
}
}
/**
* Check a Field that is declared as an Array.
* @param {Object} obj - the object being validated
* @param {Field} field - the object being visited
* @param {Object} parameters - the parameter
* @private
*/
checkArray(obj,field,parameters) {
if(!(obj instanceof Array)) {
ResourceValidator.reportFieldTypeViolation(parameters.rootResourceIdentifier, field.getName(), obj, field);
}
for(let n=0; n < obj.length; n++) {
const item = obj[n];
this.checkItem(item, field, parameters);
}
}
/**
* Check a single (non-array) field.
* @param {Object} obj - the object being validated
* @param {Field} field - the object being visited
* @param {Object} parameters - the parameter
* @private
*/
checkItem(obj,field, parameters) {
let dataType = typeof obj;
let propName = field.getName();
if (dataType === 'undefined' || dataType === 'symbol') {
ResourceValidator.reportFieldTypeViolation(parameters.rootResourceIdentifier, propName, obj, field);
}
if(field.isPrimitive()) {
let invalid = false;
switch(field.getType()) {
case 'String':
if(dataType !== 'string') {
invalid = true;
}
break;
case 'Double':
case 'Long':
case 'Integer':
if(dataType !== 'number') {
invalid = true;
}
break;
case 'Boolean':
if(dataType !== 'boolean') {
invalid = true;
}
break;
case 'DateTime':
if(!(Moment.isMoment(obj))) {
invalid = true;
}
break;
}
if (invalid) {
ResourceValidator.reportFieldTypeViolation(parameters.rootResourceIdentifier, propName, obj, field);
}
else {
if(field.getValidator() !== null) {
field.getValidator().validate(parameters.currentIdentifier, obj);
}
}
}
else {
// a field that points to a transaction, asset, participant...
let classDeclaration = parameters.modelManager.getType(field.getFullyQualifiedTypeName());
if(obj instanceof Identifiable) {
try {
classDeclaration = parameters.modelManager.getType(obj.getFullyQualifiedType());
} catch (err) {
ResourceValidator.reportFieldTypeViolation(parameters.rootResourceIdentifier, propName, obj, field);
}
// is it compatible?
if(!ModelUtil.isAssignableTo(classDeclaration.getModelFile(), classDeclaration.getFullyQualifiedName(), field)) {
ResourceValidator.reportInvalidFieldAssignment(parameters.rootResourceIdentifier, propName, obj, field);
}
}
// recurse
parameters.stack.push(obj);
classDeclaration.accept(this, parameters);
}
}
/**
* Visitor design pattern
* @param {RelationshipDeclaration} relationshipDeclaration - the object being visited
* @param {Object} parameters - the parameter
* @return {Object} the result of visiting or null
* @private
*/
visitRelationshipDeclaration(relationshipDeclaration, parameters) {
const obj = parameters.stack.pop();
if(relationshipDeclaration.isArray()) {
if(!(obj instanceof Array)) {
ResourceValidator.reportInvalidFieldAssignment(parameters.rootResourceIdentifier, relationshipDeclaration.getName(), obj, relationshipDeclaration);
}
for(let n=0; n < obj.length; n++) {
const item = obj[n];
this.checkRelationship(parameters, relationshipDeclaration, item);
}
}
else {
this.checkRelationship(parameters, relationshipDeclaration, obj);
}
return null;
}
/**
* Check a single relationship
* @param {Object} parameters - the parameter
* @param {relationshipDeclaration} relationshipDeclaration - the object being visited
* @param {Object} obj - the object being validated
* @private
*/
checkRelationship(parameters, relationshipDeclaration, obj) {
if(obj instanceof Relationship) {
// All good..
} else if (obj instanceof Resource && (this.options.convertResourcesToRelationships || this.options.permitResourcesForRelationships)) {
// All good.. Again
} else {
ResourceValidator.reportNotRelationshipViolation(parameters.rootResourceIdentifier, relationshipDeclaration, obj);
}
const relationshipType = parameters.modelManager.getType(obj.getFullyQualifiedType());
if(relationshipType.isConcept()) {
throw new Error('Cannot have a relationship to a concept. Relationships must be to resources.');
}
if(!ModelUtil.isAssignableTo(relationshipType.getModelFile(), obj.getFullyQualifiedType(), relationshipDeclaration)) {
ResourceValidator.reportInvalidFieldAssignment(parameters.rootResourceIdentifier, relationshipDeclaration.getName(), obj, relationshipDeclaration);
}
}
/**
* Throw a new error for a model violation.
* @param {string} id - the identifier of this instance.
* @param {string} propName - the name of the field.
* @param {*} value - the value of the field.
* @param {Field} field - the field
* @throws {ValidationException} the exception
* @private
*/
static reportFieldTypeViolation(id, propName, value, field) {
let isArray = field.isArray() ? '[]' : '';
let typeOfValue = typeof value;
if(value instanceof Identifiable) {
typeOfValue = value.getFullyQualifiedType();
value = value.getFullyQualifiedIdentifier();
}
else {
if(value) {
try {
value = JSON.stringify(value);
}
catch(err) {
value = value.toString();
}
}
}
let formatter = Globalize.messageFormatter('resourcevalidator-fieldtypeviolation');
throw new ValidationException(formatter({
resourceId: id,
propertyName: propName,
fieldType: field.getType() + isArray,
value: value,
typeOfValue: typeOfValue
}));
}
/**
* Throw a new error for a model violation.
* @param {string} id - the identifier of this instance.
* @param {classDeclaration} classDeclaration - the declaration of the classs
* @param {Object} value - the value of the field.
* @private
*/
static reportNotResouceViolation(id, classDeclaration, value) {
let formatter = Globalize.messageFormatter('resourcevalidator-notresourceorconcept');
throw new ValidationException(formatter({
resourceId: id,
classFQN: classDeclaration.getFullyQualifiedName(),
invalidValue: value.toString()
}));
}
/**
* Throw a new error for a model violation.
* @param {string} id - the identifier of this instance.
* @param {RelationshipDeclaration} relationshipDeclaration - the declaration of the classs
* @param {Object} value - the value of the field.
* @private
*/
static reportNotRelationshipViolation(id, relationshipDeclaration, value) {
let formatter = Globalize.messageFormatter('resourcevalidator-notrelationship');
throw new ValidationException(formatter({
resourceId: id,
classFQN: relationshipDeclaration.getFullyQualifiedTypeName(),
invalidValue: value.toString()
}));
}
/**
* Throw a new error for a missing, but required field.
* @param {string} id - the identifier of this instance.
* @param {Field} field - the field/
* @private
*/
static reportMissingRequiredProperty(id, field) {
let formatter = Globalize.messageFormatter('resourcevalidator-missingrequiredproperty');
throw new ValidationException(formatter({
resourceId: id,
fieldName: field.getName()
}));
}
/**
* Throw a new error for a missing, but required field.
* @param {string} id - the identifier of this instance.
* @param {Field} field - the field/
* @private
*/
static reportEmptyIdentifier(id) {
let formatter = Globalize.messageFormatter('resourcevalidator-emptyidentifier');
throw new ValidationException(formatter({
resourceId: id
}));
}
/**
* Throw a new error for a missing, but required field.
* @param {string} id - the identifier of this instance.
* @param {Field} field - the field
* @param {string} obj - the object value
* @private
*/
static reportInvalidEnumValue(id, field, obj) {
let formatter = Globalize.messageFormatter('resourcevalidator-invalidenumvalue');
throw new ValidationException(formatter({
resourceId: id,
value: obj,
fieldName: field.getName()
}));
}
/**
* Throw a validation exception for an abstract class
* @param {ClassDeclaration} classDeclaration - the class declaration
* @throws {ValidationException} the validation exception
* @private
*/
static reportAbstractClass(classDeclaration) {
let formatter = Globalize.messageFormatter('resourcevalidator-abstractclass');
throw new ValidationException(formatter({
className: classDeclaration.getFullyQualifiedName(),
}));
}
/**
* Throw a validation exception for an abstract class
* @param {string} resourceId - the id of the resouce being validated
* @param {string} propertyName - the name of the property that is not declared
* @param {string} fullyQualifiedTypeName - the fully qualified type being validated
* @throws {ValidationException} the validation exception
* @private
*/
static reportUndeclaredField(resourceId, propertyName, fullyQualifiedTypeName ) {
let formatter = Globalize.messageFormatter('resourcevalidator-undeclaredfield');
throw new ValidationException(formatter({
resourceId: resourceId,
propertyName: propertyName,
fullyQualifiedTypeName: fullyQualifiedTypeName
}));
}
/**
* Throw a validation exception for an invalid field assignment
* @param {string} resourceId - the id of the resouce being validated
* @param {string} propName - the name of the property that is being assigned
* @param {*} obj - the Field
* @param {Field} field - the Field
* @throws {ValidationException} the validation exception
* @private
*/
static reportInvalidFieldAssignment(resourceId, propName, obj, field) {
let formatter = Globalize.messageFormatter('resourcevalidator-invalidfieldassignment');
let typeName = field.getFullyQualifiedTypeName();
if(field.isArray()) {
typeName += '[]';
}
throw new ValidationException(formatter({
resourceId: resourceId,
propertyName: propName,
objectType: obj.getFullyQualifiedType(),
fieldType: typeName
}));
}
}
module.exports = ResourceValidator;