Skip to content

Commit 1bcb369

Browse files
committed
Populate model name in child model errors
1 parent ace076b commit 1bcb369

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

lib/modelate.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const modify = require('./modify').check;
88
* Create a new modelate, validating all properties
99
* Options might be:
1010
* - model: {Object} A model to use (instead of this.modelName)
11+
* - modelName: {String} A name for the model to use (instead of this.modelName)
1112
* @param data {Object} The data to validate
1213
* @param opts {Object} Options for the modelate.
1314
*/
@@ -17,11 +18,13 @@ function modelate(data, opts) {
1718
error: null
1819
}
1920

20-
if ((!this.modelName && !(opts && !opts.model)) || !data || typeof data !== 'object') {
21+
if ((!this.modelName && (!(opts && !opts.model) || !(opts && !opts.modelName))) || !data || typeof data !== 'object') {
2122
return modelateResponse;
2223
}
2324

24-
const model = (opts && opts.model) || models[this.modelName];
25+
var modelName = (opts && opts.modelName) || this.modelName;
26+
27+
const model = (opts && opts.model) || models[modelName];
2528
const result = util.clone(model) || {};
2629
const errors = [];
2730

@@ -30,7 +33,7 @@ function modelate(data, opts) {
3033
data[prop] = modify(data[prop], model[prop]);
3134

3235
// Step 2: validate the result
33-
if (validate(data[prop], model[prop], errors, this.modelName, prop)) {
36+
if (validate(data[prop], model[prop], errors, modelName, prop)) {
3437
result[prop] = data[prop];
3538
} else {
3639
delete result[prop];

lib/validators/model.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ function isValid(data, model, errors) {
1212
if (!model.model) {
1313
return true;
1414
}
15-
1615
if(!models[model.model]) {
1716
return false;
1817
}
1918

20-
var validate = module.parent.parent.exports;
21-
var valid = validate(data, { model: models[model.model] }, []);
19+
// Load parent modelate
20+
var modelate = module.parent.parent.exports;
21+
// Launch modelate of data with requested model
22+
var valid = modelate(data, {
23+
model: models[model.model],
24+
modelName: model.model
25+
}, []);
2226

2327
// Populate errors to parent model
2428
if (valid.error) {

0 commit comments

Comments
 (0)