Comparing version 0.9.3 to 0.9.4
@@ -16,2 +16,7 @@ # Changelog | ||
# 0.9.4 | ||
* **Bug Fix** | ||
* strict: should succeed validating an undefined field, closes #106 (@gcanti) | ||
# 0.9.3 | ||
@@ -18,0 +23,0 @@ |
@@ -672,19 +672,14 @@ "use strict"; | ||
var loose = exports.type(props); | ||
var len = Object.keys(props).length; | ||
return new StrictType(name, function (v) { return loose.is(v) && Object.getOwnPropertyNames(v).every(function (k) { return props.hasOwnProperty(k); }); }, function (s, c) { | ||
return loose.validate(s, c).chain(function (o) { | ||
var keys = Object.getOwnPropertyNames(o); | ||
if (keys.length !== len) { | ||
var errors = []; | ||
for (var i = 0; i < keys.length; i++) { | ||
var key = keys[i]; | ||
if (!props.hasOwnProperty(key)) { | ||
errors.push(exports.getValidationError(o[key], exports.appendContext(c, key, exports.never))); | ||
} | ||
var len = keys.length; | ||
var errors = []; | ||
for (var i = 0; i < len; i++) { | ||
var key = keys[i]; | ||
if (!props.hasOwnProperty(key)) { | ||
errors.push(exports.getValidationError(o[key], exports.appendContext(c, key, exports.never))); | ||
} | ||
return errors.length ? exports.failures(errors) : exports.failure(o, c); | ||
} | ||
else { | ||
return exports.success(o); | ||
} | ||
return errors.length ? exports.failures(errors) : exports.success(o); | ||
}); | ||
@@ -691,0 +686,0 @@ }, loose.serialize, props); |
{ | ||
"name": "io-ts", | ||
"version": "0.9.3", | ||
"version": "0.9.4", | ||
"description": "TypeScript compatible runtime type system for IO validation", | ||
@@ -49,3 +49,3 @@ "files": ["lib"], | ||
"tslint-config-standard": "4.0.0", | ||
"typescript": "2.6.1", | ||
"typescript": "2.6.2", | ||
"typings-checker": "1.1.2" | ||
@@ -52,0 +52,0 @@ }, |
@@ -106,2 +106,4 @@ # The idea | ||
* [io-ts-types](https://github.com/gcanti/io-ts-types) - A collection of runtime types and combinators for use with | ||
io-ts | ||
* [io-ts-reporters](https://github.com/OliverJAsh/io-ts-reporters) - Error reporters for io-ts | ||
@@ -244,2 +246,19 @@ * [geojson-iots](https://github.com/pierremarc/geojson-iots) - Runtime types for GeoJSON as defined in rfc7946 made with | ||
You can define a custom combinator to avoid the boilerplate | ||
```ts | ||
export function interfaceWithOptionals<R extends t.Props, O extends t.Props>( | ||
required: R, | ||
optional: O, | ||
name?: string | ||
): t.IntersectionType< | ||
[t.InterfaceType<R, t.InterfaceOf<R>>, t.PartialType<O, t.PartialOf<O>>], | ||
t.InterfaceOf<R> & t.PartialOf<O> | ||
> { | ||
return t.intersection([t.interface(required), t.partial(optional)], name) | ||
} | ||
const C = interfaceWithOptionals({ foo: t.string }, { bar: t.number }) | ||
``` | ||
# Custom types | ||
@@ -289,2 +308,33 @@ | ||
## The `pluck` combinator | ||
Extracting the runtime type of a field contained in each member of a union | ||
```ts | ||
const pluck = <F extends string, U extends t.UnionType<Array<t.InterfaceType<{ [K in F]: t.Any }, any>>, any>>( | ||
union: U, | ||
field: F | ||
): t.Type<any, t.TypeOf<U>[F]> => { | ||
return t.union(union.types.map(type => type.props[field])) | ||
} | ||
export const Action = t.union([ | ||
t.interface({ | ||
type: t.literal('Action1'), | ||
payload: t.interface({ | ||
foo: t.string | ||
}) | ||
}), | ||
t.interface({ | ||
type: t.literal('Action2'), | ||
payload: t.interface({ | ||
bar: t.string | ||
}) | ||
}) | ||
]) | ||
// ActionType: t.Type<any, "Action1" | "Action2"> | ||
const ActionType = pluck(Action, 'type') | ||
``` | ||
# Recipes | ||
@@ -304,5 +354,5 @@ | ||
if (NODE_ENV !== 'production') { | ||
return t.validate(value, type).fold(errors => { | ||
return t.validate(value, type).getOrElse(errors => { | ||
throw new Error(failure(errors).join('\n')) | ||
}, t.identity) | ||
}) | ||
} | ||
@@ -309,0 +359,0 @@ // unsafe cast |
Sorry, the diff of this file is not supported yet
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
102417
391
965