Optionaldata: object = {}Initial data used to construct the data object. The provided object will be owned by the constructed model instance and may be mutated.
Optionaloptions: DataModelConstructionContext = {}Context and data validation options which affects initial model construction.
The source data object for this DataModel instance. Once constructed, the source object is sealed such that no keys may be added nor removed.
An immutable reverse-reference to a parent DataModel to which this model belongs.
Static Internal_The defined and cached Data Schema for all instances of this DataModel.
StaticLOCALIZATION_A set of localization prefix paths which are used by this DataModel. This provides an alternative to defining the
label and hint property of each field by having foundry map the labels to a structure inside the path
provided by the prefix.
JavaScript class definition and localization call.
class MyDataModel extends foundry.abstract.DataModel {
static defineSchema() {
return {
foo: new foundry.data.fields.StringField(),
bar: new foundry.data.fields.NumberField()
};
}
static LOCALIZATION_PREFIXES = ["MYMODULE.MYDATAMODEL"];
}
Hooks.on("i18nInit", () => {
// Foundry will attempt to automatically localize models registered for a document subtype, so this step is only
// needed for other data model usage, e.g. for a Setting.
Localization.localizeDataModel(MyDataModel);
});
JSON localization file
{
"MYMODULE": {
"MYDATAMODEL": {
"FIELDS" : {
"foo": {
"label": "Foo",
"hint": "Instructions for foo"
},
"bar": {
"label": "Bar",
"hint": "Instructions for bar"
}
}
}
}
}
Is the current state of this DataModel invalid? The model is invalid if there is any unresolved failure.
Define the data schema for this document instance.
An array of validation failure instances which may have occurred when this instance was last validated.
StaticschemaThe Data Schema for all instances of this DataModel.
Initialize the instance by copying data from the source object to instance attributes. This mirrors the workflow of SchemaField#initialize but with some added functionality.
Options provided to the model constructor
Modify some start time by adding a number of seconds or components to it. The delta components may be negative.
The initial time
Differential components to add
The resulting time
Clone a model, creating a new data model by combining current data with provided overrides.
Optionaldata: object = {}Additional data which overrides current document data at the time of creation
Optionalcontext: DataModelConstructionContext = {}Context options passed to the data model constructor
The cloned instance
Expand a world time integer into an object containing the relevant time components.
An amount of time expressed as components
The cumulative time in seconds
Count the number of leap years which have completed prior to some current year.
The current year
The number of leap years which have occurred prior to this year
Compute the difference between some new time and some other time.
A time to difference relative to the start time.
OptionalstartTime: number | ComponentsThe starting time. If not provided the current world time is used.
The time difference expressed as components
Format a time using one of several supported display formats.
Optionaltime: number | ComponentsThe time components to format, by default the current world time.
Optionalformatter: string | TimeFormatter = "timestamp"The formatter function applied to the time. If a string is provided, it must be a function configured in CONFIG.time.formatters.
Options passed to the formatter function
The formatted date and time string
Traverse the data model instance, obtaining the DataField definition for a field of a particular property.
A property key like ["abilities", "strength"] or "abilities.strength"
The corresponding DataField definition for that field, or undefined
Test whether a year is a leap year.
The year to test
Is it a leap year?
Allow the active world calendar instance to respond to changes in the world time. This method is called and awaited before "updateWorldTime" hooks are dispatched.
The new world time, game.time.worldTime
The relative change in the world time
Optionaloptions: objectOptions passed through the world time update operation
The user who triggered the time change
Reset the state of this data instance back to mirror the contained source data, erasing any changes.
Expand a world time integer into an object containing the relevant time components.
A time in seconds
The time expressed as components
Extract the source data for the DataModel into a simple object format that can be serialized.
The document source data expressed as a plain object
Copy and transform the DataModel into a plain object. Draw the values of the extracted object from the data source (by default) otherwise from its transformed values.
Optionalsource: boolean = trueDraw values from the underlying data source rather than transformed values
The extracted primitive object
Update the DataModel locally by applying an object of changes to its source data. The provided changes are expanded, cleaned, validated, and stored to the source data object for this model. The provided changes argument is mutated in this process. The source data is then re-initialized to apply those changes to the prepared data. The method returns an object of differential changes which modified the original data.
New values which should be applied to the data model
Options which determine how the new data is merged
An object containing differential keys and values that were changed
Validate the data contained in the document to check for type and content. This method is intended to validate complete model records, verifying both individual field validation as well as joint model validity.
For validating sets of partial model changes, it is preferred to call DataModel#updateSource as a dryRun. This
method provides a convenience alias for such a workflow if changes are provided.
Warning: if fallback handling is allowed, this process will mutate provided changes or model source data.
Options which modify how the model is validated
Whether the data source or proposed change is reported as valid. A boolean is always returned if validation is non-strict.
Protected_ProtectedConfigure the data model instance before validation and initialization workflows are performed.
Optionaloptions: object = {}Additional options modifying the configuration
Protected_ProtectedDecompose a timestamp in seconds to identify the number of completed years and remaining seconds. Also returns whether the remaining seconds fall within a leap year. This method is factored out so calendars which require advanced leap year handling can override this logic.
Protected_ProtectedA generator that orders the DataFields in the DataSchema into an expected initialization order.
Protected_ProtectedInitialize the source data for a new DataModel instance. One-time migrations and initial cleaning operations are applied to the source data.
The candidate source data from which the model will be constructed
Optionaloptions: DataModelConstructionContext = {}Options provided to the model constructor
Migrated and cleaned source data which will be stored to the model instance,
which is the same object as the data argument
Protected_ProtectedPrepare the state object that is transacted through an updateSource operation.
New values which should be applied to the data model
Options which determine how the new data is merged
Data model update state
Protected_ProtectedPerform the second step of the DataModel#_updateSource workflow which applies the prepared diff to the model.
The prepared copy of source data with changes applied
The differential changes that were applied to source
Options which determine how the new data is merged
Data cleaning state which might include instructions for final commit
Protected_ProtectedPerform the first step of the DataModel#_updateSource workflow which applies changes to a copy of model source data and records the resulting diff.
A mutable copy of model source data
New values which should be applied to the data model
Options which determine how the new data is merged
Data cleaning state
The resulting difference applied to source data
StaticcleanClean a data source object to conform to a specific provided schema.
Optionaldata: object = {}Provided model data that requires cleaning
Optionaloptions: DataModelCleaningOptions = {}Options that configure how data cleaning is performed
Optional_state: Partial<data.types.DataModelUpdateState> = {}Internal options used during cleaning recursion
Cleaned data which is suitable for validation and usage
StaticdefineDefine the data schema for models of this type. The schema is populated the first time it is accessed and cached for future reuse.
The schema, through its fields, provide the essential cleaning, validation, and initialization methods to turn the _source values into direct properties of the data model. The schema is a static property of the model and is reused by all instances to perform validation.
The schemas defined by the core software in classes like foundry.documents.BaseActor are validated by the
server, where user code does not run. However, almost all documents have a flags field to store data, and many
have a system field that can be configured to be a foundry.abstract.TypeDataModel instance. Those models
are not constructed on the server and rely purely on client-side code, which means certain extra-sensitive fields
must be also be registered through your package manifest. foundry.packages.types.ServerSanitizationFields
class SomeModel extends foundry.abstract.DataModel {
static defineSchema() {
return {
foo: new foundry.data.fields.StringField()
}
}
}
class AnotherModel extends SomeModel {
static defineSchema() {
// Inheritance and object oriented principles apply to schema definition
const schema = super.defineSchema()
schema.bar = new foundry.data.fields.NumberField()
return schema;
}
}
StaticformatThe configured calendar
Time components to format
Additional formatting options
The returned string format
StaticformatThe configured calendar
Time components to format
Additional formatting options
The returned string format
StaticformatThe configured calendar
Time components to format
Additional formatting options
The returned string format
StaticfromCreate a DataModel instance using a provided serialized JSON string.
Serialized document data in string format
A constructed data model instance
StaticfromCreate a new instance of this DataModel from a source record. The source data is presumed trustworthy and is not strictly validated unless explicitly requested.
Initial document data which comes from a trusted source.
Optionalcontext: Omit<DataModelConstructionContext, "strict"> & DataModelFromSourceOptions = {}Model construction context
StaticmigrateMigrate candidate source data for this DataModel which may require initial cleaning or transformations.
Candidate source data for the module, before further cleaning
Optionaloptions: Readonly<DataModelCleaningOptions>Additional options for how the field is cleaned
Optional_state: data.types.DataModelUpdateStateInternal state variables which are used during recursion
Migrated source data, ready for further cleaning
StaticmigrateWrap data migration in a try/catch which attempts it safely.
Candidate source data for the module, before further cleaning
Optionaloptions: Readonly<DataModelCleaningOptions> = {}Additional options for how the field is cleaned
Migrated source data, ready for further cleaning
StaticshimTake data which conforms to the current data schema and add backwards-compatible accessors to it in order to support older code which uses this data.
Data which matches the current schema
Optionaloptions: { embedded?: boolean } = {}Additional shimming options
Optionalembedded?: booleanApply shims to embedded models?
Data with added backwards-compatible properties, which is the same object as
the data argument
StaticvalidateEvaluate joint validation rules which apply validation conditions across multiple fields of the model. Field-specific validation rules should be defined as part of the DataSchema for the model. This method allows for testing aggregate rules which impose requirements on the overall model.
Candidate data for the model
Protected Static_ProtectedApply final custom model-specific cleaning rules after data schema fields are cleaned. Subclass models can implement this function as an ideal place to apply custom imputation or cleaning. Cleaning must be done in-place rather than returning a different object.
The provided input data for cleaning
Options which define how cleaning was performed
The data cleaning state
The original data object, with cleaning performed inplace
Protected Static_ProtectedApply preliminary model-specific cleaning rules or alter cleaning options or initial state. Subclass models may implement this function to configure the cleaning workflow. Any mutations to data, options, or _state parameters are performed inplace.
The provided input data for cleaning
Options which define how cleaning should be performed
The data cleaning state
Game Time Calendar configuration data model.
Mixes
CalendarConfig
Template: Components