JSON tv4 object valid if true and if other object is present - javascript

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

Related

How to clear pre-defined blockly custom blocks in Angular

I have some problem initializing custom blocks
I created blockly custom blocks to modal
export const ConditionBlocklyBlock: Block[] = [
{
type: 'SUM_MAT',
message0: '%1 %2 %3',
colour: '#F88370',
tooltip: 'tooltip',
inputsInline: true,
args0: [
{
type: 'input_value',
name: 'first_value'
},
{
type: 'field_dropdown',
name: 'operator',
options: [
[ '>', '>' ],
[ '>=', '>=' ],
[ '<', '<' ],
[ '<=', '<=' ],
[ '==', '==' ]
]
},
{
type: 'input_value',
name: 'second_value'
}
],
output: "Boolean"
},
{
type: 'swapList',
message0: '%1 %2 %3',
colour: '#F88370',
tooltip: 'tooltip',
inputsInline: true,
args0: [
{
type: 'input_value',
name: 'first_value',
check: 'Boolean'
},
{
type: 'field_dropdown',
name: 'logical_operator',
options: [
[ 'AND', 'AND' ],
[ 'OR', 'OR' ],
]
},
{
type: 'input_value',
name: 'second_value',
check: 'Boolean'
}
],
output: 'Boolean'
}
];
const conditionBlocks: Block[] = ConditionBlocklyBlock;
Blockly.defineBlocksWithJsonArray(conditionBlocks);
When the modal is opened, the block is initialized.
The first time it is opened, it is normal, but from the second time on, the following log appears.
Block definition #77 in JSON array overwrites prior definition of "SUM".
Block definition #78 in JSON array overwrites prior definition of "SUM-MAT".
Block definition #79 in JSON array overwrites prior definition of "SUM_MV".
JsonOverwritesLog
Please give me some advice.

Live search through Array values

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.

How to set default value for an array jschema object?

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.

How to call a variable in a .js object that has a '-' in it

the object "user" in
client.on("chat", function (channel, user, message, self)
looks like this.
'badges': { 'broadcaster': '1', 'warcraft': 'horde' },
'color': '#FFFFFF',
'display-name': 'target',
'emotes': { '25': [ '0-4' ] },
'mod': true,
'room-id': '58355428',
'subscriber': false,
'turbo': true,
'user-id': '58355428',
'user-type': 'mod',
'emotes-raw': '25:0-4',
'badges-raw': 'broadcaster/1,warcraft/horde',
'username': 'schmoopiie',
'message-type': 'action'
I'm trying to
console.log("the user: ", user.display-name);
but that returns undefined. How can I call it?
Use bracket notation
var user = {
'badges': { 'broadcaster': '1', 'warcraft': 'horde' },
'color': '#FFFFFF',
'display-name': 'target',
'emotes': { '25': [ '0-4' ] },
'mod': true,
'room-id': '58355428',
'subscriber': false,
'turbo': true,
'user-id': '58355428',
'user-type': 'mod',
'emotes-raw': '25:0-4',
'badges-raw': 'broadcaster/1,warcraft/horde',
'username': 'schmoopiie',
'message-type': 'action'
};
console.log(user['display-name']);

extjs4.1 sort a store based on user defined criteria

Can someone help me to sort the store based on a user defined criteria?
Ext.define('Role', {
extend: 'Ext.data.Model',
fields: [
{name: 'role_id', type: 'int'},
{name: 'role_name', type: 'string'}
]
})
The role_name returned are admin,user,read-only,super
I would like to sort it as
read-only,user,admin,super.
You can sort it using the sort() method as follow:
Ext.define('Role', {
extend: 'Ext.data.Model',
fields: [
{name: 'role_id', type: 'int'},
{name: 'role_name', type: 'string'}
]
})
var store = Ext.create('Ext.data.Store', {
model: 'Role',
data: [ // non-sorted data
[ 1, 'user' ],
[ 2, 'super' ],
[ 3, 'read-only' ],
[ 4, 'admin' ]
]
});
// sort the store
store.sort([{
sorterFn: function(v1, v2) {
var order = ['read-only', 'user', 'admin', 'super'],
v1o = order.indexOf(v1.get('role_name')),
v2o = order.indexOf(v2.get('role_name'));
return v1o < v2o ? -1 : 1;;
}
}]);
console.log(store.data);
​You can see it working here: http://jsfiddle.net/lontivero/wGc2D/4/
Good luck!
This is covered in the documentation, use a custom sort type:
http://docs.sencha.com/ext-js/4-1/#!/api/Ext.data.Field-cfg-sortType
// current sort after sort we want
// +-+------+ +-+------+
// |1|First | |1|First |
// |2|Last | |3|Second|
// |3|Second| |2|Last |
// +-+------+ +-+------+
sortType: function(value) {
switch (value.toLowerCase()) // native toLowerCase():
{
case 'first': return 1;
case 'second': return 2;
default: return 3;
}
}

Categories