I'm trying to set default value for an array in jschema with swagger. Below is the example schema.
'Myobj': {
'type': 'object',
'title': 'Myobj',
'description': 'Some text',
'properties': {
'outputForms': {
'type': 'array',
'description': 'Some text',
'default': 'two',
'items': {
'type': 'string',
'enum': ['one','two'],
},
'maxItems': 4,
'uniqueItems': true,
'additionalItems': false
}
}
}
This does not work,what am I doing wrong ?
I will be grateful for any help.
Change
'default': 'two',
to
'default': ['two'],
The square brackets [] are used to denote an array.
Also, remove additionalProperties. In Swagger, the meaning of this key is different - it's an object (not a boolean) and is used to define a map / dictionary.
Related
I have my array list name tableRows
var tableRows = [
[
{
'ColumnName': 'Checkbox'
},
{
'ColumnName': 'TicketNumber',
'Type': 'text',
'Text': 20173100021
},
{
'ColumnName': 'Type',
'Type': 'text',
'Text': 'Project'
},
{
'ColumnName': 'Edits',
'Type': 'text',
},
{
'ColumnName': 'Name1',
'Type': 'text',
'CompanyName': 'CompanyA'
},
{
'ColumnName': 'Name2',
'Type': 'text',
'CompanyName': 'CompanyB'
}
]
];
how could I search CompanyName even my input is not exact for example I only typed "Comp", all CompanyName will display then if I typed "CompanyA", all with the same name will display in console.log. I used JavaScript by the way. Thanks!
You can use string.inlcudes() for this:
var tableRows = [[{'ColumnName': 'Checkbox'},{'ColumnName': 'TicketNumber','Type': 'text','Text': 20173100021},{'ColumnName': 'Type','Type': 'text','Text': 'Project'},{'ColumnName': 'Edits','Type': 'text',},{'ColumnName': 'Name1','Type': 'text','CompanyName': 'CompanyA'},{'ColumnName': 'Name2','Type': 'text','CompanyName': 'CompanyB'}]];
console.log(tableRows[0].filter(item => item.CompanyName && item.CompanyName.toLowerCase().includes("coMP".toLowerCase())))
console.log(tableRows[0].filter(item => item.CompanyName && item.CompanyName.includes("A")))
There are also other methods like startsWith and match() that can give you a lot of control over how and what is matched.
Currently, I am working on a desktop application that allows users to click on buttons to run terminal commands. I have nearly finished making this in electron, though I have run into one issue that I cannot work around.
My application is made up of two files: main.js and index.html. The javascript file displays index.html, includes the needed electron packages, and holds all of the functions called by index.html. One of the packages that I am using is Electron-Preferences. This allows me to create a simple window to store user preferences in a JSON file as well as access and change them. I have one function that allows the user to change one preference on the fly, however, I am having issues calling this function from the index.html file.
In the main.js file, an object called preferences is defined. In my html file, however, it says this object is not defined, thus it cannot run the program (I am including the main.js in the html file through a script src tag). Is there any way to include the preferences object, or some way to restructure the code so that I can call the function from a separate file?
Thank you in advanced!
Function from main.js:
function setNetworkMain(){
const network = preferences.value('blockchain.network');
if(network === "testing"){
preferences.value('blockchain.network', 'main');
}
};
Preferences Object:
const preferences = new ElectronPreferences({
/**
* Where should preferences be saved?
*/
'dataStore': prefLoc,
/**
* Default values.
*/
'defaults': {
'markdown': {
'auto_format_links': true,
'show_gutter': false
},
'preview': {
'show': true
},
'drawer': {
'show': true
},
"blockchain": {
"network": "main"
},
"update": {
"auto_update": true
},
"directory": {
"folder": dataDir
},
},
/**
* If the `onLoad` method is specified, this function will be called immediately after
* preferences are loaded for the first time. The return value of this method will be stored as the
* preferences object.
*/
'onLoad': (preferences) => {
// ...
return preferences;
},
/**
* The preferences window is divided into sections. Each section has a label, an icon, and one or
* more fields associated with it. Each section should also be given a unique ID.
*/
'sections': [
{
'id': 'blockchain',
'label': 'Blockchain Settings',
'icon': 'settings-gear-63',
'form': {
'groups': [
{
'label': 'Blockchain Settings',
'fields': [
{
'heading': 'Blockchain Network',
'content': "<p>Text</p>",
'type': 'message',
},
{
'key': 'network',
'type': 'radio',
'options': [
{'label': 'Main', 'value': 'main'},
{'label': 'Testing', 'value': 'testing'},
],
'help': 'Select which Blockchain you would like to use.'
}
]
}
]
}
},
{
'id': 'update',
'label': 'Update Settings',
'icon': 'square-download',
'form': {
'groups': [
{
/**
* Group heading is optional.
*/
'label': 'Update Settings',
'fields': [
{
'label': 'How would you like to check for updates?',
'key': 'auto_update',
'type': 'radio',
'options': [
{'label': 'Automatically check for updates', 'value': true},
{'label': 'Manually check for updates', 'value': false},
],
'help': 'Note: Automatic updates will be installed automatically.'
},
]
}
]
}
},
{
'id': 'directory',
'label': 'File Directory',
'icon': 'folder-15',
'form': {
'groups': [
{
'label': 'File Directory',
'fields': [
{
'label': 'Blockchain storage directory',
'key': 'folder',
'type': 'directory',
'help': 'Text.'
}
]
}
]
}
},
{
'id': 'about',
'label': 'About',
'icon': 'badge-13',
'form': {
'groups': [
{
'label': 'About',
'fields': [
{
'label': 'description',
'heading': 'Description',
'content': "<p>Text</p>",
'type': 'message',
},
{
'label': 'electron',
'heading': 'Electron',
'content': "<p>https://electronjs.org/</p>",
'type': 'message',
},
]
}
]
}
}
]
});
Hi I've a litle problem with selectize, namely how to add two lists and he first one should be selected by default and enduser can select items from second list in same form. Below you can find my code. If I add two times options selectize takes second one only).
$(document).ready(function(){
lol = "lol1 , lol2 , lol3"
var lol = lol.split(',');
var lol = lol.map(function(x) { return { item: x}; });
console.log(lol)
console.log(typeof(lol))
wtf = "wtf1 , wtf2 , wtf3"
var wtf = wtf.split(',');
var wtf = wtf.map(function(x) { return { item: x}; });
console.log(wtf)
console.log(typeof(wtf))
$('#show_tags').selectize({
plugins: ['remove_button', 'restore_on_backspace'],
select: true,
delimiter: ',',
maxItems: null,
options: lol,
options: wtf,
labelField: 'item',
valueField: 'item',
searchField: 'item',
create: true
});
});
Ideas?
You need to use items to provide an array of values for the options that should be selected by default (instead of using two options arrays). Option values are determined by your valueField setting.
For example:
$('#select-id').selectize({
items: ['1', '2'], // preselected options values
options: [
{ value: '1', name: 'Item 1' }, // this option will be preselected
{ value: '2', name: 'Item 2' }, // this option will be preselected
{ value: '3', name: 'Item 3' },
{ value: '4', name: 'Item 4' }
],
valueField: 'value',
labelField: 'name',
searchField: ['name'],
delimiter: ',',
select: true,
create: true,
maxItems: null
});
is possible to validate JSON, if value of object is true, then this object is valid, and if Obj2.included == true is valid, if Obj1.included == true ?
This is small piece of schema:
'attachments': {
'type': 'object',
'properties': {
'ZalA': {
'type': 'object',
'properties': {
'included': {
'type': 'boolean'
},
'version': {
'type': 'integer'
}
},
'required': [
'included',
'version'
]
},
'ZalB': {
'type': 'object',
'properties': {
'version': {
'type': 'integer'
},
'included': {
'type': 'boolean'
},
'required': [
'included',
'version'
]
}
}
}
}
I would like to check:
if ZalA.included == true, then valid.
if ZalA.included == true and ZalB.included == true, then valid.
if ZalA.included == false and ZalB.included == true, then invalid.
Is it possible to check these constraints with tv4 JSON validator ?
I've got a solution for you. But first of all you had a little mistake in your schema, because of required-property that was within properties:
'ZalB': {
'type': 'object',
'properties': {
'version': {
'type': 'integer'
},
'included': {
'type': 'boolean'
},
'required': [
'included',
'version'
]
}
}
When you use it you have to define it outside before or after properties. As you have done this with ZalA :) otherwise it does not work.
Now to your answer, I did a little experiment with this very interesting validator and came up with this:
// schema to be used for validating
var schema = {
'type': 'object',
'properties': {
'ZalA': {
'type': 'object',
'properties': {
'included': {
'type': 'boolean',
'enum': [
true
]
},
'version': {
'type': 'integer'
}
},
'required': [
'included',
'version'
]
},
'ZalB': {
'type': 'object',
'properties': {
'version': {
'type': 'integer'
},
'included': {
'type': 'boolean',
'enum': [
true
]
}
},
'required': [
'included',
'version'
]
},
'required': [
'ZalA'
],
}
};
// data to be checked against
var data = {
'ZalA': {
'version': 1,
'included': true
},
'ZalB': {
'version': 2,
'included': true
}
}
tv4.validateResult(data, schema); // Object { missing=[0], valid=true, error=null}
Schema has to be configured so that it matches your check-list:
if ZalA.included == true, then valid.
'required': [
'ZalA'
],
Requires ZalA at the end of schema after properties so that ZalA has to be present, so you can repeat this option as often as you want in each level. But this is not enougth to fulfill your check-list. Next configurations are:
'required': [
'included',
'version'
]
plus
'included': {
'type': 'boolean',
'enum': [true]
},
included-property (and actually version-property as well, it was already in your question) of ZalA must be present and true so that ZalA can be considered valid. You can define an array of different types to check whether the property has a certain value or you can use pattern-option.
These configurations are applied for ZalB too but with one difference:
'required': [
'ZalA'
],
Only ZalA is required and not ZalB at the end.
And we are done! With these configurations all your next conditions are fulfilled:
if ZalA.included == true and ZalB.included == true, then valid.
if ZalA.included == false and ZalB.included == true, then invalid.
And if ZalB.included is granted to be false as well as true then just do this:
'enum': [
true, false
]
Or omit enum-option completely so that it must be a boolean on the first place.
This is really a good validator. Thanks for your question, I'll use it for furture projects.
P.S. You may can spare yourself to define a second schema for ZalB and just referencing(using $ref) to the schema for ZalA, but I did not test this. On the other hand you could use this little schema:
var schema = {
'type': 'object',
'properties': {
'included': {
'type': 'boolean',
'enum': [
true
]
},
'version': {
'type': 'integer'
}
},
'required': [
'included',
'version'
]
};
And use it in this way:
// a bundle of objects to be checked
var data = [{
'version': 1,
'included': true
},{
'version': 2,
'included': true
}];
// iterate through each object
for(var i=0; i < data.length;i++){
var obj = data[i];
// validate each object
var result = tv4.validateResult(obj, schema);
if(!result.valid){
console.log("not valid: ",result.error);
}
}
I just speak for myself but for me this is the most important side of the validator-documentation. Because it contains all options you can define for certain properties to be valided:
http://json-schema.org/latest/json-schema-validation.html
I've got a bunch of records whose names are like "Itemtype #", and when they are outputted into a tree view, they're sorted incorrectly, like so:
Item 1
Item 10
Item 11
Item 12
Item 13
Item 2
Item 3
Item 4
My model fields are defined as follows, and I am sorting on "Name":
fields: [
{ name: 'Id', defaultValue: 0, type: 'int', mapping: 'Id' },
{ name: 'Name', defaultValue: '', type: 'string', mapping: 'Name', sortType: Ext.data.SortTypes.asUCString },
{ name: 'Type', defaultValue: 0, type: 'int', mapping: 'Type' },
{ name: 'CreationDate', type: 'date', mapping: 'CreationDate' }
],
Can anybody point me in the right direction? I'm using extjs 4.0.5
In store you have to set up how the data will display by sorters property:
var store = Ext.create('Ext.data.JsonStore', {
.
.
.
remoteSort: false, //true for server sorting
sorters: [{
property: 'Name',
direction: 'DESC' // or 'ASC'
}],
.
.
.
})
Try calling TreeStore.sort with a sorter config with a sorting callback. A simple field sort won't do in this case because the naive alpha sort isn't what you want. The example at the top of the Ext.util.Sorter doc shows how to do it in a store, but you can just as easily add it to the sorters param of your model.