Comparing version 0.5.1 to 0.6.0
@@ -36,6 +36,6 @@ /** | ||
/** | ||
* Attempts to cast a value to the type for this runtype and return it. | ||
* Throws an exception if validation fails. | ||
* Verifies that a value conforms to this runtype. If so, returns the same value, | ||
* statically typed. Otherwise throws an exception. | ||
*/ | ||
coerce(x: any): A; | ||
check(x: any): A; | ||
/** | ||
@@ -42,0 +42,0 @@ * Validates that a value conforms to this type, and returns a result indicating |
@@ -85,3 +85,3 @@ "use strict"; | ||
var x = xs_1[_i]; | ||
v.coerce(x); | ||
v.check(x); | ||
} | ||
@@ -98,7 +98,7 @@ return xs; | ||
return runtype(function (x) { | ||
var xs = arr(exports.Always).coerce(x); | ||
var xs = arr(exports.Always).check(x); | ||
if (xs.length < runtypes.length) | ||
throw new ValidationError("Expected array of " + runtypes.length + " but was " + xs.length); | ||
for (var i = 0; i < runtypes.length; i++) | ||
runtypes[i].coerce(xs[i]); | ||
runtypes[i].check(xs[i]); | ||
return x; | ||
@@ -118,3 +118,3 @@ }); | ||
if (hasKey(key, x)) | ||
runtypes[key].coerce(x[key]); | ||
runtypes[key].check(x[key]); | ||
else | ||
@@ -137,3 +137,3 @@ throw new ValidationError("Missing property " + key); | ||
if (hasKey(key, x)) | ||
Union(runtypes[key], exports.Undefined).coerce(x[key]); | ||
Union(runtypes[key], exports.Undefined).check(x[key]); | ||
return x; | ||
@@ -165,4 +165,4 @@ }); | ||
for (var _i = 0, runtypes_2 = runtypes; _i < runtypes_2.length; _i++) { | ||
var coerce = runtypes_2[_i].coerce; | ||
coerce(x); | ||
var check = runtypes_2[_i].check; | ||
check(x); | ||
} | ||
@@ -181,9 +181,9 @@ return x; | ||
cached = fn(); | ||
return cached.coerce(x); | ||
return cached.check(x); | ||
}); | ||
} | ||
exports.Lazy = Lazy; | ||
function runtype(coerce) { | ||
function runtype(check) { | ||
var A = { | ||
coerce: coerce, | ||
check: check, | ||
validate: validate, | ||
@@ -198,3 +198,3 @@ guard: guard, | ||
try { | ||
coerce(value); | ||
check(value); | ||
return { success: true, value: value }; | ||
@@ -201,0 +201,0 @@ } |
{ | ||
"name": "runtypes", | ||
"version": "0.5.1", | ||
"version": "0.6.0", | ||
"description": "Runtime validation for static types", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -112,6 +112,6 @@ # Runtypes [![Build Status](https://travis-ci.org/pelotom/runtypes.svg?branch=master)](https://travis-ci.org/pelotom/runtypes) | ||
// spaceObject: SpaceObject | ||
const spaceObject = SpaceObject.coerce(obj) | ||
const spaceObject = SpaceObject.check(obj) | ||
``` | ||
If the object doesn't conform to the type specification, `coerce` will throw an exception. | ||
If the object doesn't conform to the type specification, `check` will throw an exception. | ||
@@ -153,3 +153,3 @@ ## Static type inference | ||
In addition to providing a coercion method, runtypes can be used as [type guards](https://basarat.gitbooks.io/typescript/content/docs/types/typeGuard.html): | ||
In addition to providing a `check` method, runtypes can be used as [type guards](https://basarat.gitbooks.io/typescript/content/docs/types/typeGuard.html): | ||
@@ -156,0 +156,0 @@ ```ts |
@@ -42,6 +42,6 @@ | ||
/** | ||
* Attempts to cast a value to the type for this runtype and return it. | ||
* Throws an exception if validation fails. | ||
* Verifies that a value conforms to this runtype. If so, returns the same value, | ||
* statically typed. Otherwise throws an exception. | ||
*/ | ||
coerce(x: any): A | ||
check(x: any): A | ||
@@ -162,3 +162,3 @@ /** | ||
for (const x of xs) | ||
v.coerce(x) | ||
v.check(x) | ||
return xs | ||
@@ -216,7 +216,7 @@ }) | ||
return runtype(x => { | ||
const xs = arr(Always).coerce(x) | ||
const xs = arr(Always).check(x) | ||
if (xs.length < runtypes.length) | ||
throw new ValidationError(`Expected array of ${runtypes.length} but was ${xs.length}`) | ||
for (let i = 0; i < runtypes.length; i++) | ||
runtypes[i].coerce(xs[i]) | ||
runtypes[i].check(xs[i]) | ||
return x | ||
@@ -237,3 +237,3 @@ }) | ||
if (hasKey(key, x)) | ||
runtypes[key].coerce(x[key]) | ||
runtypes[key].check(x[key]) | ||
else | ||
@@ -258,3 +258,3 @@ throw new ValidationError(`Missing property ${key}`) | ||
if (hasKey(key, x)) | ||
Union(runtypes[key], Undefined).coerce(x[key]) | ||
Union(runtypes[key], Undefined).check(x[key]) | ||
@@ -1065,4 +1065,4 @@ return x as Partial<O> | ||
return runtype(x => { | ||
for (const { coerce } of runtypes) | ||
coerce(x) | ||
for (const { check } of runtypes) | ||
check(x) | ||
return x | ||
@@ -1080,10 +1080,10 @@ }) | ||
cached = fn() | ||
return cached.coerce(x) | ||
return cached.check(x) | ||
}) | ||
} | ||
function runtype<A>(coerce: (x: {}) => A): Runtype<A> { | ||
function runtype<A>(check: (x: {}) => A): Runtype<A> { | ||
const A = { | ||
coerce, | ||
check, | ||
validate, | ||
@@ -1100,3 +1100,3 @@ guard, | ||
try { | ||
coerce(value) | ||
check(value) | ||
return { success: true, value } | ||
@@ -1103,0 +1103,0 @@ } catch ({ message }) { |
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
64095