How to check that value of field in request body is json using Wiremock - regex

I have a JSON request and I need to check that the value of one field is json.
I'm trying use regex:
"request": {
...
"bodyPatterns": [
{
"equalToJson": {
"field_1": "Value",
"field_2": "${json-unit.any-number}",
"field_3": "${json-unit.regex}^\u007B.\u007D$"
}
}
]
}
Also I have tried:
"field_3": "${json-unit.regex}\\{.\\}"
"field_3": "${json-unit.regex}{.}"
But none of this works. How will it be correct to determine that the value of the field contains JSON?
An example request that must match the stub:
{
"field_1": "Value",
"field_2": 100,
"field_3": {
"1.1": 2,
"1.2": null,
"1.3": null,
"1.4": null,
...
}
}

You can only use the regex placeholder against string values, so this won't work in this instance as the element's value is an object.
However, if you just want to say "I want this element to be present but don't care about the contents" you can use "${json-unit.ignore}" as the value.
If you do need to be more explicit I'd suggest still doing the above, but adding a second body matcher with a JSONPath expression stating what the object should contain.

Related

How to make postman assertion on an array

"thing": [
{
"id": 1,
}
]
How do I assert that thing is an array with an object that contains ID
I tried
expect(response.thing).to.deep.include('id');
But that doesn't work
If you want to check:
the array contains String id
pm.expect(JSON.stringify(response.thing)).to.deep.include('id');
a nested object has key id
pm.expect(response.thing[0]).to.have.keys('id');

how to obtain data argument to provide to mustache template in moodle

In moodle (4.0) I have the need to call
$this->output->render_from_template('core_courseformat/local/content/section/cmlist', $sectionData);
From within a renderer. The goal is to render the normal, native, cmlist component in a particular place on the page. But the way that I am currently getting the value of $section does not seem to work. My template renders nothing. I can see from the github source that this template expects data in this format:
Example context (json):
{
"cms": [
{
"cmitem": {
"cmformat": {
"cmname": "<a class=\"aalink\" href=\"#\"><span class=\"instancename\">Forum example</span></a>",
"hasname": "true"
},
"id": 3,
"module": "forum",
"extraclasses": "newmessages"
}
},
{
"cmitem": {
"cmformat": {
"cmname": "<a class=\"aalink\" href=\"#\"><span class=\"instancename\">Assign example</span></a>",
"hasname": "true"
},
"id": 4,
"module": "assign",
"extraclasses": ""
}
}
],
"hascms": true,
"showmovehere": true,
"movingstr": "Moving this activity: folder example",
"cancelcopyurl": "#",
"movetosectionurl": "#",
"strmovefull": "Move 'folder example' to this location"
}
}}
https://github.com/moodle/moodle/blob/1d99ba19a21d57e9f1ed4211a8eeee00e50b7baf/course/format/templates/local/content/section/cmlist.mustache
But here's the challenge. How do I get an object in that format with the data needed to feed the template so it can render the correct CM list items?
Currently I am tring:
$sectionData = get_fast_modinfo($course->id)->get_section_info($section);
But it doesn't seem to return the data structured in the right way.
Any help appreciated.
You can use the function export_for_template of cmlist render class.
Something like this:
$cmlist = new \core_courseformat\output\local\content\section($format, $section);
$data->cmlist = $cmlist->export_for_template($OUTPUT);
and then send the data to the template.
I recommend you imitate the behavior of a moodle as he performs here:
https://github.com/moodle/moodle/blob/7ce003b666a66b465ce9335f430a6e4d3535a7f1/course/format/classes/output/local/content/section.php#L223

How to change json object name without changing its values in C++?

