/
expression-operand-type-validator.js
157 lines (148 loc) · 6.78 KB
/
expression-operand-type-validator.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
import __ from 'orotranslation/js/translator';
import ASTNodeWrapper from 'oroexpressionlanguage/js/ast-node-wrapper';
import {ConditionalNode, GetAttrNode, NameNode} from 'oroexpressionlanguage/js/expression-language-library';
/**
* @typedef {Object} EntityInfo
* @property {boolean} isCollection - says if entity has to be used like collection
* @property {string} name - alias or className of entity
* @property {EntityTreeNode} fields - node of entityTree that contains its fields
*/
/**
* @typedef {Object} OperationInfo
* @property {string} item - an operator (e.g. '+', '*')
* @property {string} type - type of operator (e.g. 'math', 'equal')
*/
class ExpressionOperandTypeValidator {
/**
* @param {Object} options
* @param {Array.<EntityInfo>?} options.entities - array with objects that contain information about root entities
* @param {Object.<string, OperationInfo>?} options.operations - keys are strings of operators (e.g. '+', '*')
* @param {number=} options.itemLevelLimit - says how deep in entityTree can be used a field
* @param {boolean=} options.isConditionalNodeAllowed - when false an exeption is thrown if conditional node is used
*/
constructor({entities = null, operations = null, itemLevelLimit = 3, isConditionalNodeAllowed = true}) {
Object.assign(this, {entities, itemLevelLimit, operations, isConditionalNodeAllowed});
}
/**
* Tests a parsed expression.
*
* @param {ParsedExpression} parsedExpression - instance of ParsedExpression that created by ExpressionLanguage
* @throws {TypeError} if the expression contains wrong entity fields or operations
*/
expectValid(parsedExpression) {
const astNodeWrapper = new ASTNodeWrapper(parsedExpression.getNodes());
if (!this.isConditionalNodeAllowed) {
this._expectWithoutConditionals(astNodeWrapper);
}
if (this.operations) {
this._expectAllowedOperators(astNodeWrapper);
}
if (this.entities) {
this._expectValidPropertyPath(astNodeWrapper);
}
}
/**
* @param {ASTNodeWrapper} astNodeWrapper - instance of ASTNodeWrapper that wraps nodes created by
* ExpressionLanguage
* @throws {TypeError} if node tree contains conditionals nodes
* @protected
*/
_expectWithoutConditionals(astNodeWrapper) {
if (astNodeWrapper.findInstancesOf(ConditionalNode).length !== 0) {
throw new TypeError(__('oro.form.expression_editor.validation.conditional'));
}
}
/**
* @param {ASTNodeWrapper} astNodeWrapper - instance of ASTNodeWrapper that wraps nodes created by
* ExpressionLanguage
* @throws {TypeError} if node tree contains forbidden operators
* @protected
*/
_expectAllowedOperators(astNodeWrapper) {
const forbiddenOperatorNodes = astNodeWrapper.findAll(node => {
const operator = node.attr('operator');
return operator !== void 0 && !(operator in this.operations);
});
if (forbiddenOperatorNodes.length !== 0) {
let message;
const operators = forbiddenOperatorNodes
.map(node => node.attr('operator'))
.filter((val, i, arr) => arr.indexOf(val) === i);
if (operators.length === 1) {
message = __('oro.form.expression_editor.validation.forbidden_operator', {
operator: forbiddenOperatorNodes[0].attr('operator')
});
} else {
message = __('oro.form.expression_editor.validation.forbidden_operator', {
operators: operators.join('`, `')
});
}
throw new TypeError(message);
}
}
/**
* @param {ASTNodeWrapper} astNodeWrapper - instance of ASTNodeWrapper that wraps nodes created by
* ExpressionLanguage
* @throws {TypeError} if node tree contains invalid property paths
* @protected
*/
_expectValidPropertyPath(astNodeWrapper) {
this.entities.forEach(entity => {
const nameNodes = astNodeWrapper
.findAll(node => node.instanceOf(NameNode) && node.attr('name') === entity.name);
nameNodes.forEach(node => {
if (node.parent === null || !node.parent.instanceOf(GetAttrNode)) {
throw new TypeError(__('oro.form.expression_editor.validation.property_path.like_variable', {
name: entity.name
}));
}
let source = entity.name;
if (entity.isCollection) {
if (node.parent.attr('type') !== GetAttrNode.ARRAY_CALL) {
throw new TypeError(
__('oro.form.expression_editor.validation.property_path.like_single_entity', {
name: entity.name
})
);
}
source += '[' + node.parent.child(1).attr('value') + ']';
node = node.parent;
} else if (node.parent.attr('type') !== GetAttrNode.PROPERTY_CALL) {
throw new TypeError(__('oro.form.expression_editor.validation.property_path.like_collection', {
name: entity.name
}));
}
node = node.parent;
let fieldName;
let level = 1;
let field = entity.fields;
while (node && node.instanceOf(GetAttrNode)) {
level++;
fieldName = node.child(1).attr('value');
field = field[fieldName];
if (!field) {
throw new TypeError(
__('oro.form.expression_editor.validation.property_path.field_not_present', {
fieldName,
source
})
);
} else if (level > this.itemLevelLimit) {
throw new TypeError(__('oro.form.expression_editor.validation.property_path.level_limit', {
fieldName,
source
}));
}
node = node.parent;
source += '.' + fieldName;
}
if (!field.__isField) {
throw new TypeError(__('oro.form.expression_editor.validation.property_path.not_field', {
source
}));
}
});
});
}
}
export default ExpressionOperandTypeValidator;