Conditional nested data in Strapi - jsonschema

I'm trying to set up a strapi CMS which will allow a user to create an entry with a set of conditional tags.
The requirement is that they can create a product which has tags. Those tags are to be selected from a pre-defined nested list that looks something like this:
- Category 1
- Item 1
- Item 2
- Item 3
- Category 2
- Item 4
- Item 5
- Item 6
- Category 3
- Item 7
- Item 8
- Item 9
Each product can have multiple categories and each category can have multiple items. They need to select at least one category and for every category they select, they need to select at least one item.
So, for example, they could create a product which had a selection that looked like this:
- Category 2
- Item 4
- Category 3
- Item 8
- Item 9
I attempted to create a component which did sort of work but the user was able to select any item they wanted from any category instead of that item being limited to the parent category.
Is this something Strapi is capable of? If so, how do I go about it?
For reference, here are the schemas I set up for the category and items collections:
{
"kind": "collectionType",
"collectionName": "category",
"info": {
"singularName": "categories",
"pluralName": "category",
"displayName": "categories",
"description": ""
},
"options": {
"draftAndPublish": true
},
"pluginOptions": {},
"attributes": {
"Name": {
"type": "string",
"required": true
},
"categories": {
"type": "relation",
"relation": "oneToMany",
"target": "api::category.category",
"mappedBy": "category"
}
}
}
{
"kind": "collectionType",
"collectionName": "items",
"info": {
"singularName": "item",
"pluralName": "items",
"displayName": "items",
"description": ""
},
"options": {
"draftAndPublish": true
},
"pluginOptions": {},
"attributes": {
"Name": {
"type": "string",
"required": true
},
"domain": {
"type": "relation",
"relation": "manyToOne",
"target": "api::categories.categories",
"inversedBy": "items"
}
}
}
and here is the component I made:
{
"collectionName": "components_tags_tag_selectors",
"info": {
"displayName": "Tag Selector",
"icon": "tags",
"description": ""
},
"options": {},
"attributes": {
"domain": {
"type": "relation",
"relation": "oneToOne",
"target": "api::categories.categories"
},
"categories": {
"type": "relation",
"relation": "oneToMany",
"target": "api::item.item"
}
}
}

Related

Karate: How to remove dynamic element based on value from JSON?

I have a requirement, in the below JSON I have to delete all the id elements,
[
{
"id": "0a7936ed",
"code": "test",
"label": "test",
"type": "sell"
},
{
"id": "7bc1909b2",
"code": "test2",
"label": "test2",
"type": "Buy"
}
]
My JSON should be as below,
[
{
"code": "test",
"label": "test",
"type": "sell"
},
{
"code": "test2",
"label": "test2",
"type": "Buy"
}
]
Standard JS Array operations will work in Karate 1.0 onwards:
* def dest = source.map(x => { delete x.id; return x })
For older versions of Karate, a hint is that you can loop over any JS object key-values using karate.forEach() and you could write conditional logic to ignore id etc.

How to filter Cosmos DB data based on value of an element in an array of values Using SQL API

I have a cosmosDB collection with below Data in it.
I have to find out the data only for EVENT named ABC and its value using SQL query.
[
{
"ID": "01XXXXX",
"EVENTS": [
{
"Name": "ABC",
"Value": 0
},
{
"Name": "XYZ",
"Value": 4
},
{
"Name": "PQR",
"Value": 5
}
]
},
{
"ID": "02XXXXX",
"EVENTS": [
{
"Name": "ABC",
"Value": 1
},
{
"Name": "XYZ",
"Value": 2
},
{
"Name": "PQR",
"Value": 3
}
]
}
]
I have tried the below code but it is not working since EVENT is an array.
SELECT * FROM c where c.EVENTS.Name = 'ABC'
Is there any way to find filter out the data only with Event Name as ABC using SQL?
Try using join
SELECT c FROM c
join l in c.EVENTS
where l.Name = 'ABC'