I'm using json for modern c++.
And I have a json file which contains some data like:
{
"London": {
"Adress": "londonas iela 123",
"Name": "London",
"Shortname": "LL"
},
"Riga": {
"Adrese": "lidostas iela 1",
"Name": "Riga",
"Shortname": "RIX"
}
And I found out a way to modify the values of "Adrese", "Name", "Shortname".
As you can see I have "name" and key element name set to the same thing.
But I need to change both the key element and value "name".
So at the end when somehow in the code I modify it, it would look like:
{
"Something_New": {
"Adress": "londonas iela 123",
"Name": "Something_New",
"Shortname": "LL"
},
"Riga": {
"Adrese": "lidostas iela 1",
"Name": "Riga",
"Shortname": "RIX"
}
I've tried:
/other_code/
json j
/functions_for_opening_json file/
j["London"]["Name"] = "Something_New"; //this changes the value "name"
j["London"] = "Something_New"; //But this replaces "London" with
"Something_new" and deletes all of its inside values.
Then I tried something like:
for(auto& el : j.items()){
if(el.key() == "London"){
el.key() = "Something_New";}
}
But that didn't work either.
I would like something like j["London"] = "Something_new", and for it to keep all the values that originally was for "London".
The value associated with key "London" is the entire subtree json object containing the other 3 keys with their values. This line j["London"] = "Something_New"; does not change the key, "London" but its value. So you end up with the pair "London" : "Something new", overwriting the json subtree object. The keys are stored internally as std::map . Therefore you can't simply rename a key like that. Try:
void change_key(json &j, const std::string& oldKey, const std::string& newKey)
{
auto itr = j.find(oldKey); // try catch this, handle case when key is not found
std::swap(j[newKey], itr.value());
object.erase(itr);
}
And then
change_key(j, "London", "Something_New");

Strongloop Looback Where Or filter in REST syntax

I need to query where descr like 'xxx' or short_descr like 'xxx'
I know how to do it using:
{"where": {
"or": [
{"description": {"like": "xxx"}},
{"short_description": {"like": "xxx"}}
}
}
}
but need to add query params in REST syntax.
I'm trying:
params['filter[where][or]'] = JSON.stringify([
{ "description": { "like": "xxx" } },
{ "short_description": { "like": "xxx" } }
])
with The or operator has invalid clauses result.
Here is an example (I used 'desc' instead of 'description'):
http://localhost:3000/api/cats?filter[where][or][0][desc][like]=foo&filter[where][or][1][short_desc][like]=goo
So the important parts are this:
First, you need to give an index to each part of the OR clause. Note the first one is 0, then 1.
Secondly - um... I thought I had more, but that's pretty much it.
More information on WHERE filters: https://docs.strongloop.com/display/LB/Where+filter

MongoDB - strip non numeric characters in field

I have a field of phone numbers where a random variety of separators have been used, such as:
932-555-1515
951.555.1255
(952) 555-1414
I would like to go through each field that already exists and remove the non numeric characters.
Is that possible?
Whether or not it gets stored as an integer or as a string of numbers, I don't care either way. It will only be used for display purposes.
You'll have to iterate over all your docs in code and use a regex replace to clean up the strings.
Here's how you'd do it in the mongo shell for a test collection with a phone field that needs to be cleaned up.
db.test.find().forEach(function(doc) {
doc.phone = doc.phone.replace(/[^0-9]/g, '');
db.test.save(doc);
});
Based on the previous example by #JohnnyHK, I added regex also to the find query:
/*
MongoDB: Find by regular expression and run regex replace on results
*/
db.test.find({"url": { $regex: 'http:\/\/' }}).forEach(function(doc) {
doc.url = doc.url.replace(/http:\/\/www\.url\.com/g, 'http://another.url.com');
db.test.save(doc);
});
Starting in Mongo 4.4, the $function aggregation operator allows applying a custom javascript function to implement behaviour not supported by the MongoDB Query Language.
And coupled with improvements made to db.collection.update() in Mongo 4.2 that can accept an aggregation pipeline, allowing the update of a field based on its own value,
We can manipulate and update a field in ways the language doesn't easily permit and avoid an inefficient find/foreach pattern:
// { "x" : "932-555-1515", "y" : 3 }
// { "x" : "951.555.1255", "y" : 7 }
// { "x" : "(952) 555-1414", "y" : 6 }
db.collection.updateMany(
{ "x": { $regex: /[^0-9]/g } },
[{ $set:
{ "x":
{ $function: {
body: function(x) { return x.replace(/[^0-9]/g, ''); },
args: ["$x"],
lang: "js"
}}
}
}
])
// { "x" : "9325551515", "y" : 3 }
// { "x" : "9515551255", "y" : 7 }
// { "x" : "9525551414", "y" : 6 }
The update consist of:
a match query { "x": { $regex: /[^0-9]/g } }, filtering documents to update (in our case any document that contains non-numeric characters in the field we're interested on updating).
an update aggreation pipeline [ { $set: { active: { $eq: [ "$a", "Hello" ] } } } ] (note the squared brackets signifying the use of an aggregation pipeline). $set is a new aggregation operator and an alias for $addFields.
$function takes 3 parameters:
body, which is the function to apply, whose parameter is the string to modify. The function here simply consists in replacing characters matching the regex with empty characters.
args, which contains the fields from the record that the body function takes as parameter. In our case, "$x".
lang, which is the language in which the body function is written. Only js is currently available.
in mongodb version 4.2 you have regexFind project operator which can be used together with substr inside an aggregation without looping through all the documents in client