Foundations.JSON.Schema
Schema.validate
You can use this API to determine if a JSON data object is valid JSON. Optionally, you can also validate it against a passed JSON schema object.
Syntax
Schema.validate( {instance object}, {schema object});
Parameters
| Argument | Required | Type | Description |
| instance object | Yes | any | JSON data object. |
| schema object | No | any | JSON schema object. |
Returns
{ valid : boolean, errors : [ { property : string, message : string } ] }
| Argument | Required | Type | Description |
| valid | Yes | boolean | Is instance object valid according to schema flag. |
| errors | Yes | array | Array of inline objects. Returns empty array ("[]") if no errors. |
| errors[n].property | No | string | Property that had the error. |
| errors[n].message | No | string | Error message |
Example
var jsonlib = MojoLoader.require({name:"foundations.json", version: "1.0"}); var fjson = jsonlib["foundations.json"]; var jsonSchema = {"id":"com.palm.schema.test:1", "type": "object", "properties" : { "_kind" : {"type": "string", "value":"com.palm.schema.test:1"}, "foo": {"type": "string", "description": "foo string"}, "bar": {"type": "string", "description": "bar string"}, "isMember": {"type": "boolean", "description" : "Is member flag" } } }; var BadObj = {"_kind":"com.palm.schema.test:1", "foo":"myFoo", "bar":"myBar", "isMember": "true"}; Mojo.Log.info("FoundJSON: BadObj validate "+ JSON.stringify(fjson.Schema.validate(BadObj, jsonSchema)));
Example Output
{"valid":false,"errors":[{"property":"isMember","message":"string value found, but a boolean is required"}]}
Schema.checkPropertyChange
Checks an object property for a valid value given its schema. Unlike the validate method, it assumes the passed schema object is valid.
Syntax
Schema.checkPropertyChange( {instance object}, {schema object}, "property");
Parameters
| Argument | Required | Type | Description |
| instance object | Yes | any | JSON data object. |
| schema object | Yes | any | JSON schema object. It is assumed this is valid. |
| property | Yes | string | Name of schema property. |
Returns
{ valid : boolean, errors : [ { property : string, message : string } ] }
| Argument | Required | Type | Description |
| valid | Yes | boolean | Is instance object valid according to schema flag. |
| errors | Yes | array | Array of inline objects. Returns empty array ("[]") if no errors. |
| errors[n].property | No | string | Property that had the error. |
| errors[n].message | No | string | Error message |
Example
var jsonlib = MojoLoader.require({name:"foundations.json", version: "1.0"}); var fjson = jsonlib["foundations.json"]; var jsonSchema = {"id":"com.palm.schema.test:1", "type": "object", "properties" : { "_kind" : {"type": "string", "value":"com.palm.schema.test:1"}, "foo": {"type": "string", "description": "foo string"}, "bar": {"type": "string", "description": "bar string"}, "isMember": {"type": "boolean", "description" : "Is member flag" } } }; var BadObj = {"_kind":"com.palm.schema.test:1", "foo":"myFoo", "bar":"myBar", "isMember": "true"}; var GoodObj = {"_kind":"com.palm.schema.test:1", "foo":"myFoo", "bar":"myBar", "isMember": true}; Mojo.Log.info("FoundJSON: checkPropertyChange good object "+ JSON.stringify(fjson.Schema.checkPropertyChange(GoodObj, jsonSchema, "isMember"))); Mojo.Log.info("FoundJSON: checkPropertyChange bad object "+ JSON.stringify(fjson.Schema.checkPropertyChange(BadObj, jsonSchema, "isMember")));
Example Output
FoundJSON: checkPropertyChange good object {"valid":true,"errors":[]}, FoundJSON: checkPropertyChange bad object {"valid":false,"errors":[{"property":"isMember.isMember","message":"string value found, but a boolean is required"}]}