Google Big Query - loading a csv file - Error while reading table

I'm trying to upload a report in CSV fotmat to Google Big Query.
The report contains the following column names:
Adjustment Type; Day; Country; Asset ID; Asset Title; Asset Labels; Asset
Channel ID; Asset Type; Custom ID; TMS; EIDR; UPC; Season; Episode
Title; Episode Number; Director; Studio; Owned Views; YouTube Revenue Split
: Auction; YouTube Revenue Split : Reserved; YouTube Revenue Split :
Partner Sold YouTube Served; YouTube Revenue Split : Partner Sold
Partner Served; YouTube Revenue Split; Partner Revenue : Auction; Partner
Revenue : Reserved; Partner Revenue : Partner Sold YouTube
Served; Partner Revenue : Partner Sold Partner Served; Partner Revenue
After creating the table for this report, the column names and types look as follows:
[
{
"name": "Adjustment_Type",
"type": "STRING"
},
{
"name": "Day",
"type": "STRING"
},
{
"name": "Country",
"type": "STRING"
},
{
"name": "Asset_ID",
"type": "STRING"
},
{
"name": "Asset_Title",
"type": "STRING"
},
{
"name": "Asset_Labels",
"type": "STRING"
},
{
"name": "Asset_Channel_ID",
"type": "STRING"
},
{
"name": "Asset_Type",
"type": "STRING"
},
{
"name": "Custom_ID",
"type": "STRING"
},
{
"name": "TMS",
"type": "STRING"
},
{
"name": "EIDR",
"type": "STRING"
},
{
"name": "UPC",
"type": "STRING"
},
{
"name": "Season",
"type": "STRING"
},
{
"name": "Episode_Title",
"type": "STRING"
},
{
"name": "Episode_Number",
"type": "STRING"
},
{
"name": "Director",
"type": "STRING"
},
{
"name": "Studio",
"type": "STRING"
},
{
"name": "Owned_Views",
"type": "STRING"
},
{
"name": "YouTube_Revenue_Split___Auction",
"type": "FLOAT"
},
{
"name": "YouTube_Revenue_Split___Reserved",
"type": "FLOAT"
},
{
"name": "YouTube_Revenue_Split___Partner_Sold_YouTube_Served",
"type": "FLOAT"
},
{
"name": "YouTube_Revenue_Split___Partner_Sold_Partner_Served",
"type": "FLOAT"
},
{
"name": "YouTube_Revenue_Split",
"type": "FLOAT"
},
{
"name": "Partner_Revenue___Auction",
"type": "FLOAT"
},
{
"name": "Partner_Revenue___Reserved",
"type": "FLOAT"
},
{
"name": "Partner_Revenue___Partner_Sold_YouTube_Served",
"type": "FLOAT"
},
{
"name": "Partner_Revenue___Partner_Sold_Partner_Served",
"type": "FLOAT"
},
{
"name": "Partner_Revenue",
"type": "FLOAT"
}
]
While trying to query the table, I'm getting the following error message:
Could not parse 'YouTube Revenue Split : Auction' as double for field
YouTube_Revenue_Split___Auction (position 18) starting at location 0
(error code: invalid)
Any idea, what could be the reason for this error?
I've been able to replicate the error. In my case it appears when trying to load the CSV to BigQuery. The CSV has the string YouTube Revenue Split : Auction where should be float.
What I suspect is happening is that your CSV file has the column headers in it and you are not skipping them when loading the file to BigQuery. This causes that, when the import process gets to the YouTube_Revenue_Split___Auction field (position 18), expects to insert float, but instead it tries to insert the column header, YouTube Revenue Split : Auction, which is a string that cannot be parsed correctly.
Try re-loading the CSV but remove the headers first (or skip them using the Header rows to skip option).
If my supossition is wrong and this doesn't apply, update your question by adding the query that produces the error.
Adding on to what Guillermo said, you can also have Big Query automatically identify the headers and field types when you upload your files in CSV
I had a similar error and solved it by replacing the semi-colons with commas.
You can use a regex for this or use this great online text replacement tool that I found - https://onlinetexttools.com/replace-text

