From afc4bd60988a6de6a22653f49504b50a92c983ad Mon Sep 17 00:00:00 2001 From: Tomasz Ducin <tomasz.ducin@gmail.com> Date: Wed, 6 May 2015 13:34:57 +0200 Subject: [PATCH] IoC container, #12 --- .travis.yml | 1 + README.md | 4 +++- lib/index.js | Bin 554 -> 823 bytes lib/types/object.js | Bin 1791 -> 1864 bytes lib/types/string.js | Bin 2575 -> 2639 bytes lib/util/container.js | Bin 0 -> 591 bytes lib/util/random.js | Bin 1419 -> 1466 bytes package.json | 9 +++++++-- spec/core/customize/faker-extend.js | 20 ++++++++++++++++++++ spec/core/customize/faker.json | 18 ++++++++++++++++++ spec/core/formats/semver.js | 9 +++++++++ spec/core/issues/format.json | 3 ++- spec/helpers.coffee | 2 +- spec/main-spec.coffee | 10 ++++++---- spec/{formats.coffee => validator.coffee} | 5 ----- 15 files changed, 67 insertions(+), 14 deletions(-) create mode 100644 lib/util/container.js create mode 100644 spec/core/customize/faker-extend.js create mode 100644 spec/core/customize/faker.json create mode 100644 spec/core/formats/semver.js rename spec/{formats.coffee => validator.coffee} (78%) diff --git a/.travis.yml b/.travis.yml index 1c368728..22f5006f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,7 @@ language: node_js branches: only: - master + - extending-dependencies node_js: - 0.10 before_script: diff --git a/README.md b/README.md index ff8b8cc6..e8c20281 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,9 @@ Fake your schemas! [](https://travis-ci.org/pateketrueke/json-schema-faker) [](http://badge.fury.io/js/json-schema-faker) [](https://coveralls.io/r/pateketrueke/json-schema-faker?branch=master) -`json-schema-faker` supports (currently) the JSON-Schema specification **draft-04** only (?) +[](http://badge.fury.io/js/json-schema-faker) + +`json-schema-faker` supports (currently) the JSON-Schema specification **draft-04** only. ```javascript var jsf = require('json-schema-faker'); diff --git a/lib/index.js b/lib/index.js index 9d1910a6ff4ba410125af828c143e6d0eaebbee4..add3f48753d677aeffd0076ddc03c12a9e3645bb 100644 GIT binary patch delta 280 zcmZ3*vYl<iC&v1+#3F^{{JfIH%)Hbh1zUxp)WXutqEroaJ^j*>%p85VoVunp7ni=i zLWsYMzm-C9Mq*BmLV2n}Zhl#+LS~6VNxnj&LUC$AVo_oVNHnE1C)J)Stu!yWBr`ux zA-S};BtJK^DpeyrH7~U&u_V99FEKY&CsQFUPg9|qOF=<F0pt|8tb#4VS$gTIC8+u| rt-(6rj?*hfSD~YjmWR-zscAj=9iyTg)c90AxO+hMz}z;Ohe-|q?^$Ac delta 15 Xcmdnawu)uLC&tP5m}Do*FiQgfGXn)| diff --git a/lib/types/object.js b/lib/types/object.js index 9ebd2fb00fd64e8e36b611660d97a4290c275542..752a16b51a3f741c806200e2f538c57e1c8397bf 100644 GIT binary patch delta 181 zcmey*dxB3!y|g%0p}3?dGr2_Fnv1I}u}C2~Kd&S)GcUDh;sz0MI7eMmhf6_0L7^xy zFC{;B;s%k4c^Ro`iP@<|3bqI>dg-Yp8tPzSn1-OlycE}p0!&pPlTs@RVCtY;1zQEE W4n3&I#`AJ4o4>M5W}G~MEgt|1(>@#k delta 123 zcmX@X_n$Xdy|g%0p}3?dGr2_Fnv1I}u}C2;F*~(L!B(LtwXig^C{;rpETFEb!=<30 tpb(Upmy%jhfGl4G5mMKL7%<VHd9yyFI12|<sorKc*2#=;-sGEX`2f<hC~W`$ diff --git a/lib/types/string.js b/lib/types/string.js index 8438f3a60d66d958e313116df311a94b04be5d7c..ff328ad72dcd7082e852525bdbb0c6857f516803 100644 GIT binary patch delta 180 zcmeAdIWJPLURs>0P+U@!nOvf7&BaxgSfr4gpI4HYnU`9mV5?A+T3DJ{l&Ybwr>9?9 zl9{6qmsHn;Do;zy1}lZj=%uHYXsCmQ)irgv6ciK`oHG*hl2a9I5oSXaB~Nq_RCcW> Zz?3OU%u7kFD1aI;u~>`Ib>qB3P5?gaI^X~R delta 141 zcmX>v(l1i4URs>0P+U@!nOvf7&BaxgSfr4an4MarV5?A+T3DJ{l&YZ)7EssJ;Zjgg zP;kyj%u7y1mQDtXz~qAx^HNeP3Xl~PL4?#bp%y?iDA+3GrB2*0$C$eDULhv{h)^wx diff --git a/lib/util/container.js b/lib/util/container.js new file mode 100644 index 0000000000000000000000000000000000000000..df9972353f946c20b0af69527a5157b9bd96775a GIT binary patch literal 591 zcmY!{ElyP^E-A`PE>XAU;?mbwC@x7X$xK!#N-ZqSEJ`g_&{fDt%uC5hRY=M&$xz75 z%Ph%E%uz^5ElABvNzF^HRLIFsOi|ECNi8T=D9_BvQAkQvNGwat%t=hjNd+00n46!M zu8?1nky@mXo1apelUl5)kXWoxo|=;bqDwMTE8#jb^OAE)Q&LkDk}4IFit@{gQ;Raw zDis`qL%7NkixiUc^GXsk^HPfxY!#}x6ciK`(h{>%i>zSI)ldiX)irg%lF1o~dC93r zvJe57bWvhnN@_&`k~~yIU6ZTUnu`nKTD{bYg8ZVAVz`yXsU=nlX{C9|C7Jno8hMGi zsX7W}i8-aInou`^+y!@OG)O_Lf-OXeHP~IX5a*?17=&y>W}1RVNo7H5ej38tFvDzZ z71T@fQc}}0^HNjPk#&LWF3Bj$FIUJ*EmsIiO-n6G%}Y*oEh@?{(ok1YSI|}fIY>cU yL0t(s7E|(5i}PaCOB7NoGK)*})HNY41sPWhb~A`olv+|+l!xvdh{{?}kO2UctGcHE literal 0 HcmV?d00001 diff --git a/lib/util/random.js b/lib/util/random.js index d9fd0cd60437af6ebe3e94a23cef5cfe4dc416b4..1d7404554b592d522833b407141d7f84f18fa03d 100644 GIT binary patch delta 90 zcmeC?-o+iLURs>0P+U@!nOvf7&BaxgSfr4gpI4HYnU`9mV5?A+T3DJ{l&Ybwrw<oa X*MureOUwo<g3IWor<QC~%47uqV$2?8 delta 43 ycmdnR-Oa6}URs>0P+U@!nOvf7&BaxgSfr4an4MarV5?A+T3DJ{l)5o6lNA6q;|_fQ diff --git a/package.json b/package.json index 727f37ba..d472c77a 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,17 @@ { "name": "json-schema-faker", - "version": "0.1.6", + "version": "0.1.8", "description": "JSON-Schema + Faker", "main": "lib/index.js", "author": { "name": "Alvaro Cabrera", "email": "pateketrueke@gmail.com" }, + "contributors": [{ + "name": "Tomasz Ducin", + "email": "tomasz.ducin@gmail.com", + "url": "http://ducin.it" + }], "repository": { "type": "git", "url": "https://github.com/pateketrueke/json-schema-faker" @@ -23,7 +28,7 @@ "z-schema": "^3.3.1" }, "dependencies": { - "chance": "^0.7.4", + "chance": "^0.7.5", "deref": "^0.2.8", "faker": "^2.1.2", "randexp": "^0.4.0" diff --git a/spec/core/customize/faker-extend.js b/spec/core/customize/faker-extend.js new file mode 100644 index 00000000..c66c5a6b --- /dev/null +++ b/spec/core/customize/faker-extend.js @@ -0,0 +1,20 @@ +module.exports = { + extend: function (faker) { + faker.locale = "de"; + + faker.mixin = function (namespace, fnObject) { + faker[namespace] = fnObject; + }; + + faker.mixin('custom', { + statement: function (length) { + return faker.name.firstName() + " has " + faker.finance.amount() + " on " + faker.finance.account(length) + "."; + } + }); + + return faker; + }, + register: function(jsfaker) { + return jsfaker.customize('faker', this.extend); + } +}; diff --git a/spec/core/customize/faker.json b/spec/core/customize/faker.json new file mode 100644 index 00000000..1f73c00c --- /dev/null +++ b/spec/core/customize/faker.json @@ -0,0 +1,18 @@ +[ + { + "description": "generated extended faker", + "tests": [ + { + "description": "should handle faker.custom.statement", + "schema": { + "type": "string", + "faker": { + "custom.statement": [19] + } + }, + "valid": true, + "require": "core/customize/faker-extend" + } + ] + } +] diff --git a/spec/core/formats/semver.js b/spec/core/formats/semver.js new file mode 100644 index 00000000..b5b813b4 --- /dev/null +++ b/spec/core/formats/semver.js @@ -0,0 +1,9 @@ +module.exports = { + register: function (jsf) { + return jsf.formats({ + semver: function (gen, schema) { + return gen.randexp('\\d\\.\\d\\.[1-9]\\d?'); + } + }); + } +}; diff --git a/spec/core/issues/format.json b/spec/core/issues/format.json index d5f6b747..b1096f1e 100644 --- a/spec/core/issues/format.json +++ b/spec/core/issues/format.json @@ -16,7 +16,8 @@ "type": "string", "format": "semver" }, - "valid": true + "valid": true, + "require": "core/formats/semver" }, { "description": "should support faker/chance on numeric types", diff --git a/spec/helpers.coffee b/spec/helpers.coffee index bb009d69..f5b6efb7 100644 --- a/spec/helpers.coffee +++ b/spec/helpers.coffee @@ -3,7 +3,7 @@ clone = require('clone') ZSchema = require('z-schema') JaySchema = require('jayschema') -formatValidators = require('./formats').validate +formatValidators = require('./validator').validate [tv4, ZSchema].map formatValidators diff --git a/spec/main-spec.coffee b/spec/main-spec.coffee index 7ee94e44..d67b64a3 100644 --- a/spec/main-spec.coffee +++ b/spec/main-spec.coffee @@ -3,8 +3,6 @@ glob = require('glob') deref = require('deref') jsfaker = require('../lib') -require('./formats').register(jsfaker) - pick = (obj, key) -> parts = key.split('.') obj = obj[parts.shift()] while parts.length @@ -39,6 +37,9 @@ glob.sync("#{__dirname}/**/*.json").forEach (file) -> error = '' sample = try + if test.require + wrapper = require('./' + test.require) + wrapper.register(jsfaker) jsfaker(schema, refs) catch e error = e.message @@ -60,6 +61,7 @@ glob.sync("#{__dirname}/**/*.json").forEach (file) -> refs = [schema] if schema.id expect(sample).toHaveSchema schema, refs catch e - console.log JSON.stringify(schema, null, 2) - console.log JSON.stringify(sample, null, 2) + console.log suite.description + console.log 'schema', JSON.stringify(schema, null, 2) + console.log 'sample', JSON.stringify(sample, null, 2) throw e diff --git a/spec/formats.coffee b/spec/validator.coffee similarity index 78% rename from spec/formats.coffee rename to spec/validator.coffee index 990e071b..51fbe716 100644 --- a/spec/formats.coffee +++ b/spec/validator.coffee @@ -1,11 +1,6 @@ semver = require('semver') module.exports = - register: (jsf) -> - jsf.formats - semver: (gen, schema) -> - gen.randexp '\\d\\.\\d\\.[1-9]\\d?' - validate: (v) -> registry = v.addFormat or v.registerFormat msgOnFail = not v.registerFormat -- GitLab