UNPKG

5 kBJavaScriptView Raw
1/*
2 * Licensed under the Apache License, Version 2.0 (the "License");
3 * you may not use this file except in compliance with the License.
4 * You may obtain a copy of the License at
5 *
6 * http://www.apache.org/licenses/LICENSE-2.0
7 *
8 * Unless required by applicable law or agreed to in writing, software
9 * distributed under the License is distributed on an "AS IS" BASIS,
10 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 * See the License for the specific language governing permissions and
12 * limitations under the License.
13 */
14
15'use strict';
16
17const TypedStack = require('../serializer/typedstack');
18const Resource = require('./resource');
19
20/**
21 * ValidatedResource is a Resource that can validate that property
22 * changes (or the whole instance) do not violate the structure of
23 * the type information associated with the instance.
24 * @extends Resource
25 * @see See {@link Resource}
26 * @class
27 * @memberof module:concerto-core
28 */
29class ValidatedResource extends Resource {
30 /**
31 * This constructor should not be called directly.
32 * Use the Factory class to create instances.
33 *
34 * <p>
35 * <strong>Note: Only to be called by framework code. Applications should
36 * retrieve instances from {@link Factory}</strong>
37 * </p>
38 * @param {ModelManager} modelManager - The ModelManager for this instance
39 * @param {ClassDeclaration} classDeclaration - The class declaration for this instance.
40 * @param {string} ns - The namespace this instance.
41 * @param {string} type - The type this instance.
42 * @param {string} id - The identifier of this instance.
43 * @param {ResourceValidator} resourceValidator - The validator to use for this instance
44 * @private
45 */
46 constructor(modelManager, classDeclaration, ns, type, id, resourceValidator) {
47 super(modelManager, classDeclaration, ns, type, id);
48 this.$validator = resourceValidator;
49 }
50
51 /**
52 * Sets a property, validating that it does not violate the model
53 * @param {string} propName - the name of the field
54 * @param {string} value - the value of the property
55 * @throws {Error} if the value is not compatible with the model definition for the field
56 */
57 setPropertyValue(propName, value) {
58 let classDeclaration = this.getClassDeclaration();
59 let field = classDeclaration.getProperty(propName);
60
61 if (!field) {
62 throw new Error('The instance with id ' +
63 this.getIdentifier() + ' trying to set field ' +
64 propName + ' which is not declared in the model.');
65 }
66 // else {
67 // this.log( 'Validating field ' + field + ' with data ' + value );
68 // }
69
70 const parameters = {};
71 parameters.stack = new TypedStack(value);
72 parameters.modelManager = this.getModelManager();
73 parameters.rootResourceIdentifier = this.getFullyQualifiedIdentifier();
74 field.accept(this.$validator, parameters);
75 super.setPropertyValue(propName,value);
76 }
77
78 /**
79 * Adds an array property value, validating that it does not violate the model
80 * @param {string} propName - the name of the field
81 * @param {string} value - the value of the property
82 * @throws {Error} if the value is not compatible with the model definition for the field
83 */
84 addArrayValue(propName, value) {
85 let classDeclaration = this.getClassDeclaration();
86 let field = classDeclaration.getProperty(propName);
87
88 if (!field) {
89 throw new Error('The instance with id ' +
90 this.getIdentifier() + ' trying to set field ' +
91 propName + ' which is not declared in the model.');
92 }
93
94 if (!field.isArray()) {
95 throw new Error('The instance with id ' +
96 this.getIdentifier() + ' trying to add array item ' +
97 propName + ' which is not declared as an array in the model.');
98 }
99
100 const parameters = {};
101 let newArray = [];
102 if(this[propName]) {
103 newArray = this[propName].slice(0);
104 }
105 newArray.push(value);
106 parameters.stack = new TypedStack(newArray);
107 parameters.modelManager = this.getModelManager();
108 parameters.rootResourceIdentifier = this.getFullyQualifiedIdentifier();
109 field.accept(this.$validator, parameters);
110 super.addArrayValue(propName, value);
111 }
112
113 /**
114 * Validates the instance against its model.
115 *
116 * @throws {Error} - if the instance if invalid with respect to the model
117 */
118 validate() {
119 const classDeclaration = this.getClassDeclaration();
120 const parameters = {};
121 parameters.stack = new TypedStack(this);
122 parameters.modelManager = this.getModelManager();
123 parameters.rootResourceIdentifier = this.getFullyQualifiedIdentifier();
124 classDeclaration.accept(this.$validator, parameters);
125 }
126}
127
128module.exports = ValidatedResource;