Comparing version 3.0.1 to 3.0.2
{ | ||
"name": "rttc", | ||
"version": "3.0.1", | ||
"version": "3.0.2", | ||
"description": "Runtime type-checking for JavaScript.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -65,17 +65,44 @@ # rttc | ||
## Types | ||
#### Strings | ||
#### Edge cases | ||
`example: 'stuff'` | ||
+ `undefined` is never valid. | ||
+ `null` is only valid against `example: '*'`. | ||
+ `NaN` is only valid against `example: '*'`. | ||
+ `Infinity` is only valid against `example: '*'`. | ||
+ `-Infinity` is only valid against `example: '*'`. | ||
#### Numbers | ||
#### Dictionaries | ||
`example: 323` | ||
+ Dictionaries (i.e. plain old JavaScript objects like `{}`) in type schemas can be infinitely nested. Type validation and coercion will proceed through the nested objects recursively. | ||
#### Booleans | ||
`example: {}` | ||
#### Generic dictionaries | ||
`example: {}` | ||
The **generic dictionary** type is a dictionary type schema with no keys. | ||
Dictionaries that have been validated/coerced against the generic dictionary type: | ||
+ will have no prototypal properties, getters, or setters, as well as a complete deficit of any other sort of deceit, lies, or magic | ||
+ are guaranteed to be JSON-serializable, with a few additional affordances: | ||
+ normally, stringified JSON may contain `null` values. Instead, rttc removes `null` items from arrays and removes keys with `null` values from objects. | ||
+ normally, `Error` instances get stringified into empty objects. Instead, rttc turns them into human-readable strings by reducing them to their `.stack` property (this includes the error message and the stack trace w/ line numbers) | ||
+ normally, `RegExp` instances get stringified into empty objects. Instead, rttc turns them into human-readable strings like `'/some regexp/gi'` | ||
+ normally, `function()` instances get stringified into empty objects. Instead, rttc turns them into human-readable strings like `'function doStuff (a,b) { console.log(\'wow I can actually read this!\'); }'` | ||
#### Faceted dictionaries | ||
`example: {...}` | ||
The **faceted dictionary** type is any dictionary type schema with at least one key. | ||
Extra keys in the actual value that are not in the type schema will be stripped out. | ||
Dictionary type schemas (i.e. plain old JavaScript objects nested like `{a:{}}`) can be infinitely nested. Type validation and coercion will proceed through the nested objects recursively. | ||
```js | ||
@@ -97,9 +124,30 @@ { | ||
When validating against a dictionary schema with at least one key, extra keys in the actual value will be stripped out. If the dictionary schema is empty, all actual keys will be left alone. | ||
#### Arrays | ||
#### Generic arrays | ||
+ Arrays in type schemas must be homogeneous and have exactly one item; that is, if you want to validate an array, you only need to provide the type/schema for the first item in the array, e.g.: | ||
`example: []` | ||
Arrays that have been validated/coerced against the generic array type: | ||
+ _may_ be heterogeneous (have items with different types) - but it is generally best practice to avoid heterogeneous arrays in general. | ||
+ are guaranteed to be JSON-serializable, with a few additional affordances: | ||
+ normally, stringified JSON may contain `null` values. Instead, rttc removes `null` items from arrays and removes keys with `null` values from objects. | ||
+ normally, `Error` instances get stringified into empty objects. Instead, rttc turns them into human-readable strings by reducing them to their `.stack` property (this includes the error message and the stack trace w/ line numbers) | ||
+ normally, `RegExp` instances get stringified into empty objects. Instead, rttc turns them into human-readable strings like `'/some regexp/gi'` | ||
+ normally, `function()` instances get stringified into empty objects. Instead, rttc turns them into human-readable strings like `'function doStuff (a,b) { console.log(\'wow I can actually read this!\'); }'` | ||
#### Homogeneous arrays | ||
`example: ['Margaret']` | ||
`example: [123]` | ||
`example: [true]` | ||
`example: [[...]]` | ||
`example: [{...}]` | ||
Array type schemas may be infinitely nested and combined with dictionaries or any other types. | ||
Runtime arrays being validated/coerced against array type schemas will be homogeneous (meaning every item in the array will have the same type). | ||
> Also note that, because of this, when providing a type schema or type-inference-able example for an array, you only need to provide one item in the array, e.g.: | ||
```js | ||
@@ -124,6 +172,24 @@ [ | ||
#### Wildcards | ||
`example: '*'` | ||
## Usage | ||
This special type allows anything except `undefined`. It also _does not rebuild objects_, which means it maintains the original reference (i.e. is `===`). It also does not guarantee JSON-serializability. | ||
#### Edge cases | ||
+ `undefined` is _never_ valid as a _top-level value_, but it is allowed as an item or value in a nested array or dictionary validated/coerced against `example: *`. | ||
+ `null` is only valid against `example: '*'`. | ||
+ `NaN` is only valid against `example: '*'`. | ||
+ `Infinity` is only valid against `example: '*'`. | ||
+ `-Infinity` is only valid against `example: '*'`. | ||
+ `-0` is understood as 0 | ||
+ `+0` is understood as 0 | ||
## Examples | ||
#### rttc.infer(value) | ||
@@ -130,0 +196,0 @@ |
@@ -14,2 +14,4 @@ // Export the array of tests below. | ||
{ example: 'foo', actual: -1.1, result: '-1.1' }, | ||
{ example: 'foo', actual: -0, result: '0' }, | ||
{ example: 'foo', actual: +0, result: '0' }, | ||
@@ -64,2 +66,4 @@ { example: 'foo', actual: true, result: 'true' }, | ||
{ example: 123, actual: -1.1, result: -1.1 }, | ||
{ example: 123, actual: +0, result: 0 }, | ||
{ example: 123, actual: -0, result: 0 }, | ||
@@ -115,2 +119,4 @@ { example: 123, actual: true, result: 1 }, | ||
{ example: true, actual: -1.1, result: false }, | ||
{ example: true, actual: +0, result: false }, | ||
{ example: true, actual: -0, result: false }, | ||
@@ -153,2 +159,4 @@ { example: true, actual: true, result: true }, | ||
{ example: {}, actual: 123, result: {} }, | ||
{ example: {}, actual: -0, result: {} }, | ||
{ example: {}, actual: +0, result: {} }, | ||
{ example: {}, actual: true, result: {} }, | ||
@@ -198,2 +206,4 @@ | ||
{ example: [], actual: 123, result: [] }, | ||
{ example: [], actual: -0, result: [] }, | ||
{ example: [], actual: +0, result: [] }, | ||
{ example: [], actual: true, result: [] }, | ||
@@ -200,0 +210,0 @@ |
@@ -15,2 +15,4 @@ // Export the array of tests below | ||
{ example: 'foo', actual: -1.1, result: '-1.1' }, | ||
{ example: 'foo', actual: -0, result: '0' }, | ||
{ example: 'foo', actual: +0, result: '0' }, | ||
@@ -66,2 +68,4 @@ { example: 'foo', actual: true, result: 'true' }, | ||
{ example: 123, actual: -1.1, result: -1.1 }, | ||
{ example: 123, actual: -0, result: 0 }, | ||
{ example: 123, actual: +0, result: 0 }, | ||
@@ -118,2 +122,4 @@ { example: 123, actual: true, result: 1 }, | ||
{ example: true, actual: -1.1, error: true }, | ||
{ example: true, actual: -0, result: false }, | ||
{ example: true, actual: +0, result: false }, | ||
@@ -120,0 +126,0 @@ { example: true, actual: true, result: true }, |
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
101813
2103
363