Check JSONPath for empty string - jsonpath

I have the following JSON string:
{
"myKey": "myValue"
}
I want to check whether this string is not empty. I can do this:
$.myKey
to get the value of myKey. I've tried changing it to
$.[?(#.myKey.length()>0)]
(as per this topic Expression to filter out elements with empty arrays using `jsonPath`) or even this:
$.myKey.length()>0
but I get error that it is not a valid JSONPath expression.
I add "JsonPath Match" to the test step in my project. When I set it to
$.myKey
and press "Select from current", it works. For
$.[?(#.myKey != '')]
when I press "Select from current", I get
"Invalid JsonPath expression"
I use SoapUI 5.4.0 but I don't know how to check its JsonPath implementation. I want to get false if string is empty.
How can I check whether myValue is empty or not? Thanks!

The following jsonpath filter: $.[?(#.myKey != '')] will return the document if the attribute myKey is not an empty string.
You can verify this using the Jayway JsonPath evaluator.
If this ...
How can I check whether myValue is empty or not?
... means something like: "I only want to return the document if myKey is populated" then the above jsonpath filter should suffice.

Related

QML Firebase startAt returns undefined

I am working on a 'typeahead’ type function which will check my Database with the current typed text to provide search suggestions of users using Felgo.
Here is the link for Felgos Firebase documentation
As to not search every entry I am looking to use the startAt and limitTo for a lower data use.
However when applying the startAt my searches only return undefined, I have tried testing this by changing my startAt from a variable to explicit data but this still only returns undefined.
My function is below:
function searchUsers(searchString) {
db.getValue("public/nameList/", {
orderByChild: true,
startAt: searchString, //searchString is a variable with my .currentText to search.
limitToFirst: 10,
}, function(success, key, value) {
if(success) {
searchArr = []
searchArr = value
console.debug("Read user value for key", key, "from DB:", value)
}
})
}
I have also tried by passing my var searchString through JSON.stringify(searchString) and also return undefined!
Removing the startAt: query entirely returns the entire result of nameList as expected, but no matter how I try to implement my startAt it always returns undefined.
A sample of my nameList JSON is:
nameList: {
"EddieLaw245" : 530343772383,
"EddieLawrence91" : 530343772385,
"EdwardL91" : 530343772386,
"EdwardLaw" : 530343772384,
"Edwardlawrence91" : 530343772380,
"JoBrownLondon" : 530343772381,
"KatiePrescottHair" : 543592635596,
"Tracey-Sweeting" : 530343772382
}
So with the above example, When I type E it should remove the last 3 entries, and so on.
The problem is that you're specifying orderByChild: true. If we look at the documentation of that:
orderByChild: If present, the queried object will have its properties ordered by values at sub-paths defined by the value of this property. Ordering by child properties makes the filter properties startAt, endAt and equalTo filter by the child property values
It may not be immediately clear from this, but orderByChild allows you to order the results on a property value under each of those nodes. So your code tries to order the child nodes on the value of a property true, which isn't possible (and should actually generate a compile-time error in the library) as the nodes under nameList don't have any child properties of their own. They merely have a key and a value.
What you're looking for is orderByKeys, which orders the child nodes on their keys. So:
db.getValue("public/nameList/", {
orderByKeys: true,
startAt: searchString,
limitToFirst: 10,
}
You'll typically also want to specify an endAt value, to ensure your type-ahead only shows values that start with the search string. If you only allow ASCII values in the keys, the simplest way to do this is:
startAt: searchString,
endAt: searchString + "~",
The ~ here is no magic operator, but merely the last ASCII characters. If you want to allow a broader character set, you'll need to use the last character in that character set - for example \uF7FF is the last code point for Unicode.
Update from OP
Though I'm certian Franks correct with typical Firebase usage; I suspect due to the Felgo plugin I am using the full solution has a slight adjustment;
db.getValue("public/nameList/", {
"orderByKey": true,
"startAt": searchString,
"endAt": searchString+"~",
"limitToFirst": 10,
}, function(success, key, value) {....}
})
Notes on the above - my filters/queries are surrounded by quotation marks "startAt", also instead of orderByKeys, I have used orderByKey

Firestore Security Rule - Validate incoming data length is greater than 2 but field is not required

I know how to check if a field of an incoming data set is a string or not, but how do I do a conditional check if the field exists, then check if its a string:
For example, "last name" is not a required field, but if it's supplied, then check if its length is greater than 2. I get an error if I just check the length rule generally -> is there a way to check the length only if last_name exists?
Firestore Rule:
request.resource.data.last_name.length() > 0
Function for validating that field would be like this:
function isLastNameValid() {
return request.resource.data.last_name == null || request.resource.data.last_name.size() > 2;
}
Checking if a value is a string is effectively the same as checking for existence. So if you do this:
request.resource.data.last_name is string
It will also return false if it doesn't exist.
But if you don't care what the type is, and you just want to check if it exists at all (as a string, number, whatever):
"last_name" in request.resource.data
request.resource.data is a Map, so check it's documentation at those links.

DynamoDB - Unable to "escape" input

I have a NodeJS app using Express-Js framework and the latest Express-Validator package. I have an input field that I want to "escape" and save the result in AWS DynamoDB.
The parameter is req.body.dealer_code and the value...
Before
req.body.dealer_code = "<script>alert('hello')</script>"
After
req.body.dealer_code = "<script>alert('hello')</script>"
When the value is inserted into the DynamoDB, it appears as "<script>alert('hello')</script>" in the Tree view.
What goes wrong here?
Thanks.
After an experiment, the string is indeed stored as an escaped string.
{ Item:
{ escaped: '<script>alert('hello')</script>'
}
}

What is valid query string for null array item?

I have the object:
{
"items": [ null, 1, 2 ]
}
and two versions of query strings for it:
array=null&array=1&array=2
array=&array=1&array=2
Which of them is proper? Is there any commonly used convention for this case?
Upd: the problem with null is that ASP.NET interprets it as "null" string.
It depends entirely on what the recipient application expects and parses.
The thing is, though, receiving "null", "false" etc from the query string will of course be interpreted as a string value, and therefore falsy.
So in the URL some_script.php?foo=false
if (!empty($_GET['foo'])) //true - contaisn "false", a string, not a boolean
If you want to be sure the parameter is therefore evaluated to falsy, I would suggest passing it empty.

request.getQueryString() returns value but request.getParameter() return null for the key

I am passing some parameters as part of url, I am able to see the value when I use request.getQueryString(). For instance, keyword=dual%20element%w20fuse. But when I use request.getParameter("keyword") it's returning null.
This is happening only for some keywords. For instance: if I pass the keyword as xyz, request.getParameter("keyword") correctly returns xyz.
Why am I getting null for those keywords?
String queryParams = request.getQueryString();
String keyword = request.getParameter("keyword");

Resources