daggerheart/module/data/fields/itemLinksField.mjs
Joaquin Pereyra 060fe41730 FIX: flatten object value in _cast method
FIX: `mergeObject` to `foundry.utils.mergeObject`
FIX: add validateKey
2025-07-21 17:30:48 -03:00

38 lines
1.2 KiB
JavaScript

export default class ItemLinksField extends foundry.data.fields.TypedObjectField {
/**
* @param {DataFieldOptions} [options] Options which configure the behavior of the field.
* @param {DataFieldContext} [context] Additional context which describes the field
*/
constructor(options, context) {
super(
new foundry.data.fields.StringField({
choices: CONFIG.DH.ITEM.featureSubTypes,
nullable: true,
initial: null
}),
options,
context
);
}
/** @inheritDoc */
static get _defaults() {
return foundry.utils.mergeObject(super._defaults, { validateKey: this.validateKey });
}
/**
* @param {Object} [value] The candidate object to be added.
*/
static validateKey(value) {
const parsed = foundry.utils.parseUuid(value);
if (!parsed || parsed.type !== foundry.documents.Item.documentName) return false;
if (!foundry.data.validators.isValidId(parsed.documentId)) return false;
return true;
}
/**@inheritdoc */
_cast(value) {
value = super._cast(value);
return foundry.utils.flattenObject(value);
}
}