Skip to content

Commit 43cabbd

Browse files
committed
Log Invisible Validation Errors
To prevent bugs such as we had with validating the session overview, where an error in a field that isn't currently open or showing errors prevents validation, this now logs those errors to the console. This isn't visible much to users, as it's in the console, but I used the intl service to translate them into nice errors anyway.
1 parent 52fbdf5 commit 43cabbd

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

packages/ilios-common/addon/classes/yup-validations.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getProperties } from '@ember/object';
33
import { object, setLocale } from 'yup';
44
import { restartableTask, timeout } from 'ember-concurrency';
55
import { modifier } from 'ember-modifier';
6+
import { getOwner } from '@ember/application';
67

78
const DEBOUNCE_MS = 100;
89

@@ -13,6 +14,7 @@ export default class YupValidations {
1314
context;
1415
schema;
1516
shape;
17+
intl;
1618

1719
@tracked error;
1820
@tracked showAllErrors = false;
@@ -22,6 +24,7 @@ export default class YupValidations {
2224
this.context = context;
2325
this.shape = shape;
2426
this.schema = object().shape(shape);
27+
this.intl = getOwner(context).lookup('service:intl');
2528
}
2629

2730
get errorsByKey() {
@@ -85,7 +88,26 @@ export default class YupValidations {
8588
}
8689

8790
async isValid() {
88-
return (await this.#validate()) === true;
91+
const isValid = await this.#validate();
92+
if (isValid === true) {
93+
return true;
94+
}
95+
//find errors that are not visible and log them
96+
//this makes it easier to find invisible errors when debugging
97+
const invisibleErrors = Object.keys(this.errorsByKey).filter(
98+
(key) => !this.visibleErrors.includes(key),
99+
);
100+
if (invisibleErrors.length) {
101+
invisibleErrors.forEach((key) => {
102+
const errors = this.errorsByKey[key].map(({ messageKey, values }) => {
103+
values.description = key;
104+
return this.intl.t(messageKey, values);
105+
});
106+
console.warn('Invisible errors prevented validation:\n ' + errors.join('\n '));
107+
});
108+
}
109+
110+
return false;
89111
}
90112

91113
addErrorDisplaysFor = (fields) => {

0 commit comments

Comments
 (0)