mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-01-22 04:18:10 +00:00
Make sure to correctly convert config emit events after validation
We were validating the input configuration values by coercing them to the correct type, and then using the initial value to be saved (which currently is always converted to a string). We now use the coerced value as the actual value we will store, and more importantly emit. This means that the config.on('change' ...) calls will always be properly typed, which before this change was not a guarantee that we could make. Change-type: patch Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
parent
5567682c1c
commit
3d6dc88eb0
@ -174,8 +174,8 @@ export class Config extends (EventEmitter as {
|
||||
};
|
||||
|
||||
return Bluebird.try(() => {
|
||||
// Firstly validate all of the types as they are being set
|
||||
this.validateConfigMap(keyValues);
|
||||
// Firstly validate and coerce all of the types as they are being set
|
||||
keyValues = this.validateConfigMap(keyValues);
|
||||
|
||||
if (trx != null) {
|
||||
return setValuesInTransaction(trx).return();
|
||||
@ -251,10 +251,12 @@ export class Config extends (EventEmitter as {
|
||||
return schemaTypes[key].type.decode(value);
|
||||
}
|
||||
|
||||
private validateConfigMap<T extends SchemaTypeKey>(configMap: ConfigMap<T>) {
|
||||
private validateConfigMap<T extends SchemaTypeKey>(
|
||||
configMap: ConfigMap<T>,
|
||||
): ConfigMap<T> {
|
||||
// Just loop over every value, run the decode function, and
|
||||
// throw if any value fails verification
|
||||
_.map(configMap, (value, key) => {
|
||||
return _.mapValues(configMap, (value, key) => {
|
||||
if (
|
||||
!Schema.schema.hasOwnProperty(key) ||
|
||||
!Schema.schema[key as Schema.SchemaKey].mutable
|
||||
@ -276,13 +278,14 @@ export class Config extends (EventEmitter as {
|
||||
|
||||
const decoded = type.decode(value);
|
||||
if (decoded.isLeft()) {
|
||||
throw new Error(
|
||||
throw new TypeError(
|
||||
`Cannot set value for ${key}, as value failed validation: ${
|
||||
decoded.value
|
||||
}`,
|
||||
);
|
||||
}
|
||||
});
|
||||
return decoded.value;
|
||||
}) as ConfigMap<T>;
|
||||
}
|
||||
|
||||
private generateRequiredFields() {
|
||||
|
Loading…
Reference in New Issue
Block a user