find object in nested array with lodash

I have json data similar to this:
{
"Sections": [
{
"Categories": [
{
"Name": "Book",
"Id": 1,
"Options": [
{
"Name": "AAAA",
"OptionId": 111
},
"Selected": 0
},
{
"Name": "Car",
"Id": 2,
"Options": [
{
"Name": "BBB",
"OptionId": 222
},
"Selected": 0
},
],
"SectionName": "Main"
},
... more sections like the one above
]
}
Given this data, I want to find a category inside a section based on its (Category) Id, and set its selected option, I tried this, but couldn't get it to work....Note Category Id will be unique in the whole data set.
_.find(model.Sections, { Categories: [ { Id: catId } ]});
According to your data model, it looks like you're trying to find an element that is inside a matrix: Sections can have multiple Categories and a Category can have multiple types (car, book...).
I'm afraid there isn't a function in lodash that allows a deep find, you'll have to implement it the 'traditional' way (a couple of fors).
I provide this solution that is a bit more 'functional flavoured' than the traditional nested fors. It also takes advantage of the fact that when you explicitly return false inside a forEach, the loop finishes. Thus, once an element with the provided id is found, the loop is ended and the element returned (if it's not found, undefined is returned instead).
Hope it helps.
const findCategoryById = (sections, id) => {
var category;
_.forEach(sections, (section) => {
category = _.find(section.Categories, ['Id', id]);
return _.isUndefined(category);
});
return category;
};
const ex = {
"Sections": [{
"Categories": [{
"Name": "Book",
"Id": 1,
"Options": [{
"Name": "AAAA",
"OptionId": 111
}],
"Selected": 0
},
{
"Name": "Car",
"Id": 2,
"Options": [{
"Name": "BBB",
"OptionId": 222
}],
"Selected": 0
}
],
"SectionName": "Main"
}]
};
console.log(findCategoryById(ex.Sections, 2));
<script src="https://cdn.jsdelivr.net/npm/lodash#4.17.5/lodash.min.js"></script>

How to invert the MQL query (for freebase)?

I am trying to list all the types for a particular id:
{
"id": "/en/sony",
"type": [{
"name": "Topic",
"id": null
}]
}
This query giving me the following result:
http://tinyurl.com/lubavey
{
"result": {
"type": [
{
"id": "/common/topic",
"name": "Topic"
},
{
"id": "/base/audiobase/topic",
"name": "Topic"
},
{
"id": "/base/fblinux/topic",
"name": "Topic"
},
{
"id": "/base/digitalcameras/topic",
"name": "Topic"
},
{
"id": "/base/popstra/topic",
"name": "Topic"
},
{
"id": "/base/televisions/topic",
"name": "Topic"
},
{
"id": "/base/ps3games/topic",
"name": "Topic"
},
{
"id": "/base/filmcameras/topic",
"name": "Topic"
},
{
"id": "/m/04mny2g",
"name": "Topic"
}
],
"id": "/en/sony"
}
}
I want exactly the opposite result. I want all the types which do not have name as "Topic" with them.
How can I achieve this? I tried to use ! operator with property name which is suggested in reference guide of MQL, but it's giving me error:
"Can't use unqualified property names with ! reversing".
What should I do to remove this error with ! and to obtain opposite result of the query?
Try with !=:
{
"id": "/en/sony",
"type": [{
"name!=": "Topic",
"id": null
}]
}
The != operator says that the constrained property can be anything but
the specified value. (It does require that the property be something,
however: it does not match object for which the property is null.)
Read more about != operator here: http://wiki.freebase.com/wiki/MQL_operators#The_.22but_not.22_Operator_.21.3D