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) {
switch (val.validation) {
case 'custom':
// TODO: Reemplazar eval
val.customValidator = eval(`(${val.customValidator})`);
break;
case 'format':
val.with = new RegExp(val.with);
break;
case 'custom':
// TODO: Reemplazar eval
val.bindedFunction = eval(`(${val.bindedFunction})`);
break;
case 'format':
val.with = new RegExp(val.with);
break;
}
}
onValidationsReady(json, resolve) {

View File

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

View File

@ -21,7 +21,7 @@ module.exports = function(self) {
count: true
};
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 => {
return serverFields.some(itemS => itemS === itemC);
});
var and = [];
(clientFilter && clientFilter.where) && and.push(clientFilter.where);
(serverFilter && serverFilter.where) && and.push(serverFilter.where);
var order;
var limit;
if (clientFilter && clientFilter.order)
order = clientFilter.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) {
if (Array.isArray(o)) {

View File

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

View File

@ -21,7 +21,7 @@ module.exports = function(self) {
count: true
};
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 => {
return serverFields.some(itemS => itemS === itemC);
});
var and = [];
(clientFilter && clientFilter.where) && and.push(clientFilter.where);
(serverFilter && serverFilter.where) && and.push(serverFilter.where);
var order;
var limit;
if (clientFilter && clientFilter.order)
order = clientFilter.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) {
if (Array.isArray(o)) {

View File

@ -21,7 +21,7 @@ module.exports = function(self) {
count: true
};
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 => {
return serverFields.some(itemS => itemS === itemC);
});
var and = [];
(clientFilter && clientFilter.where) && and.push(clientFilter.where);
(serverFilter && serverFilter.where) && and.push(serverFilter.where);
var order;
var limit;
if (clientFilter && clientFilter.order)
order = clientFilter.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) {
if (Array.isArray(o)) {