$ref in Json Schema with specified values - javascript

I'm trying to build a schema that references another schema. However in my schema I would like to specify that certain properties specified by the referenced schema have particular values without modifying that referenced schema.
For example with a schema like this:
{
"definitions" : {
"name" : {
"properties" : {
"firstName" : { "type" : "string" },
"lastName" : { "type" : "string" }
},
"required" : ["firstName", "lastName"]
}
},
"properties" : {
"clientName" : { "$ref" : "#/definitions/name" }
},
"required" : [ "clientName" ]
}
I would somehow like to validate objects that have firstName equal to "Bob".
So this should validate:
{
"clientName" : {
"firstName" : "Bob",
"lastName" : "Doe"
}
}
But this should not:
{
"clientName" : {
"firstName" : "Fred",
"lastName" : "Doe"
}
}
Without modifying the "definitions" block in my schema, how can I express this restriction?

Related

WebStorm autocompletion based on custom schema json files

I have some json schema files describing the structure of my objects. Here is an example:
{
"$schema" : "https://json-schema.org/draft/2019-09/schema",
"$ref" : "#/$defs/AdaugaPersoana",
"$defs" : {
"AdaugaPersoana" : {
"type" : "object",
"properties" : {
"id" : {
"type" : "string"
},
"numeComplet" : {
"$ref" : "#/$defs/NumeComplet"
},
"stare" : {
"$ref" : "#/$defs/Stare"
}
}
},
"NumeComplet" : {
"type" : "object",
"properties" : {
"nume" : {
"type" : "string"
},
"prenume" : {
"type" : "string"
},
"factory()" : {
"type" : "string"
}
},
"required" : [ "nume" ]
},
"Stare" : {
"type" : "string",
"enum" : [ "ACTIVAT", "DEACTIVAT" ]
}
}
}
Is there any way that WebStorm can use this schema to autocomplete when using a JavaScript variable?
Example JS code:
/** #type AdaugaPersoana */
const someVar = {};
someVar.numeComplet.nume //this should be autocompleted

How to filter out text fields in a complex query?

I've got a lot of doc filters on my UI (date ranges, checkboxes, input fields), so the query is generated dynamically - that's why I decided to create a boolean query, and push everything to must array. This is the example of my request:
const {
body: {
hits
}
} = await esclient.search({
from: filterQuery.page || 0,
size: filterQuery.limit || 1000,
index,
body: query
});
Checkboxes (I used additional bool.should inside must array) and date range work perfectly, but term/match filtering is not working at all:
{
"query": {
"bool": {
"must": [
{"match": { "issueNumber": "TEST-10" }}
]
}
}
}
The query above gives me all the documents from the index that contains "TEST" (with their scores), if I change match to term - it returns an empty array.
As my field is of a type 'text', I've also tried filter query - ES still gives all the documents with 'TEST' word:
{
"query": {
"bool": {
"must": [
{
"bool": {
"filter": {
"match": {"issueNumber": "TEST-10"}
}
}
}
]
}
}
}
This is how my hit looks like:
{
"_index" : "test_elastic",
"_type" : "_doc",
"_id" : "bj213hj2gghg213",
"_score" : 0.0,
"_source" : {
"date" : "2019-11-26T13:27:01.586Z",
"country" : "US",
"issueNumber" : "TEST-10",
}
Can someone give me input on how to filter the docs properly in complex query?
This is the structure of my index:
{
"test_elasticsearch" : {
"aliases" : { },
"mappings" : {
"properties" : {
"country" : {
"type" : "text"
},
"date" : {
"type" : "date"
},
"issueNumber" : {
"type" : "text"
}
}
},
"settings" : {
"index" : {
"creation_date" : "1574759226800",
"number_of_shards" : "1",
"number_of_replicas" : "1",
"uuid" : "PTDsdadasd-ERERER",
"version" : {
"created" : "7040299"
},
"provided_name" : "logs"
}
}
}
}
Ok, the problem is that your issueNumber field has not the right type, it should be keyword instead of text if your goal is to make exact searches on it. Same for country. Modify your mapping like this:
"properties" : {
"country" : {
"type" : "keyword"
},
"date" : {
"type" : "date"
},
"issueNumber" : {
"type" : "keyword"
}
}
Then reindex your data and your queries will work.

How to parse this datasnapshot using Firebase and JavaScript?

I have this data structure
{
"job-requests" : {
"pending" : {
"-KkyZGfqmiIVryyLAZpD" : {
"job_details" : "asd",
"job_type" : "Repair Required",
"location" : "123",
"location_lat" : 14.164633210106128,
"location_lng" : 121.24110514763743,
"timestamp" : 1495698316411
}
}
},
"office-info" : {
"123" : {
"office_acronym" : "123",
"office_contact_number" : "123-1234",
"office_current_head" : "None",
"office_name" : "123",
"office_parent_unit" : "123"
}
},
"office-location-list" : {
"123" : {
"location_lat" : 14.164633210106128,
"location_lng" : 121.24110514763743
}
},
"users" : {
"MBR5o37xafUyuLw14Xqa1ku0Zui1" : {
"designation" : "staff",
"email" : "123#asd.com",
"given_name" : "23",
"last_name" : "123",
"password" : "1234567",
"timestamp" : 1495617328793
},
"Nwacy3ADczgLC85OvSAgUNEGMkx2" : {
"designation" : "staff",
"email" : "adsasd#asdas.com",
"given_name" : "122123",
"last_name" : "12",
"password" : "asdasdsadasd",
"timestamp" : 1495681430048
}
}
}
I will be needing the keys [pending, active, finished] along with the data of the newly added child. This is how I accessed Firebase
firebase.database ().ref ('job-requests').on ('child_added', (snapshot) => {
console.log (snapshot.key); // prints out [pending, active, finished]
console.log (snapshot.val()); // prints out an object
});
It prints this on the console:
I tried using JSON.parse (), snapshot.child (path), snapshot.field, and snapshot[field] but errors are thrown out. How do I do this?
Your code gets all job requests. Since those are in a hierarchy by their state, your code will need to handle this:
firebase.database ().ref ('job-requests').on ('child_added', (snapshot) => {
snapshot.forEach((stateSnapshot) => {
console.log(stateSnapshot.key); // 'pending', etc
stateSnapshot.forEach((jobSnapshot) => {
console.log(jobSnapshot.key);
console.log(jobSnapshot.val());
});
});
});

Updating array inside two object in mongod

I have a collection of the structure as follows:
collection name : "positions"
Structure
{
"_id" : "vtQ3tFXg8THF3TNBc",
"candidatesActions" : {
"sourced" : [ ],
},
"appFormObject" : {
"name" : "✶ mandatory",
"questions" : [
{
"qusId" : "qs-585494",
"type" : "simple",
"qus" : "Which was your previous company"
},
{
"qusId" : "qs-867766",
"type" : "yesNo",
"qus" : "Are you willing to relocate?",
"disqualify" : "true"
}
]
}
}
I want to update "qus" field of the above collection whose _id is "vtQ3tFXg8THF3TNBc" and "qusId" is "qs-585494".
Try following....
db.positions.update(
{_id: "vtQ3tFXg8THF3TNBc", "appFormObject.questions.qusId":"qs-585494"},
{$set:{"appFormObject.questions.$.qus": "this is updated value"}}
)
Use following query
db.positions.findAndModify({
query: { _id: "vtQ3tFXg8THF3TNBc", "appFormObject.questions.qusId":"qs-585494"} ,
update: { $set: { 'appFormObject.questions.$.qus': 'Brilliant Green' } },
});
Thanks

define an object in javascript

I am new to javascript.
I want to have an object with this structure.
Users = {
User1 : {
"name" : "name",
"id" : "123"
}
User2 : {
"name" : "name",
"id" : "123"
}
}
so I should define Users like this:
var Users = {};
How can I add new User to Users? and How can I read and write from users inside Users object like this:
// reading
User1_name = Users.User1.name;
// writing
Users.User1.name = "new name";
Your code is fine (except the missing comma #Philipp pointed), but you can use an array too:
var users = [
{
"name" : "name",
"id" : "123"
},
{
"name" : "name",
"id" : "123"
}
];
var userName = users[0].name;
users[0].name = "new name";
You can define users like this, you are just missing a comma
Users = {
User1 : {
"name" : "name",
"id" : "123"
} <--
User2 : {
"name" : "name",
"id" : "123"
}
}
Should be ( with var added for clarity )
var Users = {
User1 : {
"name" : "name",
"id" : "123"
},
User2 : {
"name" : "name",
"id" : "123"
}
}
Reading and writing can be done the way you described.
To add a new user, you could do something like this
Users["User3"] = {
"name" : "name3",
"id" : "1234"
}

Categories

Resources