Skip to content

Commit

Permalink
0.5.0-rc4
Browse files Browse the repository at this point in the history
  • Loading branch information
pateketrueke committed May 31, 2017
1 parent a8f3816 commit 7857285
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 36 deletions.
63 changes: 47 additions & 16 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-rc3
* json-schema-faker library v0.5.0-rc4
* http://json-schema-faker.js.org
* @preserve
*
* Copyright (c) 2014-2017 Alvaro Cabrera & Tomasz Ducin
* Released under the MIT license
*
* Date: 2017-05-15 06:05:25.329Z
* Date: 2017-05-31 01:03:58.568Z
*/

(function (global, factory) {
Expand Down Expand Up @@ -17174,7 +17174,7 @@ var tslib_1 = require$$2$2;

// dynamic proxy for custom generators
function proxy(gen) {
return function (value) {
return function (value, schema, property) {
var fn = value;
var args = [];
// support for nested object, first-key is the generator
Expand Down Expand Up @@ -17202,6 +17202,14 @@ function proxy(gen) {
if (typeof value === 'function') {
value = value.apply(ctx, args);
}
// test for pending callbacks
if (Object.prototype.toString.call(value) === '[object Object]') {
for (var key in value) {
if (typeof value[key] === 'function') {
throw new Error('Cannot resolve value for "' + property + ': ' + fn + '", given: ' + value);
}
}
}
return value;
};
}
Expand Down Expand Up @@ -17260,10 +17268,10 @@ var Container = (function () {
var keys = Object.keys(schema);
var length = keys.length;
while (length--) {
var fn = keys[length];
var fn = keys[length].replace(/^x-/, '');
var gen = this.support[fn];
if (typeof gen === 'function') {
schema.generate = function () { return gen(schema[fn], schema); };
schema.generate = function () { return gen(schema[keys[length]], schema, keys[length]); };
break;
}
}
Expand Down Expand Up @@ -17475,17 +17483,28 @@ function merge(a, b) {
}
return a;
}
function clean(obj, isArray) {
function clean(obj, isArray, requiredProps) {
if (!obj || typeof obj !== 'object') {
return obj;
}
if (Array.isArray(obj)) {
return obj
obj = obj
.map(function (value) { return clean(value, true); })
.filter(function (value) { return value; });
.filter(function (value) { return typeof value !== 'undefined'; });
if (isArray && !obj.length) {
return undefined;
}
return obj;
}
Object.keys(obj).forEach(function (k) {
obj[k] = clean(obj[k]);
if (!requiredProps || requiredProps.indexOf(k) === -1) {
if (Array.isArray(obj[k]) && !obj[k].length) {
delete obj[k];
}
}
else {
obj[k] = clean(obj[k]);
}
});
if (!Object.keys(obj).length && isArray) {
return undefined;
Expand Down Expand Up @@ -18047,6 +18066,9 @@ var typeMap = {
// TODO provide types
function traverse(schema, path, resolve) {
schema = resolve(schema);
if (!schema) {
return;
}
if (Array.isArray(schema.enum)) {
return random.pick(schema.enum);
}
Expand Down Expand Up @@ -18080,7 +18102,7 @@ function traverse(schema, path, resolve) {
}
else {
try {
return typeMap[type](schema, path, resolve, traverse);
return utils.clean(typeMap[type](schema, path, resolve, traverse), null, schema.required);
}
catch (e) {
if (typeof e.path === 'undefined') {
Expand All @@ -18102,7 +18124,7 @@ function traverse(schema, path, resolve) {
copy[prop] = schema[prop];
}
}
return utils.clean(copy);
return copy;
}

function isKey(prop) {
Expand All @@ -18119,18 +18141,27 @@ function run(schema, container) {
return null;
}
// cleanup
delete sub.id;
delete sub.$schema;
if (typeof sub.$ref === 'string' && sub.$ref.indexOf('#/') === -1) {
throw new Error('Only local references are allowed in sync mode.');
if (sub.id && typeof sub.id === 'string') {
delete sub.id;
delete sub.$schema;
}
if (typeof sub.$ref === 'string') {
if (sub.$ref.indexOf('#/') === -1) {
throw new Error('Only local references are allowed in sync mode.');
}
return null;
}
if (Array.isArray(sub.allOf)) {
var schemas = sub.allOf;
delete sub.allOf;
// this is the only case where all sub-schemas
// must be resolved before any merge
schemas.forEach(function (subSchema) {
utils.merge(sub, reduce(subSchema, maxReduceDepth + 1));
var _sub = reduce(subSchema, maxReduceDepth + 1);
// call given thunks if present
utils.merge(sub, typeof _sub.thunk === 'function'
? _sub.thunk()
: _sub);
});
}
if (Array.isArray(sub.oneOf || sub.anyOf)) {
Expand Down
10 changes: 5 additions & 5 deletions dist/json-schema-faker.min.js

Large diffs are not rendered by default.

59 changes: 45 additions & 14 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var tslib_1 = require('tslib');

// dynamic proxy for custom generators
function proxy(gen) {
return function (value) {
return function (value, schema, property) {
var fn = value;
var args = [];
// support for nested object, first-key is the generator
Expand Down Expand Up @@ -34,6 +34,14 @@ function proxy(gen) {
if (typeof value === 'function') {
value = value.apply(ctx, args);
}
// test for pending callbacks
if (Object.prototype.toString.call(value) === '[object Object]') {
for (var key in value) {
if (typeof value[key] === 'function') {
throw new Error('Cannot resolve value for "' + property + ': ' + fn + '", given: ' + value);
}
}
}
return value;
};
}
Expand Down Expand Up @@ -92,10 +100,10 @@ var Container = (function () {
var keys = Object.keys(schema);
var length = keys.length;
while (length--) {
var fn = keys[length];
var fn = keys[length].replace(/^x-/, '');
var gen = this.support[fn];
if (typeof gen === 'function') {
schema.generate = function () { return gen(schema[fn], schema); };
schema.generate = function () { return gen(schema[keys[length]], schema, keys[length]); };
break;
}
}
Expand Down Expand Up @@ -307,17 +315,28 @@ function merge(a, b) {
}
return a;
}
function clean(obj, isArray) {
function clean(obj, isArray, requiredProps) {
if (!obj || typeof obj !== 'object') {
return obj;
}
if (Array.isArray(obj)) {
return obj
obj = obj
.map(function (value) { return clean(value, true); })
.filter(function (value) { return value; });
.filter(function (value) { return typeof value !== 'undefined'; });
if (isArray && !obj.length) {
return undefined;
}
return obj;
}
Object.keys(obj).forEach(function (k) {
obj[k] = clean(obj[k]);
if (!requiredProps || requiredProps.indexOf(k) === -1) {
if (Array.isArray(obj[k]) && !obj[k].length) {
delete obj[k];
}
}
else {
obj[k] = clean(obj[k]);
}
});
if (!Object.keys(obj).length && isArray) {
return undefined;
Expand Down Expand Up @@ -879,6 +898,9 @@ var typeMap = {
// TODO provide types
function traverse(schema, path, resolve) {
schema = resolve(schema);
if (!schema) {
return;
}
if (Array.isArray(schema.enum)) {
return random.pick(schema.enum);
}
Expand Down Expand Up @@ -912,7 +934,7 @@ function traverse(schema, path, resolve) {
}
else {
try {
return typeMap[type](schema, path, resolve, traverse);
return utils.clean(typeMap[type](schema, path, resolve, traverse), null, schema.required);
}
catch (e) {
if (typeof e.path === 'undefined') {
Expand All @@ -934,7 +956,7 @@ function traverse(schema, path, resolve) {
copy[prop] = schema[prop];
}
}
return utils.clean(copy);
return copy;
}

function isKey(prop) {
Expand All @@ -951,18 +973,27 @@ function run(schema, container) {
return null;
}
// cleanup
delete sub.id;
delete sub.$schema;
if (typeof sub.$ref === 'string' && sub.$ref.indexOf('#/') === -1) {
throw new Error('Only local references are allowed in sync mode.');
if (sub.id && typeof sub.id === 'string') {
delete sub.id;
delete sub.$schema;
}
if (typeof sub.$ref === 'string') {
if (sub.$ref.indexOf('#/') === -1) {
throw new Error('Only local references are allowed in sync mode.');
}
return null;
}
if (Array.isArray(sub.allOf)) {
var schemas = sub.allOf;
delete sub.allOf;
// this is the only case where all sub-schemas
// must be resolved before any merge
schemas.forEach(function (subSchema) {
utils.merge(sub, reduce(subSchema, maxReduceDepth + 1));
var _sub = reduce(subSchema, maxReduceDepth + 1);
// call given thunks if present
utils.merge(sub, typeof _sub.thunk === 'function'
? _sub.thunk()
: _sub);
});
}
if (Array.isArray(sub.oneOf || sub.anyOf)) {
Expand Down
2 changes: 1 addition & 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-rc3",
"version": "0.5.0-rc4",
"description": "JSON-Schema + fake data generators",
"homepage": "http://json-schema-faker.js.org",
"main": "lib/index.js",
Expand Down

0 comments on commit 7857285

Please sign in to comment.