foundryvtt icon indicating copy to clipboard operation
foundryvtt copied to clipboard

Update validation allows creating invalid items.

Open DJ4ddi opened this issue 2 years ago • 1 comments

What happened?

When using required fields in arrays, FoundryVTT accepts updates that do not contain the field for every entry, thereby creating items that are invalid (see log below, which is a correct validation failure).

It is my expectation that the update validation rejects updates that would create an invalid item.

What ways of accessing Foundry can you encounter this issue in?

  • [X] Native App (Electron)
  • [ ] Chrome
  • [X] Firefox
  • [ ] Safari
  • [ ] Other

Reproduction Steps

  1. Define a DataModel with an embedded model array containing a required field, e.g.:
class TestModel extends foundry.abstract.DataModel {
    static defineSchema() {
        return {
            path: new foundry.data.StringField({ required: true, blank: false, readonly: true }),
            value: new foundry.data.NumberField({ integer: true }),
        };
    }
}

export default class MyItemData extends foundry.abstract.DataModel {
    static defineSchema() {
        return {
            validationTest: new foundry.data.ArrayField(new fields.EmbeddedDataField(TestModel)),
        };
    }
}
  1. Register the data model for any item type and create a valid item of that type.
  2. Update the item with invalid data: item.update({ "system.validationTest": [{ value: 0 }] })
  3. Observe how the update is accepted, but the item disappears because it is now invalid (see error below).

What core version are you reporting this for?

11.315

Relevant log output

Error: Failed to initialize Item [t5l9qimzydodvfvp] in Actor [MGLszUc5xrMRyesa]: TestModel validation errors:
  path: may not be undefined
    at DataModelValidationFailure.asError (C:\Program Files\Foundry Virtual Tabletop\resources\app\public\scripts\commons.js:3894:14)
    at TestModel.validate (C:\Program Files\Foundry Virtual Tabletop\resources\app\public\scripts\commons.js:7164:61)
    at new DataModel (C:\Program Files\Foundry Virtual Tabletop\resources\app\public\scripts\commons.js:6918:12)
    at new TestModel (F:\Archive\Code\Repositories\Git\FVTT TDE5\dist\module\items\model\combatTechniqueData.js:4:1)
    at EmbeddedDataField.initialize (C:\Program Files\Foundry Virtual Tabletop\resources\app\public\scripts\commons.js:6023:14)
    at C:\Program Files\Foundry Virtual Tabletop\resources\app\public\scripts\commons.js:5903:42
    at Array.map (<anonymous>)
    at ArrayField.initialize (C:\Program Files\Foundry Virtual Tabletop\resources\app\public\scripts\commons.js:5903:20)

Bug Checklist

  • [X] The issue occurs while all Modules are disabled

DJ4ddi avatar Feb 08 '24 15:02 DJ4ddi

Thanks for the report.

aaclayton avatar Feb 12 '24 14:02 aaclayton