stride3d/stride · error · ArgumentException

The given item does not validate the collection constraint.

Error message

The given item does not validate the collection constraint.

What it means

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.

Solutions

  1. Evaluate the same constraint predicate on the item before calling Add/Insert and fix or reject invalid items.
  2. Sanitize/repair the offending item so it satisfies the constraint.
  3. Build the list with ThrowException disabled, inspect the boolean result, then re-enable strict mode.

Example fix

// before
list.Add(newEntity); // throws when newEntity violates the constraint

// after
if (list.CheckConstraint(newEntity))
    list.Add(newEntity);
else
    logger.Error($"Rejected item: {newEntity}");
Defensive patterns

Strategy: validation

Validate before calling

// evaluate the same predicate before mutating the list
if (!constraintPredicate(item))
{
    // fix or reject the item
    return false;
}
list.Add(item);

Try / catch

try
{
    list.Add(item);
}
catch (ArgumentException ex) when (ex.Message.Contains("collection constraint"))
{
    logger.Error($"Item rejected by ConstrainedList: {item}");
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of stride3d/stride@96fad776d2 (2026-09-14). Data as JSON: /api/errors/fa981af38ee9c525. Report an issue: GitHub.

Appendix: source

Thrown at sources/core/Stride.Core/Collections/ConstrainedList.cs:128

    }

    /// <inheritdoc/>
    public void RemoveAt(int index)
    {
        innerList.RemoveAt(index);
    }

    /// <inheritdoc/>
    public T this[int index] { get { return innerList[index]; } set { if (CheckConstraint(value)) innerList[index] = value; } }

    private bool CheckConstraint(T item)
    {
        var result = true;
        if (Constraint != null)
        {
            result = Constraint(this, item);
            if (!result && ThrowException)
                throw new ArgumentException(errorMessage ?? "The given item does not validate the collection constraint.");
        }

        return result;
    }
}

View on GitHub (pinned to 96fad776d2)