|
1 | | -import colors from 'colors'; |
| 1 | +import colors from "colors"; |
2 | 2 | import Ajv, { ErrorObject, Ajv as IAjv } from "ajv"; |
3 | | -import { OpenRPC, MethodObject, ContentDescriptorObject } from '@open-rpc/meta-schema'; |
| 3 | +import { OpenRPC, MethodObject, ContentDescriptorObject } from "@open-rpc/meta-schema"; |
4 | 4 |
|
5 | 5 | export default (callResults: any[], schema: OpenRPC) => { |
6 | 6 | const ajv = new Ajv(); |
7 | 7 | const metrics = { |
8 | 8 | errors: 0, |
9 | | - success: 0 |
10 | | - } |
11 | | - |
12 | | - callResults.forEach((call) => { |
| 9 | + success: 0, |
| 10 | + }; |
| 11 | + |
| 12 | + callResults.forEach(async (call) => { |
13 | 13 | if (call.error) { |
14 | 14 | metrics.errors++; |
15 | | - console.log(colors.red.underline('JSON-RPC Request Error: '), colors.cyan(call.method)); |
| 15 | + console.log(colors.red.underline("JSON-RPC Request Error: "), colors.cyan(call.method)); |
16 | 16 | console.log(call.error); |
17 | 17 | console.log(call.params); |
18 | 18 | } else { |
19 | 19 |
|
20 | | - const methodSchema = schema.methods.find((m) => m.name === call.method); |
| 20 | + const methodSchema = schema.methods.find((m: MethodObject) => m.name === call.method); |
21 | 21 | if (!methodSchema) { |
22 | 22 | return console.log(`Error: no result defined for ${call.method}`); |
23 | 23 | } |
| 24 | + const result = methodSchema.result as ContentDescriptorObject; |
| 25 | + |
| 26 | + let resultSchema: any = result.schema; |
| 27 | + |
| 28 | + if (result.oneOf) { |
| 29 | + resultSchema = { |
| 30 | + oneOf: result.oneOf.map((cd: ContentDescriptorObject) => cd.schema), |
| 31 | + }; |
| 32 | + } |
24 | 33 |
|
25 | | - const isValid = ajv.validate((methodSchema.result as ContentDescriptorObject).schema, call.result); |
| 34 | + ajv.validate(resultSchema, call.result); |
26 | 35 | const errors = ajv.errors as ErrorObject[]; |
27 | 36 |
|
28 | | - if (isValid) { |
| 37 | + if (!errors || errors.length === 0) { |
29 | 38 | metrics.success++; |
30 | | - console.log(colors.green('Success: '), call.method) |
| 39 | + console.log(colors.green("Success: "), call.method); |
31 | 40 | } else { |
32 | | - console.log(colors.red.underline('Result Validation Error: '), colors.cyan(call.method)); |
| 41 | + console.log(colors.red.underline("Result Validation Error: "), colors.cyan(call.method)); |
33 | 42 | console.log(call); |
34 | 43 | console.log(errors); |
35 | 44 | console.log(methodSchema); |
36 | 45 | } |
37 | 46 |
|
38 | 47 | } |
39 | | - }) |
40 | | - console.log('=========='); |
41 | | - console.log('Success: ', colors.green(metrics.success.toString())); |
42 | | - console.log('Errors: ', colors.red(metrics.errors.toString())); |
43 | | - console.log('=========='); |
| 48 | + }); |
| 49 | + console.log("=========="); |
| 50 | + console.log("Success: ", colors.green(metrics.success.toString())); |
| 51 | + console.log("Errors: ", colors.red(metrics.errors.toString())); |
| 52 | + console.log("=========="); |
44 | 53 | }; |
0 commit comments