Conditional Yup schema validation with array of objects - javascript

I would like to use different schemas for an array of objects based on a value.
items: [{
name: "a"
...
},
{
name: "b"
...
}]
How can I validate the item with name: "a" with another schema than item with name: "b" based on the value of name?

Related

Dynamo DB: How to update specific object inside an array in aws dynamodb?

Inside my companies table there is a company collection I have an array of users.
I need to change one of the users name ('David' to 'Solomon').
I don't mind changing to insert a new user object instead David.
But I don't mind to keep the same id and just change the name field from 'David' to 'Solomon'
company: {
id: cde9e41d-8d16-4054-907f-cc92a69d68e0
name: 'Gold Crowns Ltd',
users: [
{
id: abc9e41d-8d16-4054-907f-cc92a69d68e0
name: 'David' // <-------- I want to change 'David' to 'Solomon'
},
{
id: fgh9e41d-8d16-4054-907f-cc92a69d68e0
name: 'Saul'
},
],
}
Which option will work best and what should be the query in DynamoDB

How to filter data based on populated item's null

I do have object. For example -
{
active: true,
category: { _id: 1212, name: 'something', active: false},
name: "test",
}
My category object came from mongodb populated item. Now when I populated item I filtered it based on active=false. which returned null on category field.
For instance,
{
active: true,
category: null,
name: "test",
}
Now my main focus is to remove this object if category is null.
I did use like this -
match: { category: { $exists: true } },
populate: "category"
but it doesn't work because initially category object exists. But after populate and based on active status it's became null.
So what is the best way to remove this object if category is null.
N.B: I know it will work using aggregate. But I do not use aggregate because the query is too long. I have to change many things if I do aggregate. And populate is multilevel. So it will be a problem.
In your code you are looking for a field that "exists" even though its value is null, the field is there.
You could just search for the null value and it should work.
match: { category: null }

How to set custom CoreUI column names in Vue?

I have an array of item objects for the table. e.g.:
[{ name: 'Sam', age: 24 }]
I want to set custom field names like instead of column to be named as age, I want it to display column name as Id instead of age. So, basically how can I set custom column names?
Here is their documentation if that helps: https://coreui.io/vue/docs/components/table.html
Check fields props.
Using the fields attribute of the <CDataTable>, you can specify an array of fields to use for column names. That can be an array of strings or objects. When using the object syntax, you can specify various properties, such as the label of the column. See in the <CDataTable> docs:
key (required)(String) - define column name equal to item key.
label (String) - define visible label of column. If not defined, label will be generated automatically based on column name, by converting
This lets you name the column differently from the key. Here is an example where the table has column keys "id" and "name", but the label is "ID" and "Surname":
<div id="app">
<cdatatable
:items="items"
:fields="fields"
>
</cdatatable>
</div>
new Vue({
el: "#app",
data() {
return {
items: [
{ id: 1, name: 'Mary' },
{ id: 2, name: 'Bob' }
],
fields: [ // Array of column object data
{ key: 'id', label: 'ID' },
{ key: 'name', label: 'Surname' }
]
}
}
});

How to create tree view inside the dropdown using angular material?

Can anyone tell me, how to create a tree view inside the drop down. The drop down values will be getting from rest api call as json as follows. And subchild may contains one more level of child as well.
I have to do auto suggestion here to perform the filter from parent as well as the child level too.
VehicleList = [
{
parent: "audi",
child: [{
type: 'G-audiA',
subchild: [{
id: 1,
name: 'type audi A1'
}, {
id: 2,
name: 'type audi A2'
}]
}, {
type: 'G-audiB',
subchild: [{
id: 1,
name: 'type audi B1'
}, {
id: 2,
name: 'type audi B2'
}]
}]
}, {
parent: "bmw",
child: [{
type: 'G-bmwA',
subchild: [{
id: 1,
name: 'type bmw A1'
}, {
id: 2,
name: 'type bmw A2'
}]
}, {
type: 'G-bmwB',
subchild: [{
id: 1,
name: 'type bmw B1'
}, {
id: 2,
name: 'type bmw B2'
}]
}]
}]
Anyone help will be appreciated!!!
Based on the first example from the Angular Material Tree docs I managed to build up a drop-down with a tree structure inside like so:
The trick for displaying the tree is to add a disabled/empty option. I used it as a label. The tree is taken from their examples so I did not modify it at all, you can modify the node structure to match your own.
In order to display the selected items in the label of the drop-down, you can create a method that will return the selected items a string as their SelectionModel object has the selected property which would return all selected nodes.
/** The selection for checklist */
checklistSelection = new SelectionModel<TodoItemFlatNode>(
true /* multiple */
);
And in order to get the selected items from the tree:
return this.checklistSelection.selected.map(s => s.item).join(",");
For the filtering part I think you can look over this answer.
Hope this is helpful!
Stackblitz
Edit: If you select a child the parent gets selected too and added in the SelectionModel even if all its children are not selected. If you don't want this behaviour comment on the function descendantsPartiallySelected. This will not check the checkbox and so parents will not be added in SelectionModel unless all children are selected

Can a JSON Schema match values from an array?

Let's say I have the following data:
{
ids: ['12345'],
images: [{
id: '12345',
}]
}
I know that I can write a schema that can enforce that ids exist, is an array, and has a string value. I can also enforce that images have an id that is a string. But is there a way that I can enforce that the following two data blocks will fail validation using a JSON schema?
{
ids: [], // should fail because the id '12345' doesn't exist, and the images array is a different length than the ids array
images: [{
id: '12345',
}]
}
Or:
{
ids: ['54321'],// should fail because 12345 doesn't match any of the ids
images: [{
id: '12345',
}]
}

Categories

Resources