0
Fork 0
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:
kirrg001 2018-12-10 15:24:28 +01:00 committed by Katharina Irrgang
parent ccd9541f75
commit 7af2802e14
2 changed files with 39 additions and 0 deletions

View file

@ -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)
})
}));
}
}
},

View file

@ -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: {