Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NotEmptyCollection] to validate empty list/array #77

Closed
laurentopia opened this issue Aug 22, 2024 · 6 comments
Closed

[NotEmptyCollection] to validate empty list/array #77

laurentopia opened this issue Aug 22, 2024 · 6 comments
Assignees
Labels
enhancement New feature or request

Comments

@laurentopia
Copy link

probably by design but would be nice ™
image
image

@TylerTemp TylerTemp changed the title [Required] on empty list doesn't show error [NotEmpty] to validate empty list/array Aug 22, 2024
@TylerTemp TylerTemp self-assigned this Aug 22, 2024
@TylerTemp TylerTemp added the enhancement New feature or request label Aug 22, 2024
@TylerTemp TylerTemp changed the title [NotEmpty] to validate empty list/array [NotEmptyCollection] to validate empty list/array Aug 22, 2024
@AliAlbarrak
Copy link

I suggest using a property like [ArraySize] but with no editor behavior. That will be useful in more scenarios

It should be defined like

    [AttributeUsage(AttributeTargets.Field)]
    public class CollectionSize : Attribute
    {
        public int Min = 0;
        public int Max = int.MaxValue;
    }

And used like

    [CollectionSize(Min = 1)] // Equivalent to [NotEmptyCollection]
    public List<Transform> endOfBarrels;
    
    [CollectionSize(Min = 1, Max = 10)] // Size must be between 1 and 10 inclusive
    public List<Transform> endOfBarrels;
    
    [CollectionSize(Max = 10)] // Can be empty but size has to be less than or equal 10
    public List<Transform> endOfBarrels;

@TylerTemp
Copy link
Owner

@AliAlbarrak This is a much better idea.

I think I'll put it into existing API, either ListDrawerSettings or ArraySize

@laurentopia
Copy link
Author

laurentopia commented Sep 18, 2024

ArraySize already exists, it hard sets the array size

@TylerTemp
Copy link
Owner

ArraySize already exists, it hard sets the array size

It can be like this:

  • ArraySize(10) is the same as the old one, set the fixed size to 10
  • ArraySize(5, 10) means min=5, and max=10

Then, ArraySize(min: 1) will do the trick: at least 1 element is expected.

This at least can keep the compatibility with the old API

@TylerTemp
Copy link
Owner

TylerTemp commented Oct 6, 2024

Hi,

this feature has been added to 3.3.8

  1. ArraySize allows to set range
  2. If you have ListDrawerSettings enabled, the Add and Remove buttons will be disabled/enabled accordingly if you also have ArraySize

For example:

  • [ArraySize(3)] will make the array size fixed to 3
  • [ArraySize(1, 5)] will make the array size range to 1-5 (both included)
  • [ArraySize(min: 1)] will make the array size at least 1 (no max value specific). Useful to require a non-empty array.
[ArraySize(1, 3), ListDrawerSettings] public int[] oneToThreeWithSettings;

image

@laurentopia
Copy link
Author

That's so nice! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants