Skip to content

Commit

Permalink
fix(manager): catch bad folder IDs earlier and add more data validation
Browse files Browse the repository at this point in the history
Relates to Z3nner#67 but is not a fix. Issue Z3nner#67 is caused by a missing (empty
string) folder ID, which should logically never occur, but clearly is
possible. In order to narrow down the places where the bad data is
being introduced, add more folder ID checks on user input actions.
  • Loading branch information
cirrahn committed Oct 16, 2024
1 parent adc9f56 commit c0c6827
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
6 changes: 4 additions & 2 deletions scripts/effectManager/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -464,8 +464,7 @@ export class EffectManagerApp extends FormApplication {
/* ----- */

async _handleClick_folderExpandCollapse(evt) {
const eleFolder = evt.currentTarget.closest("[data-folder-id]");
const folderId = eleFolder?.getAttribute("data-folder-id");
const folderId = evt.currentTarget.closest("[data-folder-id]")?.getAttribute("data-folder-id");
if (!folderId) throw new Error("Should never occur!");

await this._updateObject(null, {
Expand All @@ -488,6 +487,7 @@ export class EffectManagerApp extends FormApplication {
evt.stopPropagation();

const folderId = evt.currentTarget.closest("[data-folder-id]").getAttribute("data-folder-id");
if (!folderId) throw new Error("Should never occur!");

await this._updateObject(null, {
[`folders.${folderId}`]: {
Expand All @@ -498,6 +498,7 @@ export class EffectManagerApp extends FormApplication {

async _handleClick_folderDelete(evt) {
const folderId = evt.currentTarget.closest("[data-folder-id]").getAttribute("data-folder-id");
if (!folderId) throw new Error("Should never occur!");

await this._updateObject(null, {
[`folders.-=${folderId}`]: null,
Expand Down Expand Up @@ -697,6 +698,7 @@ export class EffectManagerApp extends FormApplication {
// If dropped to an existing row, update that row
if (eleFolder) {
const folderId = eleFolder.getAttribute("data-folder-id");
if (!folderId) throw new Error("Should never occur!");

return this._updateObject(null, {
[`folders.${folderId}.actorUuid`]: actor.uuid,
Expand Down
26 changes: 16 additions & 10 deletions scripts/effectManager/models.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,28 @@ export class EffectManagerData extends foundry.abstract.DataModel {
effects: new foundry.data.fields.ObjectField({
initial: () => ({}),
validate: (value, options) => {
return Object.values(value).every(obj => {
const isValid = schemaCustomEffect.validate(obj, options);
if (isValid === undefined) return true;
return isValid;
});
return (
Object.keys(value).every(id => id != null && id.trim()) &&
Object.values(value).every(obj => {
const isValid = schemaCustomEffect.validate(obj, options);
if (isValid === undefined) return true;
return isValid;
})
);
},
}),

folders: new foundry.data.fields.ObjectField({
initial: () => ({}),
validate: (value, options) => {
return Object.values(value).every(obj => {
const isValid = schemaFolder.validate(obj, options);
if (isValid === undefined) return true;
return isValid;
});
return (
Object.keys(value).every(id => id != null && id.trim()) &&
Object.values(value).every(obj => {
const isValid = schemaFolder.validate(obj, options);
if (isValid === undefined) return true;
return isValid;
})
);
},
}),
};
Expand Down

0 comments on commit c0c6827

Please sign in to comment.