mirror of
https://github.com/Foundryborne/daggerheart.git
synced 2026-01-14 20:51:07 +01:00
FEAT: new PseudoDocumentsField
FIX: BasePseudoDocument 's getEmbeddedDocument
This commit is contained in:
parent
70d8f37d3c
commit
df23847392
3 changed files with 26 additions and 3 deletions
|
|
@ -1,2 +1,3 @@
|
||||||
export { default as FormulaField } from "./formulaField.mjs";
|
export { default as FormulaField } from "./formulaField.mjs";
|
||||||
export { default as ForeignDocumentUUIDField } from "./foreignDocumentUUIDField.mjs";
|
export { default as ForeignDocumentUUIDField } from "./foreignDocumentUUIDField.mjs";
|
||||||
|
export { default as PseudoDocumentsField } from "./pseudoDocumentsField.mjs";
|
||||||
22
module/data/fields/pseudoDocumentsField.mjs
Normal file
22
module/data/fields/pseudoDocumentsField.mjs
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
import { BasePseudoDocument } from "../pseudo-documents/_types";
|
||||||
|
export default class PseudoDocumentsField extends foundry.data.fields.TypedObjectField {
|
||||||
|
constructor(model, options = {}, context = {}) {
|
||||||
|
options.validateKey ||= ((key) => foundry.data.validators.isValidId(key));
|
||||||
|
if (!(model instanceof BasePseudoDocument)) throw new Error("The model must be a PseudoDocument");
|
||||||
|
const field = new foundry.data.fields.EmbeddedDataField(model);
|
||||||
|
super(field, options, context);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritdoc */
|
||||||
|
static get _defaults() {
|
||||||
|
return Object.assign(super._defaults, {
|
||||||
|
max: Infinity
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @override */
|
||||||
|
_validateType(value, options = {}) {
|
||||||
|
if (Object.keys(value).length > this.max) throw new Error(`cannot have more than ${this.max} elements`);
|
||||||
|
return super._validateType(value, options);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -121,8 +121,8 @@ export default class BasePseudoDocument extends foundry.abstract.DataModel {
|
||||||
getEmbeddedDocument(embeddedName, id, { invalid = false, strict = false } = {}) {
|
getEmbeddedDocument(embeddedName, id, { invalid = false, strict = false } = {}) {
|
||||||
const embeds = this.constructor.metadata.embedded ?? {};
|
const embeds = this.constructor.metadata.embedded ?? {};
|
||||||
if (embeddedName in embeds) {
|
if (embeddedName in embeds) {
|
||||||
const path = embeds[embeddedName];
|
const path = `${embeds[embeddedName]}.${id}`;
|
||||||
return foundry.utils.getProperty(this, path).get(id, { invalid, strict }) ?? null;
|
return foundry.utils.getProperty(this, path) ?? null;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue