Parses a json schema into a simple usable api object
This library is not bound to a specific method for transmitting data back and forth between your app and the queried server. Therefore you have to provide the facilities for this on your own. For all examples below, we use a WebSocket instance:
var jrpc = require'jrpc-schema';var socket = 'ws://localhost/';
The main "meat" of the library. The
Schema constructor should be invoked with a JSON Schema object (not a JSON string);
true if the value is valid and
In addition, if the validators schema had any other schemas assigned to it as properties (this will be the case for almost all top-level json-schemas), it has a validator function for each of those sub-schemas assigned to it as a property (as functions are objects in js).
var schema ="type":"object""name":"type": "string""age" :"type": "integer""maximum": 125
var root = parseschema//Returns true if `value` is an objectrootschemavalue//Returns true if `value` is a stringrootschemanamevalue//Returns true if `value` is an integer larger then 125rootschemaagevalue
If a json-schema describes a JSON-RPC method, we do not create a validator function. Instead we generate a wrapper for that method. This wrapper takes arguments either by order as an array, or as one object by name. The method will return a promise, which will be fulfilled with the methods return value once it becomes available.
var schema ="square":"type": "method""returns": "number""params":"type": "number""name": "square""required": true"minimum": 0"description": "Number to find the square root of""type": "number""name": "nth""default": 2"minimum": 2"description": "The degree of the root"
var root = schema someTransmitter;rootschemasquaresquare: 27nth: 3then//Result should be 3, given a sane serverconsole.logresult;;//By orderrootschemasquare27 3thenconsole.logresult;
JSON RPC notifications are treated similarly to methods.
var schema ="notify":"params":"name": "data""required": true"type": number"returns": null"type": "notification"
var root = schema someTransmitter;//May be called multiple timesrootschemanotifyconsole.logdata;;//Alternative without the need to have been defined in the schemarootnotificationson'notify'console.logdata;
Call a single json-rpc method and return a Promise.
method should be the method name,
params should be a parameter object and the
transmitter should be a function which takes a json string as its only argument. This string should then be send to the server.
The returned Promise has a
.handle(json) which you should call with all of your servers responses. The promise will then be resolved with the correct value as soon as it is available.
//Run the requestvar playerInfo = jrpcrun'Player.GetInfo''playerid': 1socketsendbindsocket;//Log the resultplayerInfothenconsole.loginfo;;//Ensure the library gets the servers datasocketon'message' playerInfohandle;
As you can see, this is slightly convoluted and does not utilize json-schemas at all.
You should use an instance of
jrpc.Schema wherever possible.