Skip to content

Commit 6e16852

Browse files
authored
Merge pull request #486 from open-rpc/fix/meta-schema-version-and-valid-fix
fix: meta-schema version + fix valid setting on errors in example rule
2 parents 9a22420 + 8d344e8 commit 6e16852

File tree

9 files changed

+353
-332
lines changed

9 files changed

+353
-332
lines changed

package-lock.json

Lines changed: 259 additions & 322 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"dependencies": {
2121
"@open-rpc/html-reporter-react": "^0.0.4",
2222
"@open-rpc/schema-utils-js": "^1.16.2",
23+
"@open-rpc/meta-schema": "^1.14.6",
2324
"@types/isomorphic-fetch": "0.0.35",
2425
"@types/lodash": "^4.14.162",
2526
"ajv": "^7.0.0",
@@ -30,7 +31,6 @@
3031
"lodash": "^4.17.20"
3132
},
3233
"devDependencies": {
33-
"@open-rpc/meta-schema": "^1.13.13",
3434
"@types/jest": "^29.5.12",
3535
"eslint": "^8.57.0",
3636
"jest": "^29.7.0",

src/reporters/console-rule.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import colors from "colors";
2-
import { JSONSchemaObject } from "@open-rpc/meta-schema";
2+
import { JSONSchemaObject, MethodObject } from "@open-rpc/meta-schema";
33
import { Call, IOptions } from "../coverage";
44
import _ from "lodash";
55
import Reporter from "./reporter";
@@ -85,7 +85,7 @@ class ConsoleRuleReporter implements Reporter {
8585
console.log(
8686
colors.magenta("\t\t\t \->"),
8787
colors.white.underline("Expected result:"),
88-
colors.white(expected),
88+
colors.white(expected?.toString() || ""),
8989
);
9090

9191
if (ex.requestError) {
@@ -146,7 +146,7 @@ class ConsoleRuleReporter implements Reporter {
146146
const failedMethods = Object.keys(failedCallsByMethod);
147147

148148
//take into account failed methods
149-
const missingMethods: string[] = options.openrpcDocument.methods
149+
const missingMethods: string[] = (options.openrpcDocument.methods as MethodObject[])
150150
.map(({ name }) => name)
151151
.filter((methodName) => !passingMethods.includes(methodName))
152152
.filter((methodName) => !failedMethods.includes(methodName));

src/reporters/console-streaming.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import colors from "colors";
22
import {
33
JSONSchemaObject,
4+
MethodObject,
45
} from "@open-rpc/meta-schema";
56
import {Call, IOptions} from "../coverage";
67
import _ from "lodash";
@@ -65,7 +66,7 @@ class ConsoleStreamingReporter implements Reporter {
6566
console.log(
6667
colors.magenta("\t\t\t \->"),
6768
colors.white.underline("Expected result:"),
68-
colors.white(expected),
69+
colors.white(expected?.toString() || ""),
6970
);
7071

7172
if (ex.requestError) {
@@ -123,7 +124,7 @@ class ConsoleStreamingReporter implements Reporter {
123124
const failedMethods = Object.keys(failedCallsByMethod);
124125

125126
//take into account failed methods
126-
const missingMethods: string[] = options.openrpcDocument.methods
127+
const missingMethods: string[] = (options.openrpcDocument.methods as MethodObject[])
127128
.map(({name}) => name)
128129
.filter((methodName) => !passingMethods.includes(methodName))
129130
.filter((methodName) => !failedMethods.includes(methodName));

src/reporters/console.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class ConsoleReporter implements Reporter {
7676
console.log(
7777
colors.magenta("\t\t\t \->"),
7878
colors.white.underline("Expected result:"),
79-
colors.white(expected),
79+
colors.white(expected?.toString() || ""),
8080
);
8181

8282
if (ex.requestError) {

src/rules/examples-rule.test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,63 @@ describe("ExamplesRule", () => {
162162
const result = rule.validateCall(calls[0]);
163163
expect(result.valid).toBe(false);
164164
});
165+
it("should handle errors", () => {
166+
const rule = new ExamplesRule();
167+
const openrpcDocument = {
168+
openrpc: "1.0.0",
169+
info: {
170+
title: "my api",
171+
version: "0.0.0-development",
172+
},
173+
servers: [
174+
{
175+
name: "my api",
176+
url: "http://localhost:3333",
177+
},
178+
],
179+
methods: [
180+
{
181+
name: "foo",
182+
params: [],
183+
result: {
184+
name: "fooResult",
185+
schema: {
186+
type: "string",
187+
unevaluatedProperties: false,
188+
},
189+
},
190+
examples: [
191+
{
192+
name: "fooExample",
193+
summary: "foo example",
194+
description: "this is an example of foo",
195+
params: [
196+
{
197+
name: "barParam",
198+
value: "bar",
199+
},
200+
{
201+
name: "barParam2",
202+
value: "bar",
203+
}
204+
],
205+
result: {
206+
name: "fooResult",
207+
value: "potato",
208+
}
209+
}
210+
]
211+
},
212+
],
213+
} as any;
214+
const calls = rule.getCalls(openrpcDocument, openrpcDocument.methods[0]);
215+
calls[0].error = {
216+
code: 123,
217+
message: "error",
218+
data: {}
219+
};
220+
const result = rule.validateCall(calls[0]);
221+
expect(result.valid).toBe(false);
222+
expect(result.reason).toMatch('expected "potato" but got an error: {"code":123,"message":"error","data":{}}');
223+
});
165224
});

src/rules/examples-rule.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ class ExamplesRule implements Rule {
7272
if (!call.valid) {
7373
call.reason = `expected ${JSON.stringify(call.expectedResult)} but got ${JSON.stringify(call.result)}`;
7474
}
75+
} else if (call.error) {
76+
call.valid = false;
77+
call.reason = `expected ${JSON.stringify(call.expectedResult)} but got an error: ${JSON.stringify(call.error)}`;
78+
} else {
79+
call.valid = false;
7580
}
7681
return call;
7782
}

src/rules/json-schema-faker-rule.test.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11

2+
import { Call } from "../coverage";
23
import JsonSchemaFakerRule from "./json-schema-faker-rule";
34

45
describe("JsonSchemaFakerRule", () => {
@@ -37,7 +38,7 @@ describe("JsonSchemaFakerRule", () => {
3738
});
3839
it("should handle errors within ajv when validating", () => {
3940
const rule = new JsonSchemaFakerRule();
40-
const Call = {
41+
const call: Call = {
4142
title: 'test call',
4243
methodName: "foo",
4344
params: [],
@@ -47,8 +48,26 @@ describe("JsonSchemaFakerRule", () => {
4748
unevaluatedProperties: false,
4849
},
4950
};
50-
const result = rule.validateCall(Call);
51+
const result = rule.validateCall(call);
5152
expect(result.valid).toBe(false);
5253
expect(result.reason).toMatch('unknown keyword: "unevaluatedProperties"');
5354
});
55+
it("should produce a readable error for reason", () => {
56+
const rule = new JsonSchemaFakerRule();
57+
const call: Call = {
58+
title: 'test call',
59+
methodName: "foo",
60+
params: [],
61+
url: "http://localhost:3333",
62+
resultSchema: {
63+
type: "boolean",
64+
},
65+
result: "potato",
66+
};
67+
const result = rule.validateCall(call);
68+
expect(result.valid).toBe(false);
69+
expect(result.reason).toContain('expected:\n "potato"');
70+
expect(result.reason).toContain('to match schema:');
71+
expect(result.reason).toContain(JSON.stringify(call.resultSchema, null, 2));
72+
});
5473
});

src/rules/json-schema-faker-rule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class JsonSchemaFakerRule implements Rule {
6666
ajv.validate(call.resultSchema, call.result);
6767
if (ajv.errors && ajv.errors.length > 0) {
6868
call.valid = false;
69-
call.reason = JSON.stringify(ajv.errors);
69+
call.reason = `expected:\n ${JSON.stringify(call.result, null, 2)}\n to match schema: \n${JSON.stringify(call.resultSchema, null, 2)}\nbut got:\n ${JSON.stringify(ajv.errors, null, 2)}`;
7070
} else {
7171
call.valid = true;
7272
}

0 commit comments

Comments
 (0)