validaciones custom se bajan al cliente solo si son isExportable

This commit is contained in:
Javi Gallego 2017-09-25 17:39:51 +02:00
parent a5df4afc0f
commit 9b76d3d004
6 changed files with 105 additions and 84 deletions

View File

@ -46,13 +46,13 @@ export function factory($translatePartialLoader, $http, $window, $ocLazyLoad, $q
} }
parseValidation(val) { parseValidation(val) {
switch (val.validation) { switch (val.validation) {
case 'custom': case 'custom':
// TODO: Reemplazar eval // TODO: Reemplazar eval
val.customValidator = eval(`(${val.customValidator})`); val.bindedFunction = eval(`(${val.bindedFunction})`);
break; break;
case 'format': case 'format':
val.with = new RegExp(val.with); val.with = new RegExp(val.with);
break; break;
} }
} }
onValidationsReady(json, resolve) { onValidationsReady(json, resolve) {

View File

@ -46,15 +46,7 @@ export const validators = {
throw new Error(`Invalid value`); throw new Error(`Invalid value`);
}, },
custom: function(value, conf) { custom: function(value, conf) {
let valid = true; if (!conf.bindedFunction(value))
function err(kind) {
valid = false;
}
let inst = {attr: value};
conf.customValidator.call(inst, err);
if (!valid)
throw new Error(`Invalid value`); throw new Error(`Invalid value`);
} }
}; };

View File

@ -21,7 +21,7 @@ module.exports = function(self) {
count: true count: true
}; };
for (let method in disableMethods) { for (let method in disableMethods) {
//this.disableRemoteMethod(method, disableMethods[method]); // this.disableRemoteMethod(method, disableMethods[method]);
} }
}; };
@ -50,14 +50,14 @@ module.exports = function(self) {
var fields = clientFields.filter(itemC => { var fields = clientFields.filter(itemC => {
return serverFields.some(itemS => itemS === itemC); return serverFields.some(itemS => itemS === itemC);
}); });
var and = []; var and = [];
(clientFilter && clientFilter.where) && and.push(clientFilter.where); (clientFilter && clientFilter.where) && and.push(clientFilter.where);
(serverFilter && serverFilter.where) && and.push(serverFilter.where); (serverFilter && serverFilter.where) && and.push(serverFilter.where);
var order; var order;
var limit; var limit;
if (clientFilter && clientFilter.order) if (clientFilter && clientFilter.order)
order = clientFilter.order; order = clientFilter.order;
else if (serverFilter && serverFilter.order) else if (serverFilter && serverFilter.order)
@ -179,6 +179,15 @@ module.exports = function(self) {
}); });
}; };
}; };
self.validateBinded = function(propertyName, validatorFn, options) {
var customValidator = function(err) {
if (!validatorFn(this[propertyName])) err();
};
options.isExportable = true;
options.bindedFunction = validatorFn;
this.validate(propertyName, customValidator, options);
};
}; };
function removeEmpty(o) { function removeEmpty(o) {
if (Array.isArray(o)) { if (Array.isArray(o)) {

View File

@ -1,56 +1,58 @@
module.exports = function (server) { module.exports = function (server) {
function toJson(object) { function toJson(object) {
let json = {}; let json = {};
for (let prop in object) { for (let prop in object) {
let value = object[prop]; let value = object[prop];
switch (typeof value) { switch (typeof value) {
case 'object': case 'object':
if(value instanceof RegExp) if(value instanceof RegExp)
json[prop] = value.source; json[prop] = value.source;
break; break;
case 'function': case 'function':
json[prop] = value.toString(); json[prop] = value.toString();
break; break;
default: default:
json[prop] = value; json[prop] = value;
} }
} }
return json; return json;
} }
server.get('/validations', function (req, res) { server.get('/validations', function (req, res) {
let json = {}; let json = {};
let models = server.models; let models = server.models;
for (let modelName in models) { for (let modelName in models) {
let model = models[modelName]; let model = models[modelName];
let validations = model.validations; let validations = model.validations;
let jsonValidations = {}; let jsonValidations = {};
for (let fieldName in validations) { for (let fieldName in validations) {
let jsonField = []; let jsonField = [];
for (let validation of validations[fieldName]) { for (let validation of validations[fieldName]) {
let options = validation.options; let options = validation.options;
if (options && options.async)
continue; if ((options && options.async) ||
(validation.validation == 'custom' && !validation.isExportable))
jsonField.push(toJson(validation)); continue;
}
jsonField.push(toJson(validation));
jsonValidations[fieldName] = jsonField; }
}
jsonValidations[fieldName] = jsonField;
json[modelName] = { }
properties: model.definition.rawProperties,
validations: jsonValidations json[modelName] = {
}; properties: model.definition.rawProperties,
} validations: jsonValidations
};
res.set('Content-Type', 'application/json'); }
res.send(JSON.stringify(json));
}); res.set('Content-Type', 'application/json');
}; res.send(JSON.stringify(json));
});
};

View File

@ -21,7 +21,7 @@ module.exports = function(self) {
count: true count: true
}; };
for (let method in disableMethods) { for (let method in disableMethods) {
//this.disableRemoteMethod(method, disableMethods[method]); // this.disableRemoteMethod(method, disableMethods[method]);
} }
}; };
@ -50,14 +50,14 @@ module.exports = function(self) {
var fields = clientFields.filter(itemC => { var fields = clientFields.filter(itemC => {
return serverFields.some(itemS => itemS === itemC); return serverFields.some(itemS => itemS === itemC);
}); });
var and = []; var and = [];
(clientFilter && clientFilter.where) && and.push(clientFilter.where); (clientFilter && clientFilter.where) && and.push(clientFilter.where);
(serverFilter && serverFilter.where) && and.push(serverFilter.where); (serverFilter && serverFilter.where) && and.push(serverFilter.where);
var order; var order;
var limit; var limit;
if (clientFilter && clientFilter.order) if (clientFilter && clientFilter.order)
order = clientFilter.order; order = clientFilter.order;
else if (serverFilter && serverFilter.order) else if (serverFilter && serverFilter.order)
@ -179,6 +179,15 @@ module.exports = function(self) {
}); });
}; };
}; };
self.validateBinded = function(propertyName, validatorFn, options) {
var customValidator = function(err) {
if (!validatorFn(this[propertyName])) err();
};
options.isExportable = true;
options.bindedFunction = validatorFn;
this.validate(propertyName, customValidator, options);
};
}; };
function removeEmpty(o) { function removeEmpty(o) {
if (Array.isArray(o)) { if (Array.isArray(o)) {

View File

@ -21,7 +21,7 @@ module.exports = function(self) {
count: true count: true
}; };
for (let method in disableMethods) { for (let method in disableMethods) {
//this.disableRemoteMethod(method, disableMethods[method]); // this.disableRemoteMethod(method, disableMethods[method]);
} }
}; };
@ -50,14 +50,14 @@ module.exports = function(self) {
var fields = clientFields.filter(itemC => { var fields = clientFields.filter(itemC => {
return serverFields.some(itemS => itemS === itemC); return serverFields.some(itemS => itemS === itemC);
}); });
var and = []; var and = [];
(clientFilter && clientFilter.where) && and.push(clientFilter.where); (clientFilter && clientFilter.where) && and.push(clientFilter.where);
(serverFilter && serverFilter.where) && and.push(serverFilter.where); (serverFilter && serverFilter.where) && and.push(serverFilter.where);
var order; var order;
var limit; var limit;
if (clientFilter && clientFilter.order) if (clientFilter && clientFilter.order)
order = clientFilter.order; order = clientFilter.order;
else if (serverFilter && serverFilter.order) else if (serverFilter && serverFilter.order)
@ -179,6 +179,15 @@ module.exports = function(self) {
}); });
}; };
}; };
self.validateBinded = function(propertyName, validatorFn, options) {
var customValidator = function(err) {
if (!validatorFn(this[propertyName])) err();
};
options.isExportable = true;
options.bindedFunction = validatorFn;
this.validate(propertyName, customValidator, options);
};
}; };
function removeEmpty(o) { function removeEmpty(o) {
if (Array.isArray(o)) { if (Array.isArray(o)) {