-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathValidator.cfc
More file actions
187 lines (135 loc) · 5.37 KB
/
Validator.cfc
File metadata and controls
187 lines (135 loc) · 5.37 KB
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
component accessors="true" {
property struct propertyRules;
property struct componentRules;
function init(rulesDirectory, rulesCfcPath, resourceBundle) {
var propertyRules = {};
var componentRules = {};
var messageProvider = new ok.MessageProvider(argumentcollection=arguments);
// load default rules
loadRules(expandPath('/ok/rules'), 'ok.rules', propertyRules, componentRules, messageProvider);
// load custom rules
if (structKeyExists(arguments,'rulesDirectory') && directoryExists(arguments.rulesDirectory)) {
loadRules(arguments.rulesDirectory, arguments.rulesCfcPath, propertyRules, componentRules, messageProvider);
}
setPropertyRules(propertyRules);
setComponentRules(componentRules);
return this;
}
function validate(obj, md, vr, c) {
var propertyRules = getPropertyRules();
var componentRules = getComponentRules();
var objMetaData = (isNull(arguments.md)) ? getMetaData(arguments.obj) : arguments.md;
var validationResult = (isNull(arguments.vr)) ? new ok.ValidationResult() : arguments.vr;
var context = (isNull(arguments.c)) ? objMetaData.name : arguments.c;
// recurse into any parent objects
if( !isNull(objMetaData.extends) ) {
validate(arguments.obj, objMetaData.extends, validationResult, context);
}
// component level validation
for(var attribute in objMetaData) {
if( structKeyExists(componentRules,attribute) ) {
var rule = componentRules[attribute];
if (!rule.isValid(obj, objMetaData, attribute)) {
var msg = rule.getMessage(obj, objMetaData, attribute);
validationResult.addError(
context,
objMetaData.name,
'component',
'',
attribute,
msg);
}
}
}
// property level validation
var props = (isNull(objMetaData.properties)) ? [] : objMetaData.properties;
for(var i=1; i <= ArrayLen(props); i++) {
var prop = props[i];
var attributeList = (structKeyExists(prop,'ok_validate')) ? prop.ok_validate : '';
var fieldType = (structKeyExists(prop,'fieldType')) ? prop.fieldType : 'column';
// columns
if (fieldType == 'column') {
for(var attribute in prop) {
if( structKeyExists(propertyRules,attribute)
&& ( left(attribute,3) == 'ok_' || listFindNoCase(attributeList,attribute) ) ) {
var rule = propertyRules[attribute];
if (!rule.isValid(obj, prop, attribute)) {
var msg = rule.getMessage(obj, prop, attribute);
validationResult.addError(
context,
objMetaData.name,
'property',
prop.name,
attribute,
msg);
}
}
}
}
// relationships
if ( (fieldType == 'one-to-one'
|| fieldType == 'one-to-many'
|| fieldType == 'many-to-one'
|| fieldType == 'many-to-many')
&& (!structKeyExists(prop,'inverse')
|| prop.inverse == false) ) {
for(var attribute in prop) {
if( structKeyExists(propertyRules,attribute)
&& ( left(attribute,3) == 'ok_' || listFindNoCase(attributeList,attribute) ) ) {
var rule = propertyRules[attribute];
if (!rule.isValid(obj, prop, attribute)) {
var msg = rule.getMessage(obj, prop, attribute);
validationResult.addError(
context,
objMetaData.name,
'property',
prop.name,
attribute,
msg);
}
}
// nested validation
if( attribute == 'ok_validate') {
var children = evaluate('obj.get#prop.name#()');
if(!isNull(children)){
if(isArray(children)){
for(var j = 1; j <= arrayLen(children); j++) {
var currentContext = context & '.' & prop.name & '[' & j & ']';
validate(obj=children[j],vr=validationResult,c=currentContext);
}
}
if(isStruct(children)){
var keyType = (structKeyExists(prop,'structkeytype')) ? prop.structkeytype : 'string';
for(var key in children) {
var currentContext = context & '.' & prop.name & '[' & key & ']';
validate(obj=children.get(javaCast(keytype,key)),vr=validationResult,c=currentContext);
}
}
if(isObject(children)){
var currentContext = context & '.' & prop.name;
validate(obj=children,vr=validationResult,c=currentContext);
}
}
}
}
}
}
return validationResult;
}
private function loadRules(path, cfcPath, propertyRules, componentRules, messageProvider) {
var ruleFiles = directoryList(arguments.path,false,'name');
var dotPath = (right(arguments.cfcPath,1) == '.') ? arguments.cfcPath : arguments.cfcPath & '.';
for(var ruleFile in ruleFiles) {
var cfcName = dotPath & getToken(ruleFile,1,'.');
var rule = createObject(cfcName).init(arguments.messageProvider);
for(var attributeName in rule.getAttributeNames()){
if (isInstanceOf(rule,'ok.rules.PropertyRule')) {
arguments.propertyRules[attributeName] = rule;
}
if (isInstanceOf(rule,'ok.rules.ComponentRule')) {
arguments.componentRules[attributeName] = rule;
}
}
}
}
}