How to properly write Regex validation in Firestore Security Rules - firebase

I'm trying to validate submitted data against Regex expressions in Firestore Security Rules. I've read through the documentation, but I'm still unsure how to write them.
I read through the Regex documentation here https://firebase.google.com/docs/reference/security/database/regex , but it doesn't really show how to use it in Firestore. I tried using an 'allow validate' rule and copy/pasted a regex expression from the docs, but I'm getting all kinds of errors.
Here's what I tried:
Do I need to put it in a format like this? (From the docs) allow create: if !("ranking" in request.resource.data)? If so, how do I use a regex expression with it?

It looks like you're trying to start a regex with /. That's not going to work - it's not like perl. Slash starts a path object. Might want to check the API documentation on this. You should just be passing a plain string to matches() that contains the regex to match.
https://firebase.google.com/docs/reference/rules/rules.String#matches

Related

Firebase - add database rules - Path contains invalid characters

I'm following the tutorial of firebase.
But I can´t add the rules .read and .write
Actually, this Firebase DB error is frequently caused by an outdated webpage cache. All you need to do is refresh the Realtime Database webpage, and then you'll be able to add your new values:
You are adding rules at wrong place. That is the reason for downvotes. This is the data structure not your rules document.
Put json rules under database rules tab. You have to put rules here. Please check.
This is happening because the path that includes the key CANNOT have "." period marks according to this link.
As all Firebase Realtime Database data is stored as JSON objects, when
you create your own keys, they must be UTF-8 encoded, can be a maximum
of 768 bytes, and cannot contain ., $, #, [, ], /, or ASCII control
characters 0-31 or 127. You cannot use ASCII control characters in the
values themselves, either.
I wish this helps you.
And if you want to deal with RULES follow what IGOR said.
Best regards.
Firebase Database path can't contain some special characters like " . " , "#" etc. and above all you are adding the rule in the wrong place
Maybe you should log in again in your account

How to remove punctuation from a database in marklogic?

I want to remove punctuation from a database of xml document in marklogic. This is made for preprocessing purposes for machine learning. I'm new to marklogic and i don't know how to do that. Is there an xquery query that could remove punctuation?
To do a mass replacement of all text in the database, and take out punctuation, you could start with something that looks like this code (modified for your needs):
for $doc in cts:search(fn:collection(), ())
for $text in $doc//text()
return xdmp:node-replace($text, text{fn:replace($text, "[\.,;]", "")})
To be honest, that task is much less expensive to do on the source text files themselves - or in MarkLogic by treating the XML as string during the replacement process. Updating nodes one element at a time will be expensive.
Outside of Marklogic:
use SED or AWK or a similar tool BEFORE INGESTION
Inside of MarkLogic(as a trigger, perhaps)
use xdmp:quote to change the XML to a string, then replace in a sing with fn:replace and then make XML again with xdmp:unquote
let $new-doc := xdmp:unquote(fn:replace(xdmp:quote($doc), "[\.,;]", ""))
Then either store by replacing the root node with xdmp:node-replace - or store this version as a property. This all depends on if the original (punctuated version matters to you). Or perhaps you just want to keep the original and serve this cleansed version back to someone.
In all cases above, you have to make sure that your replacement does not murder your XML. Also, be aware of options for the functions above(like how cdata is handled.
Lastly, "This is for machine learning purposes". You do not elaborate. I think many of us here have a feeling that this solution (cleansing punctuation before insert) rubs against the very grain of MarkLogic - in which you store as-is and then have awesome index, tokenizing, stemming, collation, search support to find and return your data as you need. If you were to elaborate on your use case a bit, you may inspire others to give more MarkLogic-Specific suggestions.
It will work if you use 'punctuation-insensitive' and if required 'diacritic-insensitive' in cts:element-word-query()
I'm not sure if this is what you're asking, but it's technically possible to update every document in the database to remove punctuation; however, it's very expensive and I wouldn't recommend it.
Using built-in search functions, you can probably achieve the same goal without updating your documents by querying with punctuation insensitivity. For example, if you want to select documents with a title matching a case insensitive string:
cts:search(//mydoc,
cts:element-word-query(xs:QName('title'), 'Moby-Dick', 'punctuation-insensitive'))
Or in an existing XQuery:
for $d in $documents
where cts:contains($d,
cts:element-word-query(xs:QName('title'), 'Moby-Dick', 'punctuation-insensitive'))
return $d/summary

JavaCC match token group

I ended up writing a parser for a small subset of SQL.
The grammar has a lot of regular tokens (SELECT, CREATE, ...) and a few more general (e.g. S_GEN_IDENTIFIER matches [A-Z_.\d]|\"(~[\n, \r, \"])*\").
The problem is, "SELECT col AS type ..." doesn't get parsed since instead of <S_GEN_IDENTIFIER> "type" column alias is matched as <T_TYPE>.
I had an idea to replace token with a rule with the same name and check is the token of interest lies within some token range (something like [<T_AS> - <T_KEEP_DUPLICATES>]. Unfortunately it turned out that the syntax for tokens and rules differs so I can't do it.
I could just copy-paste all tokens inside the new rule but I don't want to do it for obvious reasons.
Is there any way to check if token lies within the range of predefined tokens?
Perhaps you could treat "type" as an unreserved keyword. Then you can follow the advice of question 4.19 of the FAQ
http://www.engr.mun.ca/~theo/JavaCC-FAQ/javacc-faq-moz.htm#tth_sEc4.19

asp.net allow german characters in Url

I am using RegularExpressionValidator control with
[http(s)?://]*([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
regular expression to validate Url. I need to allow german characters
(ä,Ä,É,é,ö,Ö,ü,Ü,ß)
in Url. What should be exact regular expression to allow these characters?
I hope you are aware that it is not easy to use regex for URL validation, because there are many valid variations of URLs. See for example this question.
First your regex has several flaws (this is only after a quick check, maybe not complete)
See here for online check on Regexr
It does not match
http://RegExr.com?2rjl6]
Why do you allow only \w and - after the first dot?
but it does match
hhhhhhppth??????ht://stackoverflow.com
You define a character group at the beginning [http(s)?://] what means match any of the characters inside (You probaly want (?:http(s)?://) and ? after wards instead of *.
To answer your question:
Create a character group with those letters and put it where you want to allow it.
[äÄÉéöÖüÜß]
Use it like this
(?:https?://)?([äÄÉéöÖüÜß\w-]+\.)+[äÄÉéöÖüÜß\w-]+(/[-äÄÉéöÖüÜß\w ./?%&=]*)?
Other hints
The - inside of a character group has to be at the start or the end or needs to be escaped.
(s)? is s?

Pattern matching using decorators

I want to define a specific URL pattern using Sitemesh decorators.xml. I want to define a decorator that matches all URLs ending with "/story/_NUMBER_" to be targetted by the decorator. I tried:
<decorator name="customMain" page="customMain.jsp">
<pattern>/story/[0-9]+</pattern>
</decorator>
But this does not work.. Do regular expressions work in decorators.xml? If not, how do I target URLs that end with the above pattern?
Just ran into this myself. I don't think it's possible to use regular expressions at all. Only wildcard patterns with * and ?.
Look at the source here for more details.

Resources