Implemented global validation on defined fields (#9992)

no-issue

This is to allow global validation to run on fields that have some user
validation defined.
This commit is contained in:
Fabien O'Carroll 2018-10-12 15:16:12 +07:00 committed by GitHub
parent 7a73dfd9bc
commit caccda1aab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 3 deletions

View File

@ -38,6 +38,11 @@ const validate = (config, attrs) => {
_.each(attrs, (value, key) => {
debug(key, value);
if (GLOBAL_VALIDATORS[key]) {
debug('global validation');
errors = errors.concat(validation.validate(value, key, GLOBAL_VALIDATORS[key]));
}
if (config && config[key]) {
const allowedValues = Array.isArray(config[key]) ? config[key] : config[key].values;
@ -53,9 +58,6 @@ const validate = (config, attrs) => {
errors.push(new common.errors.ValidationError());
}
}
} else if (GLOBAL_VALIDATORS[key]) {
debug('global validation');
errors = errors.concat(validation.validate(value, key, GLOBAL_VALIDATORS[key]));
}
});

View File

@ -38,6 +38,29 @@ describe('Unit: api/shared/validators/input/all', function () {
});
});
it('should run global validations on an type that has validation defined', function () {
const frame = {
options: {
slug: 'not a valid slug %%%%% http://',
}
};
const apiConfig = {
options: {
slug: {
required: true
}
}
};
return shared.validators.input.all.all(apiConfig, frame)
.then(() => {
throw new Error('Should not resolve');
}, (err) => {
should.exist(err);
});
});
it('default include array notation', function () {
const frame = {
options: {