@humanwhocodes/object-schema
Advanced tools
Comparing version 1.1.0 to 1.1.1
{ | ||
"name": "@humanwhocodes/object-schema", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "An object schema merger/validator", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -115,8 +115,12 @@ /** | ||
return objects.reduce((result, object) => { | ||
this.validate(object); | ||
for (const [key, strategy] of this[strategies]) { | ||
try { | ||
const value = strategy.merge.call(this, result[key], object[key]); | ||
if (value !== undefined) { | ||
result[key] = value; | ||
if (key in result || key in object) { | ||
const value = strategy.merge.call(this, result[key], object[key]); | ||
if (value !== undefined) { | ||
result[key] = value; | ||
} | ||
} | ||
@@ -160,3 +164,3 @@ } catch (ex) { | ||
try { | ||
strategy.validate.call(strategy, object); | ||
strategy.validate.call(strategy, object[key]); | ||
} catch (ex) { | ||
@@ -163,0 +167,0 @@ ex.message = `Key "${key}": ` + ex.message; |
@@ -105,2 +105,19 @@ /** | ||
it("should not call the merge() strategy when both objects don't contain the key", () => { | ||
let called = false; | ||
schema = new ObjectSchema({ | ||
foo: { | ||
merge() { | ||
called = true; | ||
}, | ||
validate() {} | ||
} | ||
}); | ||
schema.merge({}, {}); | ||
assert.isFalse(called, "The merge() strategy should not have been called."); | ||
}); | ||
it("should omit returning the key when the merge() strategy returns undefined", () => { | ||
@@ -190,2 +207,17 @@ schema = new ObjectSchema({ | ||
it("should pass the property value into validate() when key is found", () => { | ||
schema = new ObjectSchema({ | ||
foo: { | ||
merge() { | ||
return "bar"; | ||
}, | ||
validate(value) { | ||
assert.isTrue(value); | ||
} | ||
} | ||
}); | ||
schema.validate({ foo: true }); | ||
}); | ||
it("should not throw an error when expected keys are found", () => { | ||
@@ -192,0 +224,0 @@ schema = new ObjectSchema({ |
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
24404
513