How do I specify an Array in my template.json?

I want schema checking for elements inside of an undefined-length array in my template.json, how do I do that?
11 Replies
Eranziel
Eranziel12mo ago
It's not possible using only template.json. You would need to use the data model API to define the schema.
Magus
Magus12mo ago
Where do I find documentation about this? static defineSchema() ?
Eranziel
Eranziel12mo ago
DataModel | Foundry Virtual Tabletop - API Documentation - Version 11
Documentation for Foundry Virtual Tabletop - API Documentation - Version 11
Eranziel
Eranziel12mo ago
I'll see if I can find some kind of walkthrough, there's a fair bit of moving parts when defining a data model & schema
Eranziel
Eranziel12mo ago
The Black Flag system should serve as a useful example, since it was written as a bit of a demonstration for data models: https://github.com/foundryvtt/black-flag/blob/main/system/datamodels/actor/player-character.mjs
GitHub
black-flag/system/datamodels/actor/player-character.mjs at main · f...
The official repository for Kobold Press's "Black Flag" game system implementation in Foundry Virtual Tabletop. - foundryvtt/black-flag
Magus
Magus12mo ago
The basic idea is I set up with my template.json and I complement it with any validation I want in defineSchema?
Eranziel
Eranziel12mo ago
That's the general idea. Maintaining template.json is a bit of a pain once you have a data model implemented, since the data model's schema gets enforced on every document change. There's only so much you can do with template.json, as you've seen. Arbitrary length arrays, optional objects, arbitrarily nested objects, etc... all can't be represented in template.json. Those all should be compatible with a data model & schema, though.
Magus
Magus12mo ago
Ty! I believe I will need to use a data model for my project then
Eranziel
Eranziel12mo ago
template.json mostly just gets used to define the initial data for a new actor/item (which then gets passed through the schema for validation and coercion), and in a couple cases Foundry can use it to validate document data if the data model/schema doesn't do so. If you've got a complete data model, template.json is kind of superfluous. (It's still a required file, though, as far as I can tell.)
Magus
Magus12mo ago
A single-element array, in theory, could easily be interpreted by Foundry as a undefined-length array, since there is no need in theory for an array with a single element, right good thing is that if im going to implement schema validations, at least It can be smarter than just type-checking usind the data model
Eranziel
Eranziel12mo ago
Yup, they're nice to have. You can also do casting to make sure a consistent type is written to the backend, e.g. converting "2" to 2 in a number field.
Want results from more Discord servers?
Add your server