{"record":{"id":"fa981af38ee9c525","repo":"stride3d/stride","slug":"the-given-item-does-not-validate-the-collection-constraint","errorCode":null,"errorMessage":"The given item does not validate the collection constraint.","messagePattern":"The given item does not validate the collection constraint\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core/Collections/ConstrainedList.cs","lineNumber":128,"sourceCode":"    }\n\n    /// <inheritdoc/>\n    public void RemoveAt(int index)\n    {\n        innerList.RemoveAt(index);\n    }\n\n    /// <inheritdoc/>\n    public T this[int index] { get { return innerList[index]; } set { if (CheckConstraint(value)) innerList[index] = value; } }\n\n    private bool CheckConstraint(T item)\n    {\n        var result = true;\n        if (Constraint != null)\n        {\n            result = Constraint(this, item);\n            if (!result && ThrowException)\n                throw new ArgumentException(errorMessage ?? \"The given item does not validate the collection constraint.\");\n        }\n\n        return result;\n    }\n}\n","sourceCodeStart":110,"sourceCodeEnd":134,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core/Collections/ConstrainedList.cs#L110-L134","documentation":"ConstrainedList<T> enforces a user-supplied Constraint predicate on every element. CheckConstraint evaluates the predicate for an item and, when the item fails and ThrowException is true, throws ArgumentException('The given item does not validate the collection constraint.'). It is invoked by Add, Insert, and the ConstrainedList constructor, so seeding the list from a collection containing an invalid item also throws.","triggerScenarios":"list.Add(item) or list.Insert(i, item) where the constraint predicate returns false; new ConstrainedList<T>(enumerable) where at least one source item violates the constraint; tightening the Constraint delegate at runtime and then adding previously valid items.","commonSituations":"Adding null where the constraint forbids null; adding values outside an allowed range (e.g. negative ids); binding the list to unvalidated user input or external data; deserialization paths feeding unsanitized items.","solutions":["Evaluate the same constraint predicate on the item before calling Add/Insert and fix or reject invalid items.","Sanitize/repair the offending item so it satisfies the constraint.","Build the list with ThrowException disabled, inspect the boolean result, then re-enable strict mode."],"exampleFix":"// before\nlist.Add(newEntity); // throws when newEntity violates the constraint\n\n// after\nif (list.CheckConstraint(newEntity))\n    list.Add(newEntity);\nelse\n    logger.Error($\"Rejected item: {newEntity}\");","handlingStrategy":"validation","validationCode":"// evaluate the same predicate before mutating the list\nif (!constraintPredicate(item))\n{\n    // fix or reject the item\n    return false;\n}\nlist.Add(item);","typeGuard":null,"tryCatchPattern":"try\n{\n    list.Add(item);\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"collection constraint\"))\n{\n    logger.Error($\"Item rejected by ConstrainedList: {item}\");\n}","preventionTips":["Validate items against the constraint predicate before Add/Insert.","Sanitize external/user-provided data before it reaches constrained collections.","When seeding from a collection, filter it with the same predicate first.","Document the constraint next to the list declaration so callers know the rules."],"tags":["csharp","stride-core","collections","validation"],"backgroundTag":"schema-validation-failed","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}