Prevent the attribute Items from getting invalid keys
This commit is contained in:
parent
3a7ffa4332
commit
bda5d05969
4 changed files with 56 additions and 5 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue