Skip to content

Commit cb47726

Browse files
committed
Renamed F.onValidation to F.onValidate() + added onParseJSON, onParseXML and onParseQuery.
1 parent 82bc5ee commit cb47726

5 files changed

Lines changed: 95 additions & 38 deletions

File tree

builders.js

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,20 @@ function SchemaBuilderEntity(parent, name, obj, validator, properties) {
136136
this.constants;
137137
this.onPrepare;
138138
this.onDefault;
139-
this.onValidation = validator ? validator : framework.onValidation;
139+
this.onValidate = validator ? validator : framework.onValidate;
140140
this.onSave;
141141
this.onGet;
142142
this.onRemove;
143143
this.onQuery;
144144
this.onError;
145145
this.gcache = {};
146+
147+
var self = this;
148+
149+
setTimeout(function() {
150+
if (self.onValidation)
151+
OBSOLETE('SchemaBuilderEntity.onValidation()', 'Instead of "SchemaBuilderEntity.onValidation()" use "SchemaBuilderEntity.setValidate()"');
152+
}, 2000)
146153
}
147154

148155
/**
@@ -416,7 +423,7 @@ SchemaBuilderEntity.prototype.getDependencies = function() {
416423
* @param {Function(propertyName, value, path, entityName, model)} fn A validation function.
417424
* @return {SchemaBuilderEntity}
418425
*/
419-
SchemaBuilderEntity.prototype.setValidation = function(properties, fn) {
426+
SchemaBuilderEntity.prototype.setValidate = function(properties, fn) {
420427
var self = this;
421428

422429
if (fn === undefined && properties instanceof Array) {
@@ -426,15 +433,16 @@ SchemaBuilderEntity.prototype.setValidation = function(properties, fn) {
426433

427434
if (typeof(properties) !== FUNCTION) {
428435
self.properties = properties;
429-
self.onValidation = fn;
436+
self.onValidate = fn;
430437
} else
431-
self.onValidation = properties;
438+
self.onValidate = properties;
432439

433440
return self;
434441
};
435442

436-
SchemaBuilderEntity.prototype.setValidate = function(properties, fn) {
437-
return this.setValidation(properties, fn);
443+
SchemaBuilderEntity.prototype.setValidation = function(properties, fn) {
444+
OBSOLETE('SchemaBuilderEntity.setValidation()', 'Instead of "SchemaBuilderEntity.setValidation()" use "SchemaBuilderEntity.setValidate()"');
445+
return this.setValidate(properties, fn);
438446
};
439447

440448
SchemaBuilderEntity.prototype.setPrefix = function(prefix) {
@@ -707,6 +715,7 @@ SchemaBuilderEntity.prototype.destroy = function() {
707715
self.properties = null;
708716
self.schema = null;
709717
self.onDefault = null;
718+
self.onValidate = null;
710719
self.onValidation = null;
711720
self.onSave = null;
712721
self.onRead = null;
@@ -1003,7 +1012,7 @@ SchemaBuilderEntity.prototype.query = function(helper, callback) {
10031012
SchemaBuilderEntity.prototype.validate = function(model, resourcePrefix, resourceName, builder, filter) {
10041013

10051014
var self = this;
1006-
var fn = self.onValidation;
1015+
var fn = self.onValidate || self.onValidation;
10071016

10081017
if (builder === undefined) {
10091018
builder = new ErrorBuilder();
@@ -1014,7 +1023,7 @@ SchemaBuilderEntity.prototype.validate = function(model, resourcePrefix, resourc
10141023
}
10151024

10161025
if (fn === undefined || fn === null) {
1017-
fn = framework.onValidation;
1026+
fn = framework.onValidate || framework.onValidation;
10181027
if (fn === undefined || fn === null)
10191028
return builder;
10201029
}
@@ -1375,7 +1384,7 @@ SchemaBuilderEntity.prototype.make = SchemaBuilderEntity.prototype.load = functi
13751384

13761385
var output = self.prepare(model);
13771386

1378-
if (self.onValidation === undefined) {
1387+
if (!self.onValidate && !self.onValidation) {
13791388
if (callback)
13801389
callback(null, output);
13811390
return output;
@@ -2348,7 +2357,7 @@ exports.validation = function(name, properties, fn) {
23482357

23492358
if (typeof(fn) === FUNCTION) {
23502359

2351-
schema.onValidation = fn;
2360+
schema.onValidate = fn;
23522361

23532362
if (properties === undefined)
23542363
schema.properties = Object.keys(schema.schema);
@@ -2365,7 +2374,7 @@ exports.validation = function(name, properties, fn) {
23652374
return validator || [];
23662375
}
23672376

2368-
schema.onValidation = fn;
2377+
schema.onValidate = fn;
23692378
return fn;
23702379
};
23712380

changes.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
- added: (IMPORTANT) merging supports BLOCKS (.js,.css), e.g. F.merge('merge.js', 'fileA.js#management,common', 'fileB.js#management')
44
- added: (IMPORTANT) a route with schema binding can contain filter e.g. `*Schema#update` or `*Group\Schema#create` --> the framework validates only fields by filter
5-
- added: MAKE([transform], function(obj))
65
- added: TRANSFORM([transform], obj)
76
- added: NEWTRANSFORM(name, fn, [isDefault]) --> alias for TransformBuilder.addTransform()
87
- added: packages can be stored as directories (recommended for debug mode only)
@@ -21,7 +20,13 @@
2120
- added: @{meta} can be imported as @{import('meta')}
2221
- added: controller.cookie('KEY') --> for reading
2322
- added: controller.cookie('KEY', 'VALUE', expire, [options]) --> for writting
23+
- added: framework.onParseQuery(function(value)) --> for parsing values from the requests
24+
- added: framework.onParseXML(function(value)) --> for parsing values from the requests
25+
- added: framework.onParseJSON(function(value)) --> for parsing values from the requests
2426

27+
- updated: (IMPORTANT) F.onValidation() was renamed to F.onValidate()
28+
- updated: (IMPORTANT) SchemaBuilderEntity.onValidation() was renamed to SchemaBuilderEntity.onValidate()
29+
- updated: (IMPORTANT) SchemaBuilderEntity.setValidation() was renamed to SchemaBuilderEntity.setValidate()
2530
- updated: CSS compressor removes comments
2631
- updated: F.restrictions.allow('IP') --> does not have to be full IP
2732
- updated: F.restrictions.disallow('IP') --> does not have to be full IP

index.js

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,10 @@ global.TRY = function(fn, err) {
271271
}
272272
};
273273

274+
global.OBSOLETE = function(name, message) {
275+
console.log(':: OBSOLETE / IMPORTANT ---> "' + name + '"', message);
276+
};
277+
274278
if (global.setImmediate === undefined) {
275279
global.setImmediate = function(cb) {
276280
process.nextTick(cb);
@@ -1825,7 +1829,7 @@ Framework.prototype.file = function(name, fnValidation, fnExecute, middleware, o
18251829
self.routes.files.push({
18261830
controller: !_controller ? 'unknown' : _controller,
18271831
name: name,
1828-
onValidation: fnValidation,
1832+
onValidate: fnValidation,
18291833
execute: fnExecute || fnValidation,
18301834
middleware: middleware,
18311835
options: options
@@ -2589,7 +2593,7 @@ Framework.prototype.install = function(type, name, declaration, options, callbac
25892593

25902594
if (typeof(obj.install) === TYPE_FUNCTION) {
25912595
if (framework.config['allow-compatibility'] || obj.install.toString().indexOf('function (framework') === 0) {
2592-
console.log('OBSOLETE ' + key + ': exports.install = function(framework <-- REMOVE ARGUMENT, options, name) { ...');
2596+
OBSOLETE(key, 'exports.install = function(framework <--- REMOVE THE ARGUMENT framework');
25932597
obj.install(self, options, name);
25942598
} else
25952599
obj.install(options, name);
@@ -2802,7 +2806,7 @@ Framework.prototype.install_make = function(key, name, obj, options, callback, s
28022806

28032807
if (typeof(obj.install) === TYPE_FUNCTION) {
28042808
if (framework.config['allow-compatibility'] || obj.install.toString().indexOf('function (framework') === 0) {
2805-
console.log('OBSOLETE ' + key + ': exports.install = function(framework <-- REMOVE ARGUMENT, options, name) { ...');
2809+
OBSOLETE(key, 'exports.install = function(framework <--- REMOVE THE ARGUMENT framework');
28062810
obj.install(self, options, name);
28072811
}
28082812
else
@@ -3087,6 +3091,40 @@ Framework.prototype.snapshot = function(url, filename, callback) {
30873091
*/
30883092
Framework.prototype.onValidation = null;
30893093

3094+
/**
3095+
* Global validation
3096+
* @param {Function(name, value)} delegate
3097+
* @type {Boolean or StringErrorMessage}
3098+
*/
3099+
Framework.prototype.onValidate = null;
3100+
3101+
/**
3102+
* Global XML parsing
3103+
* @param {String} value
3104+
* @return {Object}
3105+
*/
3106+
Framework.prototype.onParseXML = function(value) {
3107+
return framework_utils.parseXML(value);
3108+
};
3109+
3110+
/**
3111+
* Global JSON parsing
3112+
* @param {String} value
3113+
* @return {Object}
3114+
*/
3115+
Framework.prototype.onParseJSON = function(value) {
3116+
return JSON.parse(value);
3117+
};
3118+
3119+
/**
3120+
* Global JSON parsing
3121+
* @param {String} value
3122+
* @return {Object}
3123+
*/
3124+
Framework.prototype.onParseQuery = function(value) {
3125+
return qs.parse(value);
3126+
};
3127+
30903128
/**
30913129
* Schema parser delegate
30923130
* @param {Request} req
@@ -5179,6 +5217,11 @@ Framework.prototype.load = function(debug, types, path) {
51795217
delete framework.assert;
51805218
}, 500);
51815219

5220+
setTimeout(function() {
5221+
if (self.onValidation)
5222+
OBSOLETE('framework.onValidation()', 'The function was renamed to "framework.onValidate()');
5223+
}, 2000);
5224+
51825225
self.$load(types, directory);
51835226
return self;
51845227
};
@@ -6209,7 +6252,7 @@ Framework.prototype.mail = function(address, subject, view, model, callback, lan
62096252
if (language.indexOf('@') !== -1) {
62106253
replyTo = language;
62116254
language = undefined;
6212-
console.log('OBSOLETE: F.mail(..., ..., [replyTo] --> has been replaced for [language]).');
6255+
OBSOLETE('F.mail', 'F.mail(..., ..., [replyTo] --> the argument was replaced for [language])');
62136256
} else
62146257
controller.language = language;
62156258
}
@@ -7371,12 +7414,12 @@ Framework.prototype._configure = function(arr, rewrite) {
73717414
break;
73727415

73737416
case 'allow-compile-js':
7374-
console.log('CONFIG: allow-compile-js is obsolete, use: allow-compile-script');
7417+
OBSOLETE('config', 'Instead of "allow-compile-js" use "allow-compile-script"');
73757418
obj['allow-compile-script'] = value.toLowerCase() === 'true' || value === '1' || value === 'on';
73767419
break;
73777420

73787421
case 'allow-compile-css':
7379-
console.log('CONFIG: allow-compile-css is obsolete, use: allow-compile-style');
7422+
OBSOLETE('config', 'Instead of "allow-compile-css" use "allow-compile-style"');
73807423
obj['allow-compile-style'] = value.toLowerCase() === 'true' || value === '1' || value === 'on';
73817424
break;
73827425

@@ -7400,12 +7443,12 @@ Framework.prototype._configure = function(arr, rewrite) {
74007443
break;
74017444

74027445
case 'static-url-css':
7403-
console.log('OBSOLETE "config.static-url-css": use "config.static-url-style"');
7446+
OBSOLETE('config', 'Instead of "static-url-css" use "static-url-style"');
74047447
obj['static-url-style'] = value;
74057448
break;
74067449

74077450
case 'static-url-js':
7408-
console.log('OBSOLETE "config.static-url-js": use "config.static-url-script"');
7451+
OBSOLETE('config', 'Instead of "static-url-js" use "static-url-script"');
74097452
obj['static-url-script'] = value;
74107453
break;
74117454

@@ -7447,7 +7490,7 @@ Framework.prototype._configure = function(arr, rewrite) {
74477490
* @return {String}
74487491
*/
74497492
Framework.prototype.routeJS = function(name) {
7450-
console.log('OBSOLETE framework.routeJS(): use framework.routeScript()');
7493+
OBSOLETE('framework.routeJS()', 'Instead of "framework.routeJS()" use "framework.routeScript()"');
74517494
return this.routeScript(name);
74527495
};
74537496

@@ -7472,7 +7515,7 @@ Framework.prototype.routeScript = function(name) {
74727515
* @return {String}
74737516
*/
74747517
Framework.prototype.routeCSS = function(name) {
7475-
console.log('OBSOLETE framework.routeCSS(): use framework.routeStyle()');
7518+
OBSOLETE('framework.routeCSS()', 'Instead of "framework.routeCSS()" use "framework.routeStyle()"');
74767519
return this.routeStyle(name);
74777520
};
74787521

@@ -9141,7 +9184,7 @@ Subscribe.prototype.doEnd = function() {
91419184
}
91429185

91439186
try {
9144-
req.body = utils.parseXML(req.buffer_data.trim());
9187+
req.body = framework.onParseXML(req.buffer_data.trim());
91459188
req.buffer_data = null;
91469189
self.prepare(req.flags, req.uri.pathname);
91479190
} catch (err) {
@@ -9164,13 +9207,13 @@ Subscribe.prototype.doEnd = function() {
91649207

91659208
if (req.$type === 1) {
91669209
try {
9167-
req.body = JSON.parse(req.buffer_data);
9210+
req.body = framework.onParseJSON(req.buffer_data);
91689211
} catch (e) {
91699212
self.route400(new Error('Not valid JSON data.'));
91709213
return self;
91719214
}
91729215
} else
9173-
req.body = qs.parse(req.buffer_data);
9216+
req.body = framework.onParseQuery(req.buffer_data);
91749217

91759218
if (!route.schema) {
91769219
self.prepare(req.flags, req.uri.pathname);
@@ -9222,7 +9265,7 @@ Subscribe.prototype.doEndfile = function() {
92229265
var file = framework.routes.files[i];
92239266
try {
92249267

9225-
if (file.onValidation.call(framework, req, res, true)) {
9268+
if (file.onValidate.call(framework, req, res, true)) {
92269269

92279270
if (file.middleware === null)
92289271
file.execute.call(framework, req, res, false);
@@ -9736,7 +9779,7 @@ Controller.prototype.validate = function(model, properties, prefix, name) {
97369779
return builders.validate(properties, model, prefix);
97379780

97389781
var error = new builders.ErrorBuilder(resource);
9739-
return utils.validate.call(self, model, properties, framework.onValidation, error);
9782+
return utils.validate.call(self, model, properties, framework.onValidate || framework.onValidation, error);
97409783
};
97419784

97429785
/*
@@ -10222,7 +10265,7 @@ Controller.prototype.sitemap = function(name, url, index) {
1022210265
if (!url)
1022310266
return self.repository[REPOSITORY_SITEMAP];
1022410267

10225-
console.log('OBSOLETE sitemap: The newest version supports new sitemap mechanism.');
10268+
OBSOLETE('sitemap', 'The newest version supports new sitemap mechanism.');
1022610269

1022710270
if (self.repository.sitemap === undefined)
1022810271
self.repository.sitemap = [];
@@ -11223,7 +11266,7 @@ Controller.prototype._routeHelper = function(current, name, fn) {
1122311266
* @return {String}
1122411267
*/
1122511268
Controller.prototype.routeJS = function(name, tag) {
11226-
console.log('OBSOLETE controller.routeJS(): use controller.routeScript()');
11269+
OBSOLETE('controller.routeJS()', 'Instead of "controller.routeJS()" use "controller.routeScript()"');
1122711270
return this.routeScript(name, tag);
1122811271
};
1122911272

@@ -11248,7 +11291,7 @@ Controller.prototype.routeScript = function(name, tag) {
1124811291
* @return {String}
1124911292
*/
1125011293
Controller.prototype.routeCSS = function(name, tag) {
11251-
console.log('OBSOLETE controller.routeCSS(): use controller.routeStyle()');
11294+
OBSOLETE('controller.routeCSS()', 'Instead of "controller.routeCSS()" use "controller.routeStyle()"');
1125211295
return this.routeStyle(name, tag);
1125311296
};
1125411297

@@ -12276,7 +12319,7 @@ Controller.prototype.proxy = function(url, obj, fnCallback, timeout) {
1227612319
return;
1227712320

1227812321
if ((headers['content-type'] || '').indexOf('application/json') !== -1)
12279-
data = JSON.parse(data);
12322+
data = framework.onParseJSON(data);
1228012323

1228112324
fnCallback.call(self, error, data, code, headers);
1228212325

@@ -12930,7 +12973,7 @@ WebSocket.prototype.proxy = function(url, obj, fnCallback) {
1293012973
return;
1293112974

1293212975
if ((headers['content-type'] || '').indexOf('application/json') !== -1)
12933-
data = JSON.parse(data);
12976+
data = framework.onParseJSON(data);
1293412977

1293512978
fnCallback.call(self, error, data, code, headers);
1293612979

@@ -13103,7 +13146,7 @@ WebSocket.prototype.validate = function(model, properties, prefix, name) {
1310313146
};
1310413147

1310513148
var error = new builders.ErrorBuilder(resource);
13106-
return utils.validate.call(self, model, properties, framework.onValidation, error);
13149+
return utils.validate.call(self, model, properties, framework.onValidate || framework.onValidation, error);
1310713150
};
1310813151

1310913152
/*
@@ -13362,7 +13405,7 @@ WebSocketClient.prototype.parse = function() {
1336213405
// JSON
1336313406
if (self.type === 3) {
1336413407
try {
13365-
self.container.emit('message', self, JSON.parse(self.container.config['default-websocket-encodedecode'] === true ? $decodeURIComponent(output) : output));
13408+
self.container.emit('message', self, framework.onParseJSON(self.container.config['default-websocket-encodedecode'] === true ? $decodeURIComponent(output) : output));
1336613409
} catch (ex) {
1336713410
self.errors++;
1336813411
self.container.emit('error', new Error('JSON parser: ' + ex.toString()), self);
@@ -14063,7 +14106,7 @@ http.IncomingMessage.prototype = {
1406314106
var self = this;
1406414107
if (self._dataGET)
1406514108
return self._dataGET;
14066-
self._dataGET = qs.parse(self.uri.query);
14109+
self._dataGET = framework.onParseQuery(self.uri.query);
1406714110
return self._dataGET;
1406814111
},
1406914112

0 commit comments

Comments
 (0)