Regexp Pattern to Validate Filters

I need to validate a filter entered by user!

As an example consider I have a textf contains a filter expression

<$vars textf="[tag[demo]]" pattern="^\[.*\]$">
<$list filter="[<text>regexp<patern>]" emptyMessage="No it is not">
    Yes, this is a filter
</$vars>

The above code partially works! for example for the below textf it returns wrong result

textf= "[tag[demo[]"                                  ------> unbalance [
textf=[tag[demo]addprefix<a]                  ------> unbalance <
...

what is the correct filter pattern to validate a filter entered by user!

I think inside filter we should balance
`<>`
`{}`
`[]`

and filter starts with `[` and ends with `]`
1 Like

I suspect it would be vastly more complex than you might think to have a general purpose validation routine due to the complexity allowed. I will say that the core itself does it, so reading through the core tiddlers might give you some insight? Here’s how I took a quick look:

  1. I opened up the “Advanced Search” and typed something known to be wrong. It gave me a “Filter error” message.
  2. I searched for that message in shadow tiddlers and found a whole list of javascript tiddlers with code for this kind of thing, such as:
    1. $:/core/modules/filters.js
    2. $:/core/modules/filters/format.js
    3. $:/core/modules/filters/is.js
    4. $:/core/modules/filters/range.js
    5. $:/core/modules/filters/strings.js

Reading through filters.js there is lots of regex testing to view, right at the beginning it looks like:


So that one long string looks like just the string to detect an operand? Lots of reverse engineering to do depending on how in-depth you’re trying to be. Maybe there’s a way to re-use or call upon the existing logic rather than build it again yourself? That’s a bit out of my league, but thought this might give you some insight.

Thank you Adam!
So, It is better not go through that because it seems too complex!
You are right, as the core does it itself I can just pass any filter entered by user and let see what core do with it!

You could instead accept values from users such as tags or field values, checking they exist and construct the filter for them.

There are also ways to simplify what the user provides, some time ago i made a tool for tags where the user could provide tag names with or without a plus or minus. Eg tag1 +tag2 -tag3 and turned this into the result they would expect.

By chance, I had a similar case in Section Editor, I do not want to validate and just check if entry is similar to a filter, so for me [ ] is enough!
Like @stobot I assume this is done by core and no need to duplicate it!