Comparing version 1.2.1 to 1.3.0
@@ -16,2 +16,7 @@ # Changelog | ||
# 1.3.0 | ||
- **New Feature** | ||
- add `TaggedUnionType` (@gcanti) | ||
# 1.2.1 | ||
@@ -18,0 +23,0 @@ |
@@ -283,3 +283,7 @@ import { Either } from 'fp-ts/lib/Either'; | ||
export declare const getTagValue: <Tag extends string>(tag: Tag) => (type: Tagged<Tag, any, any>) => string | number | boolean; | ||
export declare const taggedUnion: <Tag extends string, RTS extends Tagged<Tag, any, any>[]>(tag: Tag, types: RTS, name?: string) => UnionType<RTS, RTS["_A"]["_A"], RTS["_A"]["_O"], mixed>; | ||
export declare class TaggedUnionType<Tag extends string, RTS extends Array<Tagged<Tag>>, A = any, O = A, I = mixed> extends UnionType<RTS, A, O, I> { | ||
readonly tag: Tag; | ||
constructor(name: string, is: TaggedUnionType<Tag, RTS, A, O, I>['is'], validate: TaggedUnionType<Tag, RTS, A, O, I>['validate'], serialize: TaggedUnionType<Tag, RTS, A, O, I>['encode'], types: RTS, tag: Tag); | ||
} | ||
export declare const taggedUnion: <Tag extends string, RTS extends Tagged<Tag, any, any>[]>(tag: Tag, types: RTS, name?: string) => TaggedUnionType<Tag, RTS, RTS["_A"]["_A"], RTS["_A"]["_O"], mixed>; | ||
export declare class ExactType<RT extends Any, A = any, O = A, I = mixed> extends Type<A, O, I> { | ||
@@ -286,0 +290,0 @@ readonly type: RT; |
"use strict"; | ||
var __extends = (this && this.__extends) || (function () { | ||
var extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
var extendStatics = function (d, b) { | ||
extendStatics = Object.setPrototypeOf || | ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || | ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; | ||
return extendStatics(d, b); | ||
} | ||
return function (d, b) { | ||
@@ -12,9 +15,12 @@ extendStatics(d, b); | ||
})(); | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
@@ -840,2 +846,13 @@ Object.defineProperty(exports, "__esModule", { value: true }); | ||
}; | ||
var TaggedUnionType = /** @class */ (function (_super) { | ||
__extends(TaggedUnionType, _super); | ||
function TaggedUnionType(name, is, validate, serialize, types, tag) { | ||
var _this = _super.call(this, name, is, validate, serialize, types) /* istanbul ignore next */ // <= workaround for https://github.com/Microsoft/TypeScript/issues/13455 | ||
|| this; | ||
_this.tag = tag; | ||
return _this; | ||
} | ||
return TaggedUnionType; | ||
}(UnionType)); | ||
exports.TaggedUnionType = TaggedUnionType; | ||
exports.taggedUnion = function (tag, types, name) { | ||
@@ -869,3 +886,3 @@ if (name === void 0) { name = "(" + types.map(function (type) { return type.name; }).join(' | ') + ")"; } | ||
var TagValue = new Type(values.map(function (l) { return JSON.stringify(l); }).join(' | '), isTagValue, function (m, c) { return (isTagValue(m) ? exports.success(m) : exports.failure(m, c)); }, exports.identity); | ||
return new UnionType(name, function (v) { | ||
return new TaggedUnionType(name, function (v) { | ||
if (!exports.Dictionary.is(v)) { | ||
@@ -893,3 +910,3 @@ return false; | ||
} | ||
}, useIdentity(types, len) ? exports.identity : function (a) { return types[getIndex(a[tag])].encode(a); }, types); | ||
}, useIdentity(types, len) ? exports.identity : function (a) { return types[getIndex(a[tag])].encode(a); }, types, tag); | ||
}; | ||
@@ -896,0 +913,0 @@ // |
{ | ||
"name": "io-ts", | ||
"version": "1.2.1", | ||
"version": "1.3.0", | ||
"description": "TypeScript compatible runtime type system for IO validation", | ||
@@ -46,3 +46,3 @@ "files": [ | ||
"tslint-config-standard": "7.0.0", | ||
"typescript": "^2.9.2" | ||
"typescript": "^3.0.1" | ||
}, | ||
@@ -49,0 +49,0 @@ "tags": [ |
@@ -84,3 +84,3 @@ [![build status](https://img.shields.io/travis/gcanti/io-ts/master.svg?style=flat-square)](https://travis-ci.org/gcanti/io-ts) | ||
The stable version is tested against TypeScript 2.9.x+ | ||
The stable version is tested against TypeScript 3.0.1, but should run with TypeScript 2.7.2+ too | ||
@@ -117,2 +117,34 @@ # Error reporters | ||
You can define your own reporter. `Errors` has the following type | ||
```ts | ||
interface ContextEntry { | ||
readonly key: string | ||
readonly type: Decoder<any, any> | ||
} | ||
type Context = ReadonlyArray<ContextEntry> | ||
interface ValidationError { | ||
readonly value: mixed | ||
readonly context: Context | ||
} | ||
type Errors = Array<ValidationError> | ||
``` | ||
Example | ||
```ts | ||
import * as t from 'io-ts' | ||
const getPaths = <A>(v: t.Validation<A>): Array<string> => { | ||
return v.fold(errors => errors.map(error => error.context.map(({ key }) => key).join('.')), () => ['no errors']) | ||
} | ||
const Person = t.type({ | ||
name: t.string, | ||
age: t.number | ||
}) | ||
console.log(getPaths(Person.decode({}))) // => [ '.name', '.age' ] | ||
``` | ||
# Community | ||
@@ -144,6 +176,6 @@ | ||
```ts | ||
type IPerson = t.TypeOf<typeof Person> | ||
interface IPerson extends t.TypeOf<typeof Person> {} | ||
// same as | ||
type IPerson = { | ||
interface IPerson { | ||
name: string | ||
@@ -319,3 +351,3 @@ age: number | ||
type CT = t.TypeOf<typeof C> | ||
interface CT extends t.TypeOf<typeof C> {} | ||
@@ -329,2 +361,21 @@ // same as | ||
You can apply `partial` to an already defined runtime type via its `props` field | ||
```ts | ||
const Person = t.type({ | ||
name: t.string, | ||
age: t.number | ||
}) | ||
const PartialPerson = t.partial(Person.props) | ||
interface PartialPerson extends t.TypeOf<typeof PartialPerson> {} | ||
// same as | ||
interface PartialPerson { | ||
name?: string | ||
age?: number | ||
} | ||
``` | ||
# Custom types | ||
@@ -331,0 +382,0 @@ |
Sorry, the diff of this file is not supported yet
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
117957
1339
551