Comparing version 4.2.1 to 4.3.0
@@ -0,1 +1,16 @@ | ||
### 4.3.0 - 2020-02-20 | ||
#### 🚀 Updates | ||
- Improved error messages by including the currently invalid value. | ||
#### 🐞 Fixes | ||
- Fixed an issue where arrays and tuples were used at the same time in a union. | ||
#### 🛠 Internals | ||
- Updated union error messages to be more readable. | ||
- Updated tuple type aliases to use "tuple<value>". | ||
### 4.2.1 - 2020-02-11 | ||
@@ -2,0 +17,0 @@ |
@@ -48,2 +48,23 @@ function isObject(value) { | ||
function typeOf(value) { | ||
if (Array.isArray(value)) { | ||
return 'array'; | ||
} | ||
if (isObject(value)) { | ||
return value.constructor.name === 'Object' ? 'object' : 'instance'; | ||
} | ||
switch (typeof value) { | ||
case 'boolean': | ||
case 'function': | ||
case 'number': | ||
case 'string': | ||
return typeof value; | ||
default: | ||
return 'unknown'; | ||
} | ||
} | ||
var Predicate = function () { | ||
@@ -350,6 +371,8 @@ function Predicate(type, defaultValue, bypassFactory) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
var displayValue = this.extractDisplayValue(value); | ||
switch (this.type) { | ||
case 'array': | ||
case 'tuple': | ||
this.invariant(Array.isArray(value), 'Must be an array.', path); | ||
this.invariant(Array.isArray(value), "Must be an array, received " + displayValue + ".", path); | ||
break; | ||
@@ -364,7 +387,7 @@ | ||
case 'shape': | ||
this.invariant(isObject(value), 'Must be a plain object.', path); | ||
this.invariant(isObject(value), "Must be a plain object, received " + displayValue + ".", path); | ||
break; | ||
default: | ||
this.invariant(typeof value === this.type, "Must be a " + this.type + ".", path); | ||
this.invariant(typeof value === this.type, "Must be a " + this.type + ", received " + displayValue + ".", path); | ||
break; | ||
@@ -375,2 +398,17 @@ } | ||
_proto.extractDisplayValue = function extractDisplayValue(value) { | ||
var type = typeOf(value); | ||
var displayValue = ''; | ||
if (type === 'string') { | ||
displayValue = "\"" + value + "\""; | ||
} else if (type === 'number' || type === 'boolean' || value === null || value === undefined) { | ||
displayValue = String(value); | ||
} else { | ||
displayValue = type; | ||
} | ||
return displayValue; | ||
}; | ||
_proto.isOptionalDefault = function isOptionalDefault(value) { | ||
@@ -402,25 +440,2 @@ return !this.isRequired && value === this.default(); | ||
function typeOf(value) { | ||
if (Array.isArray(value)) { | ||
return value.every(function (item) { | ||
return typeof item === typeof value[0]; | ||
}) ? 'array' : 'union'; | ||
} | ||
if (isObject(value)) { | ||
return value.constructor.name === 'Object' ? 'object' : 'instance'; | ||
} | ||
switch (typeof value) { | ||
case 'boolean': | ||
case 'function': | ||
case 'number': | ||
case 'string': | ||
return typeof value; | ||
default: | ||
return 'unknown'; | ||
} | ||
} | ||
var Schema = function () { | ||
@@ -1272,5 +1287,5 @@ function Schema(blueprint) { | ||
_proto.typeAlias = function typeAlias() { | ||
return "[" + this.contents.map(function (item) { | ||
return "tuple<" + this.contents.map(function (item) { | ||
return item.typeAlias(); | ||
}).join(', ') + "]"; | ||
}).join(', ') + ">"; | ||
}; | ||
@@ -1352,6 +1367,4 @@ | ||
} | ||
return false; | ||
} catch (error) { | ||
errors.add(" - " + error.message + "\n"); | ||
errors.add("\n - " + error.message); | ||
} | ||
@@ -1361,6 +1374,6 @@ | ||
}); | ||
var message = "Type must be one of: " + keys + "."; | ||
var displayValue = this.extractDisplayValue(value); | ||
var message = contents.length === 1 ? "Received " + displayValue + " but must be: " + keys : "Received " + displayValue + " but must be a union of: " + keys; | ||
if (!passed && errors.size > 0) { | ||
message += " Received " + type + " with the following invalidations:\n"; | ||
errors.forEach(function (error) { | ||
@@ -1367,0 +1380,0 @@ message += error; |
@@ -52,2 +52,23 @@ 'use strict'; | ||
function typeOf(value) { | ||
if (Array.isArray(value)) { | ||
return 'array'; | ||
} | ||
if (isObject(value)) { | ||
return value.constructor.name === 'Object' ? 'object' : 'instance'; | ||
} | ||
switch (typeof value) { | ||
case 'boolean': | ||
case 'function': | ||
case 'number': | ||
case 'string': | ||
return typeof value; | ||
default: | ||
return 'unknown'; | ||
} | ||
} | ||
var Predicate = function () { | ||
@@ -354,6 +375,8 @@ function Predicate(type, defaultValue, bypassFactory) { | ||
if ("production" !== process.env.NODE_ENV) { | ||
var displayValue = this.extractDisplayValue(value); | ||
switch (this.type) { | ||
case 'array': | ||
case 'tuple': | ||
this.invariant(Array.isArray(value), 'Must be an array.', path); | ||
this.invariant(Array.isArray(value), "Must be an array, received " + displayValue + ".", path); | ||
break; | ||
@@ -368,7 +391,7 @@ | ||
case 'shape': | ||
this.invariant(isObject(value), 'Must be a plain object.', path); | ||
this.invariant(isObject(value), "Must be a plain object, received " + displayValue + ".", path); | ||
break; | ||
default: | ||
this.invariant(typeof value === this.type, "Must be a " + this.type + ".", path); | ||
this.invariant(typeof value === this.type, "Must be a " + this.type + ", received " + displayValue + ".", path); | ||
break; | ||
@@ -379,2 +402,17 @@ } | ||
_proto.extractDisplayValue = function extractDisplayValue(value) { | ||
var type = typeOf(value); | ||
var displayValue = ''; | ||
if (type === 'string') { | ||
displayValue = "\"" + value + "\""; | ||
} else if (type === 'number' || type === 'boolean' || value === null || value === undefined) { | ||
displayValue = String(value); | ||
} else { | ||
displayValue = type; | ||
} | ||
return displayValue; | ||
}; | ||
_proto.isOptionalDefault = function isOptionalDefault(value) { | ||
@@ -406,25 +444,2 @@ return !this.isRequired && value === this.default(); | ||
function typeOf(value) { | ||
if (Array.isArray(value)) { | ||
return value.every(function (item) { | ||
return typeof item === typeof value[0]; | ||
}) ? 'array' : 'union'; | ||
} | ||
if (isObject(value)) { | ||
return value.constructor.name === 'Object' ? 'object' : 'instance'; | ||
} | ||
switch (typeof value) { | ||
case 'boolean': | ||
case 'function': | ||
case 'number': | ||
case 'string': | ||
return typeof value; | ||
default: | ||
return 'unknown'; | ||
} | ||
} | ||
var Schema = function () { | ||
@@ -1276,5 +1291,5 @@ function Schema(blueprint) { | ||
_proto.typeAlias = function typeAlias() { | ||
return "[" + this.contents.map(function (item) { | ||
return "tuple<" + this.contents.map(function (item) { | ||
return item.typeAlias(); | ||
}).join(', ') + "]"; | ||
}).join(', ') + ">"; | ||
}; | ||
@@ -1356,6 +1371,4 @@ | ||
} | ||
return false; | ||
} catch (error) { | ||
errors.add(" - " + error.message + "\n"); | ||
errors.add("\n - " + error.message); | ||
} | ||
@@ -1365,6 +1378,6 @@ | ||
}); | ||
var message = "Type must be one of: " + keys + "."; | ||
var displayValue = this.extractDisplayValue(value); | ||
var message = contents.length === 1 ? "Received " + displayValue + " but must be: " + keys : "Received " + displayValue + " but must be a union of: " + keys; | ||
if (!passed && errors.size > 0) { | ||
message += " Received " + type + " with the following invalidations:\n"; | ||
errors.forEach(function (error) { | ||
@@ -1371,0 +1384,0 @@ message += error; |
@@ -98,2 +98,3 @@ import Schema from './Schema'; | ||
protected checkType(path: string, value: T): void; | ||
protected extractDisplayValue(value: unknown): string; | ||
/** | ||
@@ -100,0 +101,0 @@ * Return true if the value matches the default value and the predicate is optional. |
{ | ||
"name": "optimal", | ||
"version": "4.2.1", | ||
"version": "4.3.0", | ||
"description": "A system for building and validating defined object structures.", | ||
@@ -51,6 +51,6 @@ "main": "./lib/index.js", | ||
"@milesj/build-tools": "^2.17.1", | ||
"@types/node": "^13.9.0", | ||
"conventional-changelog-beemo": "^1.6.0", | ||
"rollup": "^2.0.0", | ||
"rollup-plugin-babel": "^4.3.3", | ||
"@types/node": "^14.14.31", | ||
"conventional-changelog-beemo": "^2.1.0", | ||
"rollup": "^2.39.0", | ||
"rollup-plugin-babel": "^4.4.0", | ||
"rollup-plugin-node-resolve": "^5.2.0" | ||
@@ -57,0 +57,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
104506
2600