mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Added validation for null|undefined values for required keys
closes #10071
This commit is contained in:
parent
ccd9541f75
commit
7af2802e14
2 changed files with 39 additions and 0 deletions
|
@ -122,10 +122,13 @@ module.exports = {
|
|||
|
||||
if (apiConfig.data) {
|
||||
const missedDataProperties = [];
|
||||
const nilDataProperties = [];
|
||||
|
||||
_.each(apiConfig.data, (value, key) => {
|
||||
if (jsonpath.query(frame.data[apiConfig.docName][0], key).length === 0) {
|
||||
missedDataProperties.push(key);
|
||||
} else if (_.isNil(frame.data[apiConfig.docName][0][key])) {
|
||||
nilDataProperties.push(key);
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -137,6 +140,15 @@ module.exports = {
|
|||
})
|
||||
}));
|
||||
}
|
||||
|
||||
if (nilDataProperties.length) {
|
||||
return Promise.reject(new common.errors.ValidationError({
|
||||
message: common.i18n.t('notices.data.validation.index.validationFailed', {
|
||||
validationName: 'FieldIsInvalid',
|
||||
key: JSON.stringify(nilDataProperties)
|
||||
})
|
||||
}));
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
|
|
|
@ -332,6 +332,33 @@ describe('Unit: api/shared/validators/input/all', function () {
|
|||
});
|
||||
});
|
||||
|
||||
it('fails', function () {
|
||||
const frame = {
|
||||
data: {
|
||||
docName: [{
|
||||
a: 'b',
|
||||
b: null
|
||||
}]
|
||||
}
|
||||
};
|
||||
|
||||
const apiConfig = {
|
||||
docName: 'docName',
|
||||
data: {
|
||||
b: {
|
||||
required: true
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return shared.validators.input.all.add(apiConfig, frame)
|
||||
.then(Promise.reject)
|
||||
.catch((err) => {
|
||||
should.exist(err);
|
||||
err.message.should.eql('Validation (FieldIsInvalid) failed for ["b"]');
|
||||
});
|
||||
});
|
||||
|
||||
it('success', function () {
|
||||
const frame = {
|
||||
data: {
|
||||
|
|
Loading…
Add table
Reference in a new issue