Prevent the attribute Items from getting invalid keys

This commit is contained in:
Oliver 2026-04-25 13:55:01 -06:00
parent 3a7ffa4332
commit bda5d05969
4 changed files with 56 additions and 5 deletions

View file

@ -5,11 +5,11 @@ import { PlayerSheet } from "./apps/PlayerSheet.mjs";
import { QueryStatus } from "./apps/QueryStatus.mjs";
// Utils
import { toID, isValidID, } from "./utils/toID.mjs";
import { attributeSorter } from "./utils/attributeSort.mjs";
import { DialogManager } from "./utils/DialogManager.mjs";
import { localizer } from "./utils/localizer.mjs";
import { QueryManager } from "./utils/QueryManager.mjs";
import { toID } from "./utils/toID.mjs";
const { deepFreeze } = foundry.utils;
@ -26,5 +26,6 @@ export const api = deepFreeze({
attributeSorter,
localizer,
toID,
isValidID,
},
});

View file

@ -1,3 +1,7 @@
import { isValidID, toID } from "../../utils/toID.mjs";
const { hasProperty } = foundry.utils;
export class AttributeItemData extends foundry.abstract.TypeDataModel {
// #region Schema
static defineSchema() {
@ -37,6 +41,17 @@ export class AttributeItemData extends foundry.abstract.TypeDataModel {
// #region Lifecycle
async _preCreate(data, options, user) {
// Assign the key as the ID'd name if isn't provided, or validate if
// it is provided.
if (!this.key) {
this.updateSource({ key: toID(this.parent.name) });
} else if (!isValidID(this.key)) {
ui.notifications.error(_loc(
`taf.notifs.error.invalid-attribute-key`,
{ key: this.key },
));
return false;
};
// Prevent duplicate Attribute keys from existing on a single Actor
if (this.parent.isEmbedded) {
@ -55,6 +70,19 @@ export class AttributeItemData extends foundry.abstract.TypeDataModel {
return super._preCreate(data, options, user);
};
async _preUpdate(data, options, user) {
// Prevent invalid IDs
if (hasProperty(data, `system.key`) && !isValidID(data.system.key)) {
ui.notifications.error(_loc(
`taf.notifs.error.invalid-attribute-key`,
{ key: data.system.key },
));
delete data.system?.key;
};
return super._preUpdate(data, options, user);
};
// #endregion Lifecycle
// #region Methods

View file

@ -1,6 +1,6 @@
/**
* A helper method that converts an arbitrary string into a format that can be
* used as an object key easily.
* A helper method that converts an arbitrary string into a format
* that can be used as an object key easily.
*
* @param {string} text The text to convert
* @returns The converted ID
@ -9,5 +9,26 @@ export function toID(text) {
return text
.toLowerCase()
.replace(/\s+/g, `_`)
.replace(/\W/g, ``);
.replace(/\W/g, ``)
.replace(/(^_|_$)/);
};
/**
* A helper method that reports if an arbitrary string is considered a
* valid ID for use in the system
*
* @param {string} text The text to check
* @returns Whether or not the text is a valid ID
*/
export function isValidID(text) {
return !(
// any uppercase characters
text.match(/[A-Z]/)
// any non-word characters
|| text.match(/\W/)
// any whitespace characters
|| text.match(/\s/)
);
};