Skip to content

Commit

Permalink
feat(rpc): adds RPC schemas validation
Browse files Browse the repository at this point in the history
  • Loading branch information
rafamel committed Oct 28, 2019
1 parent d2d49ad commit 45d3a62
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 14 deletions.
14 changes: 4 additions & 10 deletions packages/rpc/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/rpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@
"typescript": "^3.6.4"
},
"dependencies": {
"@karmic/core": "0.0.0"
"@karmic/core": "0.0.0",
"ajv": "^6.10.2"
},
"peerDependencies": {
"rxjs": "6.x"
Expand Down
5 changes: 2 additions & 3 deletions packages/rpc/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export default function main(): null {
return null;
}
export * from './validate';
export * from './types';
23 changes: 23 additions & 0 deletions packages/rpc/src/validate/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Ajv from 'ajv';
import draft04 from 'ajv/lib/refs/json-schema-draft-04.json';
import { notification, request, response } from './schemas';
import { RPCSpecNotification, RPCSpecRequest, RPCSpecResponse } from '~/types';

const ajv = new Ajv({ schemaId: 'id', logger: false });
ajv.addMetaSchema(draft04);

const validateNotification = ajv.compile(notification);
const validateRequest = ajv.compile(request);
const validateResponse = ajv.compile(response);

export const validate = {
notification(data: object): data is RPCSpecNotification {
return validateNotification(data) as boolean;
},
request(data: object): data is RPCSpecRequest {
return validateRequest(data) as boolean;
},
response(data: object): data is RPCSpecResponse {
return validateResponse(data) as boolean;
}
};
61 changes: 61 additions & 0 deletions packages/rpc/src/validate/schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { Schema } from '@karmic/core';

export const id: Schema = {
anyOf: [{ type: 'number' }, { type: 'string' }, { type: 'null' }]
};

export const notification: Schema = {
type: 'object',
required: ['jsonrpc', 'method'],
properties: {
jsonrpc: { type: 'string', enum: ['2.0'] },
method: { type: 'string' },
params: { anyOf: [{ type: 'array' }, { type: 'object' }] }
},
not: { required: ['id'] }
};

export const request: Schema = {
type: 'object',
required: ['jsonrpc', 'id', 'method'],
properties: {
jsonrpc: { type: 'string', enum: ['2.0'] },
id,
method: { type: 'string' },
params: { anyOf: [{ type: 'array' }, { type: 'object' }] }
}
};

export const response: Schema = {
definitions: {
success: {
type: 'object',
required: ['jsonrpc', 'id', 'result'],
properties: {
jsonrpc: { type: 'string', enum: ['2.0'] },
id: id,
result: {},
error: { not: {} }
}
},
error: {
type: 'object',
required: ['jsonrpc', 'id', 'error'],
properties: {
jsonrpc: { type: 'string', enum: ['2.0'] },
id,
result: { not: {} },
error: {
type: 'object',
required: ['code', 'message'],
properties: {
code: { type: 'number' },
message: { type: 'string' },
data: {}
}
}
}
}
},
anyOf: [{ $ref: '#/definitions/success' }, { $ref: '#/definitions/error' }]
};

0 comments on commit 45d3a62

Please sign in to comment.