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

Make empty and nempty work for Array types #18042

Open
rijkvanzanten opened this issue Apr 3, 2023 Discussed in #18039 · 6 comments
Open

Make empty and nempty work for Array types #18042

rijkvanzanten opened this issue Apr 3, 2023 Discussed in #18039 · 6 comments

Comments

@rijkvanzanten
Copy link
Member

Discussed in #18039

Originally posted by soulseekah April 3, 2023

Summary

There is currently no way to check if an array is empty or not.

_empty does not work as per #18028

while "_eq": [] is not supported

Basic Example

Create a flow, Read step that returns [] when nothing is found, Condition that checks {"$last": {"_empty": true}} or {"$last":{"_eq": []}}. First option expects '', false or null. Second option errors out as unsupported operand.

Motivation

Checking if an array is empty or not is a quite important and foundational construct to have available in any system.

Detailed Design

_empty and _nempty should treat an empty [] as falsy, despite them being always truthy.

Requirements List

Must Have:

  • _empty checks for length of 0 on array typed values
  • _nempty checks for length of >= 1 on array typed values

Should Have:

n/a

Could Have:

n/a

Won't Have:

n/a

Drawbacks

There are no known drawbacks to supporting arrays in _empty and _nempty.

Alternatives

Writing a custom script that checks .length is a usable approach, before applying a condition.

image

Adoption Strategy

_empty will start supporting arrays. A notice will be posted on the release notes to notify people of the possible breaking change

Unresolved Questions

n/a

@shipsides-sparknel
Copy link

#15504 ([] not being considered as null for field validation) is tangentially related to this, which was attempted to be fixed but instead caused #16291. is finding a solution for this part of the scope of this?

@jekuer
Copy link
Sponsor Contributor

jekuer commented Sep 20, 2023

The mentioned alternative does not work well when filtering within a (for example) delete flow step, where it is about a lot of items. Of course, you can also do this via a script, but feels like unnecessarily complicated.

What you can do, right within the filter (since function parameters are now possible):

{
    "count(field)": {
        "_eq": 0
    }
}

Maybe an alternative approach for this ticket is to update documentation instead of updating "_empty"?

@MarvinXu
Copy link

MarvinXu commented Oct 11, 2023

The mentioned alternative does not work well when filtering within a (for example) delete flow step, where it is about a lot of items. Of course, you can also do this via a script, but feels like unnecessarily complicated.

What you can do, right within the filter (since function parameters are now possible):

{
    "count(field)": {
        "_eq": 0
    }
}

Maybe an alternative approach for this ticket is to update documentation instead of updating "_empty"?

@jekuer how did you make type check work for this?

@jekuer
Copy link
Sponsor Contributor

jekuer commented Oct 11, 2023

@MarvinXu , you mean before whether it's an array or not OR regarding the type of the array element?

Regarding first: This is quite static in my setup. Not sure, but most probably you could also work with the "_or" operator. like nnull = false OR count = 0. This would fail if not given at all, null or an empty array.

Regarding latter one: I don't 😆

@MarvinXu
Copy link

@jekuer sorry, I mean Typescript error
image

@jekuer
Copy link
Sponsor Contributor

jekuer commented Oct 11, 2023

Good catch. I don't, because I use this only at the integrated Flow-Feature of Directus. In my app, I am filtering out empty arrays after fetching the data from the Directus backend.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 📋 Backlog
Development

No branches or pull requests

5 participants