Comparing version 0.10.0 to 0.10.2
@@ -0,1 +1,21 @@ | ||
<a name="0.10.2"></a> | ||
## [0.10.2](https://github.com/boosterfuels/archetype/compare/v0.10.0...v0.10.2) (2020-02-25) | ||
### Bug Fixes | ||
* clone empty arrays / objects if set as `$default`, throw if not an empty object ([312aa14](https://github.com/boosterfuels/archetype/commit/312aa14)) | ||
<a name="0.10.1"></a> | ||
## [0.10.1](https://github.com/boosterfuels/archetype/compare/v0.10.0...v0.10.1) (2020-02-25) | ||
### Bug Fixes | ||
* clone empty arrays / objects if set as `$default`, throw if not an empty object ([312aa14](https://github.com/boosterfuels/archetype/commit/312aa14)) | ||
<a name="0.10.0"></a> | ||
@@ -2,0 +22,0 @@ # [0.10.0](https://github.com/boosterfuels/archetype/compare/v0.9.1...v0.10.0) (2019-08-09) |
{ | ||
"name": "archetype", | ||
"version": "0.10.0", | ||
"version": "0.10.2", | ||
"author": "Valeri Karpov <val@boosterfuels.com>", | ||
@@ -5,0 +5,0 @@ "dependencies": { |
@@ -10,2 +10,3 @@ 'use strict'; | ||
const shouldSkipPath = require('./util').shouldSkipPath; | ||
const util = require('util'); | ||
@@ -35,3 +36,3 @@ function applyDefaults(obj, schema, projection) { | ||
if ('$default' in schemaPath) { | ||
return handleDefault(schemaPath.$default, root); | ||
return handleDefault(schemaPath.$default, root, path); | ||
} | ||
@@ -70,7 +71,20 @@ | ||
function handleDefault(obj, ctx) { | ||
function handleDefault(obj, ctx, path) { | ||
if (typeof obj === 'function') { | ||
return obj(ctx); | ||
} | ||
// If default is an object type, be very careful - | ||
// returning an object default would be by reference, | ||
// which means user data could modify the default. | ||
if (typeof obj === 'object' && obj != null) { | ||
if (Array.isArray(obj) && obj.length === 0) { | ||
return []; | ||
} else if (!Array.isArray(obj) && Object.keys(obj).length === 0) { | ||
return {}; | ||
} | ||
throw new Error('Default at path `' + path + '` is a non-empty ' + | ||
'object `' + util.inspect(obj) + '`. Please make `$default` ' + | ||
'a function that returns an object instead'); | ||
} | ||
return obj; | ||
} |
'use strict'; | ||
const util = require('util'); | ||
class Path { | ||
constructor(obj) { | ||
if (obj != null && obj.$default != null && typeof obj.$default === 'object') { | ||
const $default = obj.$default; | ||
const numKeys = Array.isArray($default) ? | ||
$default.length : | ||
Object.keys($default).length; | ||
if (numKeys > 0) { | ||
throw new Error('Default is a non-empty object `' + | ||
util.inspect($default) + '`. Please make `$default` a function ' + | ||
'that returns an object instead'); | ||
} | ||
} | ||
Object.assign(this, obj); | ||
@@ -6,0 +19,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42999
800