Can json schema be used to validate based on input data - jsonschema

I'm just wondering if it is possible to use json schema to do validation of input data BASED on the actual input data itself.
Say I have an object:
{
parts: {
123: {
happy: 'me'
}
},
cells: [{
part_id: 123,
some: 'other property'
}, {
part_id: 124,
some: 'thing else'
}]
}
Can I write a json schema that ensures that the part_id of the cell objects actually corresponds to one of the part objects passed in?
In other words, part_id: 123 would be acceptable, but part_id: 124 wouldn't be.
Thanks!

After doing a bit of research, it looks like JSON Schema v5 has the functionality already via the $data reference attribute.
The following is from the javascript schema validator, ajv:
https://www.npmjs.com/package/ajv#data-reference
$data reference
With v5 option you can use values from the validated data as the values for the schema keywords. See v5 proposal for more information about how it works.
$data reference is supported in the keywords: constant, enum, format, maximum/minimum, exclusiveMaximum / exclusiveMinimum, maxLength / minLength, maxItems / minItems, maxProperties / minProperties, formatMaximum / formatMinimum, formatExclusiveMaximum / formatExclusiveMinimum, multipleOf, pattern, required, uniqueItems.
The value of "$data" should be a relative JSON-pointer.
Examples.
This schema requires that the value in property smaller is less or equal than the value in the property larger:
var schema = {
"properties": {
"smaller": {
"type": number,
"maximum": { "$data": "1/larger" }
},
"larger": { "type": number }
}
};
var validData = {
smaller: 5,
larger: 7
};
This schema requires that the properties have the same format as their field names:
var schema = {
"additionalProperties": {
"type": "string",
"format": { "$data": "0#" }
}
};
var validData = {
'date-time': '1963-06-19T08:30:06.283185Z',
email: 'joe.bloggs#example.com'
}
$data reference is resolved safely - it won't throw even if some property is undefined. If $data resolves to undefined the validation succeeds (with the exclusion of constant keyword). If $data resolves to incorrect type (e.g. not "number" for maximum keyword) the validation fails.

Related

Does Json Schema allow a property's definition to reference another property?

I'd like to create a JSON schema that restricts one property's values based on another property's values.
An example valid object might look like this:
{
"lookup": {
"foo": "string",
"bar": "number",
},
// properties in `values` must exist in `lookup`
"values": {
// `foo` must be a string
"foo": "string is OK",
// `bar` must be a number
"bar": 100
}
}
The idea is for the schema to enforce a relationship between the two properties.
{
"type": "object",
"properties": {
"lookup" : {
"type": "object",
"additionalProperties" : {
"type": "string",
"enum": ["string", "number"]
}
},
"values": {
"type": "object",
// - this value's properties must exist in `lookup`
// - if the property in `lookup` is set to `string`, the type here must be `string`; if the property in `lookup` is set to `number`, the type here must be `number`
}
}
}
This is possible in some cases. While you can't restrict a piece of data to certain values taken from other parts of the data (for example: using property X to provide a list of values that property Y can have), you can specify conditionals between parts of your schema.
requirement 1: this value's properties must exist in lookup -> not possible
requirement 2: if the property in lookup is set to string, the type here must be string; if the property in lookup is set to number, the type here must be number -> possible
See https://json-schema.org/understanding-json-schema/reference/conditionals.html for the various options available to you.
This is not possible with the standard specification of JSON Schema.

MarkLogic - Xpath on JSON document

MarkLogic Version: 9.0-6.2
I am trying to apply Xpath in extract-document-data (using Query Options) on a JSON document shown below. I need to filter out "Channel" property if the underneath property "OptIn" has a value of "True".
{
"Category":
{
"Name": "Severe Weather",
"Channels":[
{
"Channel":
{
"Name":"Email",
"OptIn": "True"
}
},
{
"Channel":
{
"Name":"Text",
"OptIn": "False"
}
}
]
}
}
I tried below code,
'<extract-document-data selected="include">' +
'<extract-path>//*[OptIn="True"]/../..</extract-path>' +
'</extract-document-data>' +
which is only pulling from "Channel" property as shown below.
[
{
"Channel": {
"Name": "Email",
"OptIn": "True"
}
}
]
But my need is to pull from parent "Category" property, but filter out the Channels that have OptIn value as False.
Any pointers?
If I understand correctly, you'd like to extract 'Category', but only with those 'Channel's that have 'OptIn' equalling 'true', right?
Extract-document-data is not advanced enough for that. You best extract entire Categories which have at least one OptIn equalling true (//Category[//OptIn = 'true']), and use a REST transform on the search response to trim down the unwanted Channels..
HTH!

JSON Schema - require all properties

The required field in JSON Schema
JSON Schema features the properties, required and additionalProperties fields. For example,
{
"type": "object",
"properties": {
"elephant": {"type": "string"},
"giraffe": {"type": "string"},
"polarBear": {"type": "string"}
},
"required": [
"elephant",
"giraffe",
"polarBear"
],
"additionalProperties": false
}
Will validate JSON objects like:
{
"elephant": "Johnny",
"giraffe": "Jimmy",
"polarBear": "George"
}
But will fail if the list of properties is not exactly elephant, giraffe, polarBear.
The problem
I often copy-paste the list of properties to the list of required, and suffer from annoying bugs when the lists don't match due to typos and other silly errors.
Is there a shorter way to denote that all properties are required, without explicitly naming them?
You can just use the "minProperties" property instead of explicity naming all the fields.
{
"type": "object",
"properties": {
"elephant": {"type": "string"},
"giraffe": {"type": "string"},
"polarBear": {"type": "string"}
},
"additionalProperties": false,
"minProperties": 3
}
I doubt there exists a way to specify required properties other than explicitly name them in required array.
But if you encounter this issue very often I would suggest you to write a small script that post-process your json-schema and add automatically the required array for all defined objects.
The script just need to traverse the json-schema tree, and at each level, if a "properties" keyword is found, add a "required" keyword with all defined keys contained in properties at the same level.
Let the machines do the bore stuff.
I do this in code with a one-liner, for instance, if I want to use required for insert in a DB, but only want to validate against the schema when performing an update.
prepareSchema(action) {
const actionSchema = R.clone(schema)
switch (action) {
case 'insert':
actionSchema.$id = `/${schema.$id}-Insert`
actionSchema.required = Object.keys(schema.properties)
return actionSchema
default:
return schema
}
}
if you using the library jsonschema in python use custom validators:
first create custom validator:
# Custom validator for requiring all properties listed in the instance to be in the 'required' list of the instance
def allRequired(validator, allRequired, instance, schema):
if not validator.is_type(instance, "object"):
return
if allRequired and "required" in instance:
# requiring all properties to 'required'
instanceRequired = instance["required"]
instanceProperties = list(instance["properties"].keys())
for property in instanceProperties:
if property not in instanceRequired:
yield ValidationError("%r should be required but only the following are required: %r" % (property, instanceRequired))
for property in instanceRequired:
if property not in instanceProperties:
yield ValidationError("%r should be in properties but only the following are properties: %r" % (property, instanceProperties))
then extend an exsitsing validator:
all_validators = dict(Draft4Validator.VALIDATORS)
all_validators['allRequired'] = allRequired
customValidator = jsonschema.validators.extend(
validator=Draft4Validator,
validators=all_validators
)
now test:
schema = {"allRequired": True}
instance = {"properties": {"name": {"type": "string"}}, "required": []}
v = customValidator(schema)
errors = validateInstance(v, instance)
you will get the error:
'name' should be required but only the following are required: []
As suggested by others, here's such post-processing python code:
def schema_to_strict(schema):
if schema['type'] not in ['object', 'array']:
return schema
if schema['type'] == 'array':
schema['items'] = schema_to_strict(schema['items'])
return schema
for k, v in schema['properties'].items():
schema['properties'][k] = schema_to_strict(v)
schema['required'] = list(schema['properties'].keys())
schema['additionalProperties'] = False
return schema
You can use the function below:
export function addRequiredAttributeRecursive(schema) {
if (schema.type === 'object') {
schema.required = [];
Object.keys(schema.properties).forEach((key) => {
schema.required.push(key);
if (schema.properties[key].type === 'object') {
schema.properties[key] = addRequiredAttributeRecursive(
schema.properties[key],
);
} else if (schema.properties[key].type === 'array') {
schema.properties[key].items = addRequiredAttributeRecursive(
schema.properties[key].items,
);
}
});
} else if (schema.type === 'array') {
if (schema.items.type === 'object') {
schema.items = addRequiredAttributeRecursive(schema.items);
}
}
return schema;
}
It recursively write the required attribute for every property on all objects from the schema you have.
If you are using Javascript, you can use property getter.
{
"type": "object",
"properties": {
"elephant": {"type": "string"},
"giraffe": {"type": "string"},
"polarBear": {"type": "string"}
},
get required() { return Object.keys(this.properties) },
"additionalProperties": false
}

IBM Worklight JSONStore Auto-increment field

I use Worklight JSONStore and I need a numeric field autoincrement.
I tried this way but it did not work.
var COLLECTION_SPESE = {
Spese : {
searchFields:
{id: "INTEGER primary key autoincrement", importo: "string", valuta: "string", metodoPagamento: "string",
acconto: "string", data: "string", motivazione: "string", categoria: "string",
icona: "string", checked: "boolean"}
}
};
How can I do?
You would have to provide code to do the auto-incrementing yourself. For example WL.JSONStore.get('collection').add({id: getLastId() + 1, ...}). The getLastId() function would return the last id value used in the collection. You would have to write the implementation for the getLastId function. The search field type for id would be integer.
Alternatively, you could depend of the value of _id which is generated by JSONStore. It is an auto-incremented integer starting from 1. The value assigned to _id is never re-used, for example, if you remove the document with _id == 1 and then add a new one, 1 is not used again for the new document.
WL.JSONStore.get('collection').add({name: 'carlos})
.then(function () {
return WL.JSONStore.get('collection').findAll();
})
.then(function (res) {
//res => [{_id: 1, json: {name: 'carlos'}}]
})
FYI - Feature requests here.

Adding an array element to all documents in a collection

My Foo documents have a CustomData collection used to add user-configurable properties.
Sometimes, when I create those properties, I'm need to add them with a default value for indexing purposes.
This is what I'm trying to use for that purpose:
DatabaseCommands.UpdateByIndex(
"dynamic/Foos",
new IndexQuery(),
new[]
{
new PatchRequest
{
Name = "CustomData",
Type = PatchCommandType.Add,
Value = RavenJObject.FromObject(new
{
Value = false,
Bar = new { Baz = "Qux"}
})
}
});
This generates the following HTTP request:
PATCH /databases/MyDb/bulk_docs/dynamic/Foos?&pageSize=128&allowStale=False
[
{
"Type": "Add",
"Value": {
"Value": false,
"Bar": {
"Baz": "Qux"
}
},
"Name": "CustomData"
}
]
And this returns 200 OK, but no documents are modified.
It looks like the problem is the usage of dynamic indexes.
Switching to a persistent index solved the problem.