salix/services/loopback/server/boot/validations.js

59 lines
1.6 KiB
JavaScript
Raw Normal View History

2017-10-13 14:54:07 +00:00
module.exports = function(app) {
2017-10-13 14:22:45 +00:00
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;
}
2017-10-13 14:54:07 +00:00
app.get('/validations', function(req, res) {
2017-10-13 14:22:45 +00:00
let json = {};
2017-10-13 14:54:07 +00:00
let models = app.models;
2017-10-13 14:22:45 +00:00
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));
});
};