Skip to content

Commit 44c804f

Browse files
poojabelapooja-bruno
authored andcommitted
feat: support tv4 as a inbuilt lib (usebruno#4589)
1 parent 95ffa61 commit 44c804f

File tree

6 files changed

+52
-1
lines changed

6 files changed

+52
-1
lines changed

packages/bruno-js/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"node-vault": "^0.10.2",
3636
"path": "^0.12.7",
3737
"quickjs-emscripten": "^0.29.2",
38+
"tv4": "^1.3.0",
3839
"uuid": "^9.0.0",
3940
"xml2js": "^0.6.2"
4041
},

packages/bruno-js/src/runtime/script-runtime.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const CryptoJS = require('crypto-js');
3030
const NodeVault = require('node-vault');
3131
const xml2js = require('xml2js');
3232
const cheerio = require('cheerio');
33+
const tv4 = require('tv4');
3334
const { executeQuickJsVmAsync } = require('../sandbox/quickjs');
3435

3536
class ScriptRuntime {
@@ -151,6 +152,7 @@ class ScriptRuntime {
151152
'crypto-js': CryptoJS,
152153
'xml2js': xml2js,
153154
cheerio,
155+
tv4,
154156
...whitelistedModules,
155157
fs: allowScriptFilesystemAccess ? fs : undefined,
156158
'node-vault': NodeVault
@@ -285,6 +287,7 @@ class ScriptRuntime {
285287
'crypto-js': CryptoJS,
286288
'xml2js': xml2js,
287289
cheerio,
290+
tv4,
288291
...whitelistedModules,
289292
fs: allowScriptFilesystemAccess ? fs : undefined,
290293
'node-vault': NodeVault

packages/bruno-js/src/runtime/test-runtime.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ const CryptoJS = require('crypto-js');
3232
const NodeVault = require('node-vault');
3333
const xml2js = require('xml2js');
3434
const cheerio = require('cheerio');
35+
const tv4 = require('tv4');
3536
const { executeQuickJsVmAsync } = require('../sandbox/quickjs');
3637

3738
const getResultsSummary = (results) => {
@@ -209,6 +210,7 @@ class TestRuntime {
209210
'crypto-js': CryptoJS,
210211
'xml2js': xml2js,
211212
cheerio,
213+
tv4,
212214
...whitelistedModules,
213215
fs: allowScriptFilesystemAccess ? fs : undefined,
214216
'node-vault': NodeVault

packages/bruno-js/src/sandbox/bundle-libraries.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,24 @@ const bundleLibraries = async () => {
1212
import btoa from "btoa";
1313
import atob from "atob";
1414
import * as CryptoJS from "@usebruno/crypto-js";
15+
import tv4 from "tv4";
1516
globalThis.expect = expect;
1617
globalThis.assert = assert;
1718
globalThis.moment = moment;
1819
globalThis.btoa = btoa;
1920
globalThis.atob = atob;
2021
globalThis.Buffer = Buffer;
2122
globalThis.CryptoJS = CryptoJS;
23+
globalThis.tv4 = tv4;
2224
globalThis.requireObject = {
2325
...(globalThis.requireObject || {}),
2426
'chai': { expect, assert },
2527
'moment': moment,
2628
'buffer': { Buffer },
2729
'btoa': btoa,
2830
'atob': atob,
29-
'crypto-js': CryptoJS
31+
'crypto-js': CryptoJS,
32+
'tv4': tv4
3033
};
3134
`;
3235

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
meta {
2+
name: tv4
3+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
meta {
2+
name: tv4
3+
type: http
4+
seq: 1
5+
}
6+
7+
post {
8+
url: {{host}}/api/echo/json
9+
body: json
10+
auth: inherit
11+
}
12+
13+
body:json {
14+
{
15+
"name": "John",
16+
"age": 30
17+
}
18+
}
19+
20+
tests {
21+
const tv4 = require("tv4")
22+
23+
const schema = {
24+
type: 'object',
25+
properties: {
26+
name: { type: 'string' },
27+
age: { type: 'number' }
28+
}
29+
};
30+
31+
let responseData = res.getBody();
32+
33+
let isValid = tv4.validate(responseData, schema);
34+
35+
test("Response body matches expected schema", function () {
36+
expect(isValid, tv4.error ? tv4.error.message : "").to.be.true;
37+
});
38+
39+
}

0 commit comments

Comments
 (0)