Schema Enforcement

DB8 Service provides a mechanism for validating JSON data objects when stored with the put() call. Your app or service can do this when creating the data object's kind with the putKind() call using the schema property. For this field, enter the object's JSON schema according to official JSON standards.

The following is a simple example of how this works using Enyo's serviceRequest API.

1. Create a kind with a schema:

var request = new enyo.ServiceRequest();
var parameter = {
    "id":"com.yourdomain.schema.test:1",
    // Define schema here
    "schema": {
        "id":"com.yourdomain.schema.test:1", 
        "type": "object", 
        "properties" : {
            "_kind" : {
                "type": "string",
                "value":"com.yourdomain.schema.test:1"
            }, 
            "foo": {
                "type": "string",
                "description": "foo string"
            }, 
            "bar": {
                "type": "string", 
                "description": "bar string"
            },
            "isMember": {
                "type": "boolean", 
                "description" : "Is member flag" 
            }
        }
    },
    "owner":"com.yourdomain.dbtest",
    "indexes": [
        {
            "name":"foo",
            "props":[
                {"name":"foo"}
            ]
        },
        {
            "name":"barfoo",
            "props":[
                {"name":"bar"},
                {"name":"foo"}
            ] 
        }
    ]
};
request.service = "luna://com.palm.db/putKind";
request.response(this, "registerDatabaseKindResponse");
request.error(this, "registerDatabaseKindError");
request.go(parameter);

 

One of the following two handlers is called when creating the kind is complete.

registerDatabaseKindResponse: function(inSender, inResponse) {
    enyo.log("Schema test, putKind success!");
},
registerDatabaseKindError: function(inSender, inResponse) {
    enyo.log("Schema test, putKind failure!");
},

 

If the kind creation has succeeded, you will see the following ouput in the log.

Schema test, putKind success!

 

2. Store an object with all the required fields, as in the schema defined in step 1.

var testObj = {
    "_kind":"com.yourdomain.schema.test:1",
    "foo":"myFoo", 
    "bar":"myBar",
    "isMember": true
};
var objs = [testObj];
var request = new enyo.ServiceRequest();
var parameter = {"objects": objs};
request.service = "luna://com.palm.db/put";
request.response(this, "putDataIntoDatabaseResponse");
request.error(this, "putDataIntoDatabaseError");
request.go(parameter);

 

One of the following two handlers is called when the request for storing an object has been completed.

putDataIntoDatabaseResponse: function(inSender, inResponse) {
    enyo.log("Schema test, put success!");
},
putDataIntoDatabaseError: function(inSender, inResponse) {
    enyo.log("Schema test, put failure!");
},

 

You will see the following ouput in the log.

Schema test, put success!

 

3. Try to store an object with a missing required field:

var incompletedObj = {
    "_kind":"com.yourdomain.schema.test:1",
    "foo":"myFoo",
    "bar":"myBar"
};
objs = [incompletedObj];
 
var request = new enyo.ServiceRequest();
var parameter = {"objects": objs};
request.service = "luna://com.palm.db/put";
request.response(this, "putDataIntoDatabaseResponse");
request.error(this, "putDataIntoDatabaseError");
request.go(parameter);

 

Since your request did not comply with the schema registered, you will see the following output in the log.

Schema test, put failure!

 

4. Try to store an object with a field that has a wrong type value:

var invalidObj = {
    "_kind":"com.palm.schema.test:1",
    "foo":"myFoo",
    "bar":"myBar",
    "isMember":"true"
};
objs = [invalidObj];
 
var request = new enyo.ServiceRequest();
var parameter = {"objects": objs};
request.service = "luna://com.palm.db/put";
request.response(this, "putDataIntoDatabaseResponse");
request.error(this, "putDataIntoDatabaseError");
request.go(parameter);

 

Similar to the step 3, your request did not comply with the schema registered, you will see the following output in the log.

Schema test, put failure!

 

Navigation