Skip to content

Commit

Permalink
0.5.0-rc7
Browse files Browse the repository at this point in the history
  • Loading branch information
pateketrueke committed Jun 24, 2017
1 parent b9cf086 commit 328c17f
Show file tree
Hide file tree
Showing 5 changed files with 439 additions and 71 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-schema-faker",
"version": "0.5.0-rc6",
"version": "0.5.0-rc7",
"description": "JSON-Schema + fake data generators",
"homepage": "http://json-schema-faker.js.org",
"main": "dist/json-schema-faker.js",
Expand Down
67 changes: 38 additions & 29 deletions dist/json-schema-faker.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*!
* json-schema-faker library v0.5.0-rc6
* json-schema-faker library v0.5.0-rc7
* http://json-schema-faker.js.org
* @preserve
*
* Copyright (c) 2014-2017 Alvaro Cabrera & Tomasz Ducin
* Released under the MIT license
*
* Date: 2017-06-02 17:45:42.194Z
* Date: 2017-06-24 09:28:22.082Z
*/

(function (global, factory) {
Expand Down Expand Up @@ -17506,26 +17506,19 @@ function typecast(value, schema) {
return value;
}
}
function clone(arr) {
var out = [];
arr.forEach(function (item, index) {
if (typeof item === 'object' && item !== null) {
out[index] = Array.isArray(item) ? clone(item) : merge({}, item);
}
else {
out[index] = item;
}
});
return out;
}
// TODO refactor merge function
function merge(a, b) {
for (var key in b) {
if (typeof b[key] !== 'object' || b[key] === null) {
a[key] = b[key];
}
else if (Array.isArray(b[key])) {
a[key] = (a[key] || []).concat(clone(b[key]));
a[key] = a[key] || [];
// fix #292 - skip duplicated values from merge object (b)
b[key].forEach(function (value) {
if (a[key].indexOf(value)) {
a[key].push(value);
}
});
}
else if (typeof a[key] !== 'object' || a[key] === null || Array.isArray(a[key])) {
a[key] = merge({}, b[key]);
Expand Down Expand Up @@ -17570,7 +17563,6 @@ var utils = {
getSubAttribute: getSubAttribute,
hasProperties: hasProperties,
typecast: typecast,
clone: clone,
merge: merge,
clean: clean,
short: short,
Expand Down Expand Up @@ -18129,8 +18121,8 @@ function traverse(schema, path, resolve) {
return random.pick(schema.enum);
}
// thunks can return sub-schemas
if (typeof schema.thunk === 'function') {
return traverse(schema.thunk(), path, resolve);
if (typeof schema === 'function') {
return traverse(schema(), path, resolve);
}
if (typeof schema.generate === 'function') {
return utils.typecast(schema.generate(), schema);
Expand Down Expand Up @@ -18187,7 +18179,7 @@ function isKey(prop) {
return prop === 'enum' || prop === 'default' || prop === 'required' || prop === 'definitions';
}
// TODO provide types
function run(schema, container) {
function run(refs, schema, container) {
try {
return traverse(schema, [], function reduce(sub, maxReduceDepth) {
if (typeof maxReduceDepth === 'undefined') {
Expand All @@ -18203,7 +18195,11 @@ function run(schema, container) {
}
if (typeof sub.$ref === 'string') {
if (sub.$ref.indexOf('#/') === -1) {
throw new Error('Reference not found: ' + sub.$ref);
var ref = deref.util.findByRef(sub.$ref, refs);
if (!ref) {
throw new Error('Reference not found: ' + sub.$ref);
}
return ref;
}
// just remove the reference
delete sub.$ref;
Expand All @@ -18217,18 +18213,19 @@ function run(schema, container) {
schemas.forEach(function (subSchema) {
var _sub = reduce(subSchema, maxReduceDepth + 1);
// call given thunks if present
utils.merge(sub, typeof _sub.thunk === 'function'
? _sub.thunk()
utils.merge(sub, typeof _sub === 'function'
? _sub()
: _sub);
});
}
if (Array.isArray(sub.oneOf || sub.anyOf)) {
var key = sub.oneOf ? 'oneOf' : 'anyOf';
var mix = sub.oneOf || sub.anyOf;
delete sub.anyOf;
delete sub.oneOf;
return {
thunk: function () { return random.pick(mix); }
return function () {
var copy = utils.merge({}, sub);
utils.merge(copy, random.pick(mix));
return copy;
};
}
for (var prop in sub) {
Expand All @@ -18253,7 +18250,7 @@ var container = new Container();
function getRefs(refs) {
var $refs = {};
if (Array.isArray(refs)) {
refs.forEach(function (schema) {
refs.map(deref.util.normalizeSchema).forEach(function (schema) {
$refs[schema.id] = schema;
});
}
Expand All @@ -18265,7 +18262,7 @@ function getRefs(refs) {
var jsf = function (schema, refs) {
var $ = deref();
var $refs = getRefs(refs);
return run($(schema, $refs, true), container);
return run($refs, $(schema, $refs, true), container);
};
jsf.resolve = function (schema, refs, cwd) {
if (typeof refs === 'string') {
Expand All @@ -18275,8 +18272,20 @@ jsf.resolve = function (schema, refs, cwd) {
// normalize basedir (browser aware)
cwd = cwd || (typeof process !== 'undefined' ? process.cwd() : '');
cwd = cwd.replace(/\/+$/, '') + '/';
var $refs = getRefs(refs);
// identical setup as json-schema-sequelizer
var fixedRefs = {
order: 300,
canRead: true,
read: function (file, callback) {
callback(null, deref.util.findByRef(cwd !== '/'
? file.url.replace(cwd, '')
: file.url, $refs));
},
};
return $RefParser
.dereference(cwd, schema, {
resolve: { fixedRefs: fixedRefs },
dereference: {
circular: 'ignore',
},
Expand All @@ -18299,7 +18308,7 @@ jsf.define = function (name, cb) {
jsf.locate = function (name) {
return container.get(name);
};
var VERSION="0.5.0-rc6";
var VERSION="0.5.0-rc7";
jsf.version = VERSION;

var index = jsf;
Expand Down
63 changes: 36 additions & 27 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,26 +285,19 @@ function typecast(value, schema) {
return value;
}
}
function clone(arr) {
var out = [];
arr.forEach(function (item, index) {
if (typeof item === 'object' && item !== null) {
out[index] = Array.isArray(item) ? clone(item) : merge({}, item);
}
else {
out[index] = item;
}
});
return out;
}
// TODO refactor merge function
function merge(a, b) {
for (var key in b) {
if (typeof b[key] !== 'object' || b[key] === null) {
a[key] = b[key];
}
else if (Array.isArray(b[key])) {
a[key] = (a[key] || []).concat(clone(b[key]));
a[key] = a[key] || [];
// fix #292 - skip duplicated values from merge object (b)
b[key].forEach(function (value) {
if (a[key].indexOf(value)) {
a[key].push(value);
}
});
}
else if (typeof a[key] !== 'object' || a[key] === null || Array.isArray(a[key])) {
a[key] = merge({}, b[key]);
Expand Down Expand Up @@ -349,7 +342,6 @@ var utils = {
getSubAttribute: getSubAttribute,
hasProperties: hasProperties,
typecast: typecast,
clone: clone,
merge: merge,
clean: clean,
short: short,
Expand Down Expand Up @@ -908,8 +900,8 @@ function traverse(schema, path, resolve) {
return random.pick(schema.enum);
}
// thunks can return sub-schemas
if (typeof schema.thunk === 'function') {
return traverse(schema.thunk(), path, resolve);
if (typeof schema === 'function') {
return traverse(schema(), path, resolve);
}
if (typeof schema.generate === 'function') {
return utils.typecast(schema.generate(), schema);
Expand Down Expand Up @@ -966,7 +958,7 @@ function isKey(prop) {
return prop === 'enum' || prop === 'default' || prop === 'required' || prop === 'definitions';
}
// TODO provide types
function run(schema, container) {
function run(refs, schema, container) {
try {
return traverse(schema, [], function reduce(sub, maxReduceDepth) {
if (typeof maxReduceDepth === 'undefined') {
Expand All @@ -982,7 +974,11 @@ function run(schema, container) {
}
if (typeof sub.$ref === 'string') {
if (sub.$ref.indexOf('#/') === -1) {
throw new Error('Reference not found: ' + sub.$ref);
var ref = deref.util.findByRef(sub.$ref, refs);
if (!ref) {
throw new Error('Reference not found: ' + sub.$ref);
}
return ref;
}
// just remove the reference
delete sub.$ref;
Expand All @@ -996,18 +992,19 @@ function run(schema, container) {
schemas.forEach(function (subSchema) {
var _sub = reduce(subSchema, maxReduceDepth + 1);
// call given thunks if present
utils.merge(sub, typeof _sub.thunk === 'function'
? _sub.thunk()
utils.merge(sub, typeof _sub === 'function'
? _sub()
: _sub);
});
}
if (Array.isArray(sub.oneOf || sub.anyOf)) {
var key = sub.oneOf ? 'oneOf' : 'anyOf';
var mix = sub.oneOf || sub.anyOf;
delete sub.anyOf;
delete sub.oneOf;
return {
thunk: function () { return random.pick(mix); }
return function () {
var copy = utils.merge({}, sub);
utils.merge(copy, random.pick(mix));
return copy;
};
}
for (var prop in sub) {
Expand All @@ -1032,7 +1029,7 @@ var container = new Container();
function getRefs(refs) {
var $refs = {};
if (Array.isArray(refs)) {
refs.forEach(function (schema) {
refs.map(deref.util.normalizeSchema).forEach(function (schema) {
$refs[schema.id] = schema;
});
}
Expand All @@ -1044,7 +1041,7 @@ function getRefs(refs) {
var jsf = function (schema, refs) {
var $ = deref();
var $refs = getRefs(refs);
return run($(schema, $refs, true), container);
return run($refs, $(schema, $refs, true), container);
};
jsf.resolve = function (schema, refs, cwd) {
if (typeof refs === 'string') {
Expand All @@ -1054,8 +1051,20 @@ jsf.resolve = function (schema, refs, cwd) {
// normalize basedir (browser aware)
cwd = cwd || (typeof process !== 'undefined' ? process.cwd() : '');
cwd = cwd.replace(/\/+$/, '') + '/';
var $refs = getRefs(refs);
// identical setup as json-schema-sequelizer
var fixedRefs = {
order: 300,
canRead: true,
read: function (file, callback) {
callback(null, deref.util.findByRef(cwd !== '/'
? file.url.replace(cwd, '')
: file.url, $refs));
},
};
return $RefParser
.dereference(cwd, schema, {
resolve: { fixedRefs: fixedRefs },
dereference: {
circular: 'ignore',
},
Expand All @@ -1078,7 +1087,7 @@ jsf.define = function (name, cb) {
jsf.locate = function (name) {
return container.get(name);
};
var VERSION="0.5.0-rc6";
var VERSION="0.5.0-rc7";
jsf.version = VERSION;

module.exports = jsf;
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-schema-faker",
"version": "0.5.0-rc6",
"version": "0.5.0-rc7",
"description": "JSON-Schema + fake data generators",
"homepage": "http://json-schema-faker.js.org",
"main": "lib/index.js",
Expand Down Expand Up @@ -68,6 +68,7 @@
"devDependencies": {
"casual-cjs": "^1.5.10",
"chance": "^1.0.9",
"chokidar": "^1.7.0",
"clone": "^2.1.1",
"codecov": "^2.2.0",
"faker": "^4.1.0",
Expand Down
Loading

0 comments on commit 328c17f

Please sign in to comment.