salix/loopback/server/boot/validations.js

64 lines
1.8 KiB
JavaScript

module.exports = function(app) {
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;
}
app.get('/validations', function(req, res) {
let json = {};
let models = app.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;
let validationCp = Object.assign({}, validation);
if (validationCp.message)
validationCp.message = req.__(validationCp.message);
jsonField.push(toJson(validationCp));
}
jsonValidations[fieldName] = jsonField;
}
json[modelName] = {
properties: model.definition.rawProperties,
validations: jsonValidations
};
}
res.set('Content-Type', 'application/json');
res.send(JSON.stringify(json));
});
};