[#46] Fix sheets entering an inconsistent state if they throw an error during form data marshalling.

This commit is contained in:
Kim Mantas 2023-05-16 14:20:48 +01:00
parent 0996cf810c
commit 2bd28e939d
No known key found for this signature in database
GPG key ID: 3A4E57DAE36AC3C4
2 changed files with 17 additions and 5 deletions

View file

@ -16,6 +16,7 @@
"SIMPLE.NotifyGroupAlphanumeric": "Attribute group names may not contain spaces or periods.", "SIMPLE.NotifyGroupAlphanumeric": "Attribute group names may not contain spaces or periods.",
"SIMPLE.NotifyGroupReserved": "Attribute group \"{key}\" is reserved and cannot be used.", "SIMPLE.NotifyGroupReserved": "Attribute group \"{key}\" is reserved and cannot be used.",
"SIMPLE.NotifyAttrDuplicate": "Attribute key already exists as a group.", "SIMPLE.NotifyAttrDuplicate": "Attribute key already exists as a group.",
"SIMPLE.NotifyAttrInvalid": "Attribute keys may not contain spaces or periods.",
"SIMPLE.ResourceMin": "Min", "SIMPLE.ResourceMin": "Min",
"SIMPLE.ResourceValue": "Value", "SIMPLE.ResourceValue": "Value",
@ -35,4 +36,4 @@
"SIMPLE.Create": "Create", "SIMPLE.Create": "Create",
"SIMPLE.New": "New" "SIMPLE.New": "New"
} }

View file

@ -429,8 +429,7 @@ export class EntitySheetHelper {
group = v[attrKey]['group']; group = v[attrKey]['group'];
groupKeys.push(group); groupKeys.push(group);
let attr = v[attrKey]; let attr = v[attrKey];
let k = v[attrKey]["key"] ? v[attrKey]["key"].trim() : attrKey.trim(); const k = this.cleanKey(v[attrKey]["key"] ? v[attrKey]["key"].trim() : attrKey.trim());
if ( /[\s\.]/.test(k) ) return ui.notifications.error("Attribute keys may not contain spaces or periods");
delete attr["key"]; delete attr["key"];
// Add the new attribute if it's grouped, but we need to build the nested structure first. // Add the new attribute if it's grouped, but we need to build the nested structure first.
if ( !obj[group] ) { if ( !obj[group] ) {
@ -441,8 +440,7 @@ export class EntitySheetHelper {
} }
// Handle attribute keys for ungrouped attributes. // Handle attribute keys for ungrouped attributes.
else { else {
let k = v["key"].trim(); const k = this.cleanKey(v["key"].trim());
if ( /[\s\.]/.test(k) ) return ui.notifications.error("Attribute keys may not contain spaces or periods");
delete v["key"]; delete v["key"];
// Add the new attribute only if it's ungrouped. // Add the new attribute only if it's ungrouped.
if ( !group ) { if ( !group ) {
@ -588,4 +586,17 @@ export class EntitySheetHelper {
foundry.utils.setProperty(attrs, attr, Math.clamped(value, current.min || 0, current.max || 0)); foundry.utils.setProperty(attrs, attr, Math.clamped(value, current.min || 0, current.max || 0));
} }
} }
/* -------------------------------------------- */
/**
* Clean an attribute key, emitting an error if it contained invalid characters.
* @param {string} key The key to clean.
* @returns {string}
*/
static cleanKey(key) {
const clean = key.replace(/[\s.]/g, "");
if ( clean !== key ) ui.notifications.error("SIMPLE.NotifyAttrInvalid", { localize: true });
return clean;
}
} }