Schema Analizer
W ngsi-parser helps you to manage your Data Models like used in FIWARE, ngsi-parser can analize if the entity complies with the specified model and identify its errors, to do it you need the JSON Schema of this Data Model provided or you can build some.
You can know about JSON Schemas in JSON Schema.
Importing your JSON Schema
You can import your JSON schema importing a JSON from a file or from a repository using the help of ocb-sender.
Importing From a JSON File
var ngsi = require('ngsi-parser');
var mySchema = require('mySchema.json');
ngsi.setModel({
mySchema : mySchema
});
Importing from a remote repository
var ngsi = require('ngsi-parser');
ngsi.setModel({
myRemoteSchema : 'https://yourdatamodels.com/myRemote'
});
Importing several schemas from different sources
var ngsi = require('ngsi-parser');
var mySchema = require('mySchema.json');
ngsi.setModel({
mySchema : mySchema,
myRemoteSchema : 'https://yourdatamodels.com/myRemote',
AlertModel : 'https://raw.githubusercontent.com/smartsdk/dataModels/master/Alert/schema.json',
anotherModel : require('anotherSchema.json')
});
Using your Data Models Schemas
To use the schemas imported from a JSON file only you need to specify the name with which you entered it to ngsi-parser and it will return you one array with the errors found.
var ngsi = require('ngsi-parser');
var mySchema = require('mySchema.json');
ngsi.setModel({
mySchema : mySchema
});
var entity = {
id :'Room1',
type:'Room',
temperature : 50,
dateStamp : new Date()
};
let errors = ngsi.verifyModel('mySchema', entity);
if (errors.length === 0 ){
console.log("The entity it's OK")
}else {
errors.map(console.log)
}
To use Schemas from a remote repository is necesary download it, is because you need use ocb-sender, and in this case the method ngsi.verifyModel() becomes to a promise.
var ngsi = require('ngsi-parser');
var ocb = require('ocb-sender');
ngsi.setModel({
myRemoteSchema : 'https://yourdatamodels.com/myRemote',
});
var entity = {
id :'Room1',
type:'Room',
temperature : 50,
dateStamp : new Date()
};
ngsi.verifyModel('myRemoteSchema', entity, ocb)
.then((errors) => {
if (errors.length === 0 ){
console.log("The entity it's OK")
}else {
errors.map(console.log)
}
})
Use without storing the JSON schema
var ngsi = require('ngsi-parser');
var ocb = require('ocb-sender');
ngsi.verifyModel('https://yourdatamodels.com/myRemote', entity ,cb)
.then((errors) => {
if (errors.length === 0 ){
console.log("The entity it's OK")
}else {
errors.map(console.log)
}
})
Real Example Case
The Alert Schema imported from Fiware Data Models Repository in this Link.
{
"$schema": "http://json-schema.org/schema#",
"id": "https://fiware.github.io/dataModels/Alert/schema.json",
"title": "Alert data model JSON Schema",
"description": "An alert generated by a user or device in a givel location",
"type": "object",
"allOf": [
{
"$ref": "https://fiware.github.io/dataModels/common-schema.json#/definitions/GSMA-Commons"
},
{
"$ref": "https://fiware.github.io/dataModels/common-schema.json#/definitions/Location-Commons"
},
{
"properties": {
"description": {
"type": "string"
},
"dateObserved": {
"type": "string",
"format": "date-time"
},
"validFrom": {
"type": "string",
"format": "date-time"
},
"validTo": {
"type": "string",
"format": "date-time"
},
"severity": {
"type": "string",
"enum": [
"informational",
"low",
"medium",
"high",
"critical"
]
},
"category": {
"type": "string",
"enum": [
"traffic",
"weather",
"environment",
"health",
"security"
]
},
"subCategory": {
"type": "string",
"enum": [
"trafficJam",
"carAccident",
"carWrongDirection",
"carStopped",
"pothole",
"roadClosed",
"roadWorks",
"hazardOnRoad",
"injuredBiker",
"rainfall",
"highTemperature",
"lowTemperature",
"heatWave",
"ice",
"snow",
"wind",
"fog",
"flood",
"tsunami",
"tornado",
"tropicalCyclone",
"hurricane",
"asthmaAttack",
"bumpedPatient",
"fallenPatient",
"heartAttack",
"suspiciousAction",
"robbery",
"assault"
]
},
"alertSource": {
"oneOf": [
{
"type": "string",
"format": "uri"
},
{
"$ref": "https://fiware.github.io/dataModels/common-schema.json#/definitions/EntityIdentifierType"
}
]
},
"data": {
"type": "object"
},
"type": {
"type": "string",
"enum": [
"Alert"
],
"description": "NGSI Entity type"
}
}
}
],
"oneOf": [
{
"required": [
"id",
"type",
"location",
"alertSource",
"category",
"dateObserved"
]
},
{
"required": [
"id",
"type",
"address",
"alertSource",
"category",
"dateObserved"
]
}
]
}
var ngsi = require('ngsi-parser');
var ocb = require('ocb-sender');
ngsi.setModel({
Alert : 'https://raw.githubusercontent.com/smartsdk/dataModels/master/Alert/schema.json'
})
var alertEntity = {
id: "Alert:Device_Smartphone_7a85d9df7209b8bc:1519086635021",
type: "Alert",
alertSource: "Device_Smartphone_7a85d9df7209b8bc",
category: "traffic",
dateObserved: new Date(),
description: "Car Accident on Cenidet",
location: {
type : "geo:point",
value : "18.81186166666667 ,-98.96342000000001"
},
severity: "medium",
subCategory: "carAccident",
validFrom: new Date(),
validTo: new Date(),
dateCreated : new Date()
}
ngsi.verifyModel('Alert', alertEntity, ocb)
.then((errors) => {
if (errors.length === 0 ){
console.log("The entity it's OK");
var ngsiEntity = ngsi.parseEntity(alertEntity);
ocb.createEntity(ngsiEntity)
.then((result) => console.log(result))
.catch((err) => console.log(err))
}else {
errors.map(console.log)
//another action if it does not comply with the model
}
})