diff --git a/.changeset/tricky-bikes-protect.md b/.changeset/tricky-bikes-protect.md new file mode 100644 index 00000000..a7110e4e --- /dev/null +++ b/.changeset/tricky-bikes-protect.md @@ -0,0 +1,5 @@ +--- +"counterfact": minor +--- + +add a tools.randomFromSchema(schema: JSONSchema) function diff --git a/package.json b/package.json index a0420d03..cd51c189 100644 --- a/package.json +++ b/package.json @@ -34,14 +34,15 @@ "eslint": "8.18.0", "eslint-config-hardcore": "24.5.0", "eslint-formatter-github-annotations": "0.1.0", + "husky": "8.0.1", "jest": "28.1.2", "koa": "2.13.4", - "husky": "8.0.1", "nodemon": "2.0.18", "stryker-cli": "1.0.2", "supertest": "6.2.3" }, "dependencies": { - "chokidar": "^3.5.3" + "chokidar": "^3.5.3", + "json-schema-faker": "^0.5.0-rcv.44" } } diff --git a/src/tools.js b/src/tools.js index 6c04cb9b..c1ce71d1 100644 --- a/src/tools.js +++ b/src/tools.js @@ -1,3 +1,5 @@ +import JSONSchemaFaker from "json-schema-faker"; + export class Tools { constructor({ headers = {} } = {}) { this.headers = headers; @@ -25,4 +27,8 @@ export class Tools { ); }); } + + randomFromSchema(schema) { + return JSONSchemaFaker.generate(schema); + } } diff --git a/test/tools.test.js b/test/tools.test.js index 30559b1c..4a7ba024 100644 --- a/test/tools.test.js +++ b/test/tools.test.js @@ -36,4 +36,10 @@ describe("tools", () => { expect(tools.accepts(contentType)).toBe(false); } ); + + it("randomFromSchema() returns a value (the implementation is in a third party library)", () => { + const tools = new Tools(); + + expect(typeof tools.randomFromSchema({ type: "integer" })).toBe("number"); + }); }); diff --git a/yarn.lock b/yarn.lock index 542fb88b..a3f6b772 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3357,6 +3357,11 @@ call-bind@^1.0.0, call-bind@^1.0.2: function-bind "^1.1.1" get-intrinsic "^1.0.2" +call-me-maybe@^1.0.1: + version "1.0.1" + resolved "https://registry.yarnpkg.com/call-me-maybe/-/call-me-maybe-1.0.1.tgz#26d208ea89e37b5cbde60250a15f031c16a4d66b" + integrity sha512-wCyFsDQkKPwwF8BDwOiWNx/9K45L/hvggQiDbve+viMNMQnWhrlYIuBk09offfwCRtCO9P6XwUttufzU11WCVw== + callsites@^3.0.0: version "3.1.0" resolved "https://registry.yarnpkg.com/callsites/-/callsites-3.1.0.tgz#b3630abd8943432f54b3f0519238e33cd7df2f73" @@ -5079,6 +5084,11 @@ format-io@^2.0.0: dependencies: currify "^4.0.0" +format-util@^1.0.3: + version "1.0.5" + resolved "https://registry.yarnpkg.com/format-util/-/format-util-1.0.5.tgz#1ffb450c8a03e7bccffe40643180918cc297d271" + integrity sha512-varLbTj0e0yVyRpqQhuWV+8hlePAgaoFRhNFj50BNjEIrw1/DphHSObtqwskVCPWNgzwPoQrZAbfa/SBiicNeg== + formidable@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/formidable/-/formidable-2.0.1.tgz#4310bc7965d185536f9565184dee74fbb75557ff" @@ -6414,7 +6424,7 @@ js-types@^1.0.0: resolved "https://registry.yarnpkg.com/js-types/-/js-types-1.0.0.tgz#d242e6494ed572ad3c92809fc8bed7f7687cbf03" integrity sha1-0kLmSU7Vcq08koCfyL7X92h8vwM= -js-yaml@^3.13.0, js-yaml@^3.13.1, js-yaml@^3.6.1: +js-yaml@^3.12.1, js-yaml@^3.13.0, js-yaml@^3.13.1, js-yaml@^3.6.1: version "3.14.1" resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-3.14.1.tgz#dae812fdb3825fa306609a8717383c50c36a0537" integrity sha512-okMH7OXXJ7YrN9Ok3/SXrnu4iX9yOk+25nqX4imS2npuvTYDmo/QEZoqwZkYaIDk3jVvBOTOIEgEhaLOynBS9g== @@ -6449,6 +6459,23 @@ json-parse-even-better-errors@^2.3.0: resolved "https://registry.yarnpkg.com/json-parse-even-better-errors/-/json-parse-even-better-errors-2.3.1.tgz#7c47805a94319928e05777405dc12e1f7a4ee02d" integrity sha512-xyFwyhro/JEof6Ghe2iz2NcXoj2sloNsWr/XsERDK/oiPCfaNhl5ONfp+jQdAZRQQ0IJWNzH9zIZF7li91kh2w== +json-schema-faker@^0.5.0-rcv.44: + version "0.5.0-rcv.44" + resolved "https://registry.yarnpkg.com/json-schema-faker/-/json-schema-faker-0.5.0-rcv.44.tgz#dca9b8f3a844c0426cf4c581cc8d7f4428f29f90" + integrity sha512-MbDxYFsPXTVMawW1Y6zEU7QhfwsT+ZJ2d+LI8n57Y8+Xw1Cdx1hITgsFTLNOJ1lDMHZqWeXGGgMbc1hW0BGisg== + dependencies: + json-schema-ref-parser "^6.1.0" + jsonpath-plus "^5.1.0" + +json-schema-ref-parser@^6.1.0: + version "6.1.0" + resolved "https://registry.yarnpkg.com/json-schema-ref-parser/-/json-schema-ref-parser-6.1.0.tgz#30af34aeab5bee0431da805dac0eb21b574bf63d" + integrity sha512-pXe9H1m6IgIpXmE5JSb8epilNTGsmTb2iPohAXpOdhqGFbQjNeHHsZxU+C8w6T81GZxSPFLeUoqDJmzxx5IGuw== + dependencies: + call-me-maybe "^1.0.1" + js-yaml "^3.12.1" + ono "^4.0.11" + json-schema-traverse@^0.4.1: version "0.4.1" resolved "https://registry.yarnpkg.com/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz#69f6a87d9513ab8bb8fe63bdb0979c448e684660" @@ -6497,6 +6524,11 @@ jsonfile@^6.0.1: optionalDependencies: graceful-fs "^4.1.6" +jsonpath-plus@^5.1.0: + version "5.1.0" + resolved "https://registry.yarnpkg.com/jsonpath-plus/-/jsonpath-plus-5.1.0.tgz#2fc4b2e461950626c98525425a3a3518b85af6c3" + integrity sha512-890w2Pjtj0iswAxalRlt2kHthi6HKrXEfZcn+ZNZptv7F3rUGIeDuZo+C+h4vXBHLEsVjJrHeCm35nYeZLzSBQ== + "jsx-ast-utils@^2.4.1 || ^3.0.0", jsx-ast-utils@^3.2.1, jsx-ast-utils@^3.2.2: version "3.2.2" resolved "https://registry.yarnpkg.com/jsx-ast-utils/-/jsx-ast-utils-3.2.2.tgz#6ab1e52c71dfc0c0707008a91729a9491fe9f76c" @@ -7571,6 +7603,13 @@ only@~0.0.2: resolved "https://registry.yarnpkg.com/only/-/only-0.0.2.tgz#2afde84d03e50b9a8edc444e30610a70295edfb4" integrity sha1-Kv3oTQPlC5qO3EROMGEKcCle37Q= +ono@^4.0.11: + version "4.0.11" + resolved "https://registry.yarnpkg.com/ono/-/ono-4.0.11.tgz#c7f4209b3e396e8a44ef43b9cedc7f5d791d221d" + integrity sha512-jQ31cORBFE6td25deYeD80wxKBMj+zBmHTrVxnc6CKhx8gho6ipmWM5zj/oeoqioZ99yqBls9Z/9Nss7J26G2g== + dependencies: + format-util "^1.0.3" + optionator@^0.9.1: version "0.9.1" resolved "https://registry.yarnpkg.com/optionator/-/optionator-0.9.1.tgz#4f236a6373dae0566a6d43e1326674f50c291499"