2017-03-14 12:08:55 +00:00
|
|
|
module.exports = function (server) {
|
2017-01-31 13:13:06 +00:00
|
|
|
function toJson(object) {
|
|
|
|
let json = {};
|
|
|
|
|
2017-06-01 16:23:41 +00:00
|
|
|
for (let prop in object) {
|
2017-01-31 13:13:06 +00:00
|
|
|
let value = object[prop];
|
|
|
|
|
2017-06-01 16:23:41 +00:00
|
|
|
switch (typeof value) {
|
2017-01-31 13:13:06 +00:00
|
|
|
case 'object':
|
|
|
|
if(value instanceof RegExp)
|
2017-02-01 17:55:02 +00:00
|
|
|
json[prop] = value.source;
|
2017-01-31 13:13:06 +00:00
|
|
|
break;
|
|
|
|
case 'function':
|
|
|
|
json[prop] = value.toString();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
json[prop] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2017-03-14 12:08:55 +00:00
|
|
|
server.get('/validations', function (req, res) {
|
2017-01-31 13:13:06 +00:00
|
|
|
let json = {};
|
2017-03-14 12:08:55 +00:00
|
|
|
let models = server.models;
|
2017-01-31 13:13:06 +00:00
|
|
|
|
2017-06-01 16:23:41 +00:00
|
|
|
for (let modelName in models) {
|
2017-01-31 13:13:06 +00:00
|
|
|
let model = models[modelName];
|
|
|
|
let validations = model.validations;
|
|
|
|
let jsonValidations = {};
|
|
|
|
|
2017-06-01 16:23:41 +00:00
|
|
|
for (let fieldName in validations) {
|
2017-01-31 13:13:06 +00:00
|
|
|
let jsonField = [];
|
|
|
|
|
2017-06-01 16:23:41 +00:00
|
|
|
for (let validation of validations[fieldName]) {
|
|
|
|
let options = validation.options;
|
|
|
|
if (options && options.async)
|
|
|
|
continue;
|
|
|
|
|
2017-01-31 13:13:06 +00:00
|
|
|
jsonField.push(toJson(validation));
|
2017-06-01 16:23:41 +00:00
|
|
|
}
|
2017-01-31 13:13:06 +00:00
|
|
|
|
|
|
|
jsonValidations[fieldName] = jsonField;
|
|
|
|
}
|
|
|
|
|
|
|
|
json[modelName] = {
|
|
|
|
properties: model.definition.rawProperties,
|
|
|
|
validations: jsonValidations
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
res.set('Content-Type', 'application/json');
|
|
|
|
res.send(JSON.stringify(json));
|
2017-06-01 16:23:41 +00:00
|
|
|
});
|
2017-01-31 13:13:06 +00:00
|
|
|
};
|