JSON schema - conditionally apply a $ref to a value based on the other value? - jsonschema

I am wanting to conditionally validate against a $ref based on another value in my schema.
items: {
type: 'object',
properties: {
kind: { type: 'string', enum: ['foo', 'bar'] },
//parameters: { $ref: 'foo.json#' } // This works
parameters: {
if: {
kind: 'foo'
},
then: {
$ref: 'foo.json#'
}
}
// also tried
if: {
kind: 'foo'
},
then: {
parameters: { $ref: 'foo.json#' }
}
I would like parameters to be validated against the foo.json reference whenever the value of kind is equal to foo (same with bar and bar.json). However the above is not working. Uncommenting out the commented section works so they are not equivalent.
How can I format this to conditionally apply a $ref to a value based on the other value?
I actually have about 10 different values for the type enum so if there is a cleaner way do this than if/else I am open.

Ah got it....
items: {
type: 'object',
properties: {
kind: { type: 'string', enum: ['foo', 'bar'] }
},
required: ['parameters'],
if: {
properties: { kind: { const: 'foo' } },
required: ['kind']
},
then: {
properties: {
parameters: {
$ref: 'fpp.json#'
}
}
}
It's helpful for me to think that whatever is in if and else are essentially merged with the main schema.

Related

Fastify and json schema validation

I'm trying to learn fastify throught the official documentation. I'm really intrested in the validation of an incoming post request with a json schema. Following the instructions i added to my routes:
fastify.addSchema({
$id: 'http://example.com/',
type: 'object',
properties: {
hello: { type: 'string' }
}
})
fastify.post('/', {
handler() { },
schema: {
body: {
type: 'array',
items: { $ref: 'http://example.com#/properties/hello' }
}
}
})
Now the problem is that I can not write a json that can be accepted by this schema. From my basic understanding a simple post request like the following should be accepted
[
{
"hello": "bye"
},
{
"hello": "bye bye"
}
]
However server keeps telling me that body[0] should be string. Where am I wrong?
The reference $ref: 'http://example.com#/properties/hello' points to the hello property schema value, which is { type: 'string' }.
This means the schema in fastify.post('/', { expects the body to be an array of strings.

set 'dependsOn' on a Relationship field in KeystoneJS

I wonder if there is a way to achieve dependsOn option for a Types.Relationship field in KeystoneJS.
Example:
Event.add({
category: { type: Types.Relationship, ref: 'EventCategory' },
singer: { type: String, dependsOn: { category: { type: 'concerts' } } },
});
Currently, I can only walk around this by manually set dependsOn to the id of the related field.

Loading Json in Store to display it on List Sencha Touch 2 jsonp

This is my model
Ext.define("StockWatch.model.Market", {
extend: "Ext.data.Model",
config: {
idProperty: 'CompanyCode',
fields: [
{ name: 'CompanyCode', type: 'string' },
{ name: 'LastTradedPrice', type: 'string' },
{ name: 'PercentageDiff', type: 'string' },
{ name: 'FiftyTwoWeekHigh', type: 'string' },
{ name: 'FiftyTwoWeekLow', type: 'string' },
{ name: 'ChangePercent', type: 'string' },
{ name: 'Change', type: 'string' },
{ name: 'MarketCap', type: 'string' },
{ name: 'High', type: 'string' },
{ name: 'Low', type: 'string' },
{ name: 'PrevClose', type: 'string' },
{ name: 'OpenInterest', type: 'string' },
{ name: 'MarketLot', type: 'string' },
{ name: 'ChangeInOpenInterest', type: 'string' },
{ name: 'LastTradedTime', type: 'date', dateFormat: 'c' },
]
}
});
this is my store
Ext.define("StockWatch.store.Markets", {
extend: "Ext.data.Store",
requires: ["Ext.data.proxy.LocalStorage", "Ext.data.proxy.JsonP", "StockWatch.model.Market"],
config: {
model: "StockWatch.model.Market",
autoLoad : true,
proxy : {
type : 'jsonp',
url : 'http://money.rediff.com/money1/current_status_new.php?companylist=17023928%7C17023929&id=1354690151&Rand=0.6305125835351646',
reader:{
type:'json',
rootProperty:''
}
}
}
});
I'm not able to get the data on to my list, may be somewhere fetching of data is wrong.
guide me to find the solution.
also I'm using pull to refresh list plugin, so will the data be loaded automatically each time i pull down the list or do i have to write something over there to??
thnx in advance
EDIT:
I also get this warning in the console
Resource interpreted as Script but transferred with MIME type text/html: "http://money.rediff.com/money1/current_status_new.php?companylist=17023928%7C17023929&id=1354690151&Rand=0.6305125835351646&_dc=1355822361093&page=1&start=0&limit=25&callback=Ext.data.JsonP.callback1".
use callbackKey
callbackKey: Specifies the GET parameter that will be sent to the server containing the function name to be executed when the request completes. Defaults to callback. Thus, a common request will be in the form of url?callback=Ext.data.JsonP.callback1
Defaults to: "callback"
You need to wrap the JSON response in a callback function for JSONP. It doesn't look like your remote call is returning this, try specifying a callback parameter - otherwise if the remote server does not allow this you need to pass it through another server to wrap it in a callback function.
Also, the warning you mentioned in the bottom of your post, don't worry about that. It won't cause any problems.

Sencha Touch Grouper Property Conversion

I have a list reading from a Json store which contains a grouper on a field of type int. This field is called "req" in the sample model below. However, rather than group by the int value, I would like to assign a text value instead, so for example, in case of "1" I would group by "Yes" and in case of "0" I would group by "No". The conversion can be hard coded as it will not change. Where do I make this conversion in my code? Thanks for your help
Ext.define('MyApp.store.MyStore', {
extend: 'Ext.data.Store',
requires: [
'MyApp.model.MyData'
],
config: {
model: 'MyApp.model.MyData',
storeId: 'MyStore',
proxy: {
type: 'ajax',
url: '/path/to/data.json',
reader: {
type: 'json',
rootProperty: 'items'
}
},
grouper: {
property: 'req',
sortProperty: 'req'
},
groupDir: 'DESC'
}
});
Model:
Ext.define('Mypp.model.MyModel', {
extend: 'Ext.data.Model',
config: {
fields: [
{
name: 'example',
type: 'string'
},
{
name: 'req',
type: 'int'
}
]
}
});
Add to the Model a calculated field with convert() function:
Ext.define('Mypp.model.MyModel', {
extend: 'Ext.data.Model',
config: {
fields: [
{
name: 'example',
type: 'string'
},
{
name: 'req',
type: 'int'
},
{
name: 'reqDisplay',
type: 'string',
convert: function (value, record) {
if (value == null) {
var req = record.get('req');
value = req ? 'Yes' : 'No'
}
return value;
}
}
]
}
});
... and use it instead
grouper: {
property: 'reqDisplay',
sortProperty: 'reqDisplay'
},
Cheers, Oleg

How to use inner properties of a JSON response with Sencha Proxy

The JSONP proxy is largely working for me, but I need to set properties of a model based on some nested properties in the JSON response. I can't figure how to do this without extending the Reader class, but thought there might be an easier way that I'm just missing.
My Recipe model:
Ext.define('NC.model.Recipe', {
extend: 'Ext.data.Model',
config: {
fields: [
{ name: 'name', type: 'string' },
{ name: 'image', type: 'string' },
{ name: 'preparationText', type: 'string' },
{ name: 'ingredientsText', type: 'string' },
{ name: 'servings', type: 'string' }
]
}
});
My Store:
Ext.define('NC.store.Recipes', {
extend: 'Ext.data.Store',
config: {
model: 'NC.model.Recipe',
storeId: 'Recipes',
proxy: {
type: 'jsonp',
url: 'http://anExternalSite.com/api',
callbackKey: 'callback',
filterParam: 'text',
extraParams: {
type: 'Recipe'
},
reader: {
type: 'json',
idProperty: 'uuid',
}
}
}
});
The JSON format:
[
{
uuid: "/UUID(XXXX)/",
name: "Spicy Peanut Noodle Salad",
image: "http://someplace.com/noodle-salad.jpg",
properties: {
preparationText: "Make it all nice and stuff",
ingredientsText: "Heaps of fresh food",
servings: "serves 4",
}
},
{ ... },
{ ... }
]
I would like those 3 'properties' - preparationText, ingredientsText, and servings, to be placed in the model, but currently only id, name, and image are. What is the method to make this work? If it does involve extending the Reader class, some direction would be great.
Thanks.
You can change your code like this to access nested values
{ name: 'preparationText', type: 'string', mapping: 'properties.preparationText' },
This mapping path should start excluding the root element.