io-ts-types
- Version 0.5.19
- Published
- 137 kB
- No dependencies
- MIT license
Install
npm i io-ts-types
yarn add io-ts-types
pnpm add io-ts-types
Overview
A collection of codecs and combinators for use with io-ts
Index
Variables
Functions
Interfaces
Type Aliases
Variables
variable BigIntFromString
const BigIntFromString: BigIntFromStringC;
Example 1
import { BigIntFromString } from 'io-ts-types/lib/BigIntFromString' import { right } from 'fp-ts/lib/Either' import { PathReporter } from 'io-ts/lib/PathReporter'
assert.deepStrictEqual(BigIntFromString.decode('1'), right(BigInt(1))) assert.deepStrictEqual(PathReporter.report(BigIntFromString.decode('1.1')), ['Invalid value "1.1" supplied to : BigIntFromString']) assert.deepStrictEqual(PathReporter.report(BigIntFromString.decode('')), ['Invalid value "" supplied to : BigIntFromString']) assert.deepStrictEqual(PathReporter.report(BigIntFromString.decode(' ')), ['Invalid value " " supplied to : BigIntFromString'])
0.5.11
variable BooleanFromNumber
const BooleanFromNumber: BooleanFromNumberC;
Example 1
import { BooleanFromNumber } from 'io-ts-types/lib/BooleanFromNumber' import { right } from 'fp-ts/lib/Either' import { PathReporter } from 'io-ts/lib/PathReporter'
assert.deepStrictEqual(BooleanFromNumber.decode(1), right(true)) assert.deepStrictEqual(BooleanFromNumber.decode(0), right(false)) assert.deepStrictEqual(BooleanFromNumber.decode(123), right(true)) assert.deepStrictEqual(PathReporter.report(BooleanFromNumber.decode('a')), ['Invalid value "a" supplied to : BooleanFromNumber'])
0.5.13
variable BooleanFromString
const BooleanFromString: BooleanFromStringC;
Example 1
import { BooleanFromString } from 'io-ts-types/lib/BooleanFromString' import { right } from 'fp-ts/lib/Either' import { PathReporter } from 'io-ts/lib/PathReporter'
assert.deepStrictEqual(BooleanFromString.decode('true'), right(true)) assert.deepStrictEqual(BooleanFromString.decode('false'), right(false)) assert.deepStrictEqual(PathReporter.report(BooleanFromString.decode('a')), ['Invalid value "a" supplied to : BooleanFromString'])
0.5.0
variable date
const date: DateC;
0.5.0
variable DateFromISOString
const DateFromISOString: DateFromISOStringC;
Example 1
import { DateFromISOString } from 'io-ts-types/lib/DateFromISOString' import { right } from 'fp-ts/lib/Either'
const date = new Date(1973, 10, 30) const input = date.toISOString() assert.deepStrictEqual(DateFromISOString.decode(input), right(date))
0.5.0
variable DateFromNumber
const DateFromNumber: DateFromNumberC;
Example 1
import { DateFromNumber } from 'io-ts-types/lib/DateFromNumber' import { right } from 'fp-ts/lib/Either'
const date = new Date(1973, 10, 30) const input = date.getTime() assert.deepStrictEqual(DateFromNumber.decode(input), right(date))
0.5.0
variable DateFromUnixTime
const DateFromUnixTime: DateFromUnixTimeC;
Example 1
import { DateFromUnixTime } from 'io-ts-types/lib/DateFromUnixTime' import { right } from 'fp-ts/lib/Either'
const date = new Date(1973, 10, 30) const input = date.getTime() / 1000 assert.deepStrictEqual(DateFromUnixTime.decode(input), right(date))
0.5.0
variable IntFromString
const IntFromString: IntFromStringC;
A codec that succeeds if a string can be parsed to an integer
Example 1
import { IntFromString } from 'io-ts-types/lib/IntFromString' import { right } from 'fp-ts/lib/Either' import { PathReporter } from 'io-ts/lib/PathReporter'
assert.deepStrictEqual(IntFromString.decode('1'), right(1)) assert.deepStrictEqual(PathReporter.report(IntFromString.decode('1.1')), ['Invalid value "1.1" supplied to : IntFromString'])
0.4.4
variable Json
const Json: t.Type<Json>;
0.5.15
variable JsonArray
const JsonArray: t.Type<JsonArray>;
0.5.15
variable JsonFromString
const JsonFromString: t.Type<Json, string, string>;
0.5.14
variable JsonRecord
const JsonRecord: t.Type<JsonRecord>;
0.5.15
variable NonEmptyString
const NonEmptyString: NonEmptyStringC;
A codec that succeeds if a string is not empty
Example 1
import { NonEmptyString } from 'io-ts-types/lib/NonEmptyString' import { right } from 'fp-ts/lib/Either' import { PathReporter } from 'io-ts/lib/PathReporter'
assert.deepStrictEqual(NonEmptyString.decode('a'), right('a')) assert.deepStrictEqual(PathReporter.report(NonEmptyString.decode('')), ['Invalid value "" supplied to : NonEmptyString'])
0.4.5
variable NumberFromString
const NumberFromString: NumberFromStringC;
Example 1
import { NumberFromString } from 'io-ts-types/lib/NumberFromString'
NumberFromString.decode('1') // right(1) NumberFromString.decode('1.1') // right(1.1)
0.5.0
variable regexp
const regexp: RegExpC;
Example 1
import { regexp } from 'io-ts-types/lib/regexp' import { right } from 'fp-ts/lib/Either'
const input1 = /\w+/ const input2 = new RegExp('\w+') assert.deepStrictEqual(regexp.decode(input1), right(input1)) assert.deepStrictEqual(regexp.decode(input2), right(input2))
0.4.4
variable UUID
const UUID: t.BrandC<t.StringC, UUIDBrand>;
Example 1
import { UUID } from 'io-ts-types/lib/UUID' import { right } from 'fp-ts/lib/Either' import { PathReporter } from 'io-ts/lib/PathReporter'
assert.deepStrictEqual(UUID.decode('00000000-0000-0000-0000-000000000000'), right('00000000-0000-0000-0000-000000000000')) assert.deepStrictEqual(PathReporter.report(UUID.decode('not a uuid')), ['Invalid value "not a uuid" supplied to : UUID'])
0.4.6
Functions
function clone
clone: <C extends t.Any>(t: C) => C;
Returns a clone of the given codec
Example 1
import { clone } from 'io-ts-types/lib/clone' import * as t from 'io-ts'
assert.deepStrictEqual(clone(t.string), t.string)
0.4.3
function either
either: <L extends t.Mixed, R extends t.Mixed>( leftCodec: L, rightCodec: R, name?: string) => EitherC<L, R>;
Given a codec representing a type
L
and a codec representing a typeA
, returns a codec representingEither<L, A>
that is able to deserialize the JSON representation of anEither
.Example 1
import { either } from 'io-ts-types/lib/either' import { left, right } from 'fp-ts/lib/Either' import * as t from 'io-ts' import { PathReporter } from 'io-ts/lib/PathReporter'
const T = either(t.string, t.number)
assert.deepStrictEqual(T.decode(right(1)), right(right(1))) assert.deepStrictEqual(T.decode(left('a')), right(left('a'))) assert.deepStrictEqual(PathReporter.report(T.decode(right('a'))), ['Invalid value "a" supplied to : Either<string, number>/1: Right/right: number'])
0.5.0
function fromNewtype
fromNewtype: <N extends AnyNewtype = never>( codec: t.Type<CarrierOf<N>, t.OutputOf<CarrierOf<N>>>, name?: string) => t.Type<N, CarrierOf<N>, unknown>;
Returns a codec from a newtype
Example 1
import { fromNewtype } from 'io-ts-types/lib/fromNewtype' import * as t from 'io-ts' import { right } from 'fp-ts/lib/Either' import { PathReporter } from 'io-ts/lib/PathReporter' import { Newtype, iso } from 'newtype-ts'
interface Token extends Newtype<{ readonly Token: unique symbol }, string> {}
const T = fromNewtype(t.string)
assert.deepStrictEqual(T.decode('sometoken'), right(iso().wrap('sometoken'))) assert.deepStrictEqual(PathReporter.report(T.decode(42)), ['Invalid value 42 supplied to : fromNewtype(string)'])
0.5.2
function fromNullable
fromNullable: <C extends t.Mixed>(codec: C, a: t.TypeOf<C>, name?: string) => C;
Returns a clone of the given codec that replace a nullable input with the given value
a
Example 1
import { fromNullable } from 'io-ts-types/lib/fromNullable' import * as t from 'io-ts' import { right } from 'fp-ts/lib/Either' import { PathReporter } from 'io-ts/lib/PathReporter'
const T = fromNullable(t.number, -1)
assert.deepStrictEqual(T.decode(1), right(1)) assert.deepStrictEqual(T.decode(null), right(-1)) assert.deepStrictEqual(T.decode(undefined), right(-1)) assert.deepStrictEqual(PathReporter.report(T.decode('a')), ['Invalid value "a" supplied to : fromNullable(number)'])
0.5.0
function fromRefinement
fromRefinement: <A>( name: string, is: (u: unknown) => u is A) => t.Type<A, A, unknown>;
Returns a codec from a refinement
0.4.4
function getLenses
getLenses: <C extends unknown>( codec: C) => { [x: string]: Lens<t.TypeOf<C>, t.TypeOf<C>> };
Return a
Lens
for each propExample 1
import * as t from 'io-ts' import { getLenses } from 'io-ts-types/lib/getLenses'
const Person = t.type({ name: t.string, age: t.number })
const lenses = getLenses(Person) assert.strictEqual(lenses.age.get({ name: 'Giulio', age: 44 }), 44)
0.5.0
function mapFromEntries
mapFromEntries: <K extends t.Mixed, V extends t.Mixed>( keyCodec: K, KO: Ord<t.TypeOf<K>>, valueCodec: V, name?: string) => MapFromEntriesC<K, V>;
0.5.19
function mapOutput
mapOutput: <A, O, I, P>( codec: t.Type<A, O, I>, f: (p: O) => P, name?: string) => t.Type<A, P, I>;
Changes the output type of the given runtime type
Example 1
import * as t from 'io-ts' import { mapOutput } from 'io-ts-types/lib/mapOutput' import { optionFromNullable } from 'io-ts-types/lib/optionFromNullable' import { none, some } from 'fp-ts/lib/Option'
// Input: t.Type<Option, number | null, t.mixed> const Input = optionFromNullable(t.number)
const toUndefined = (x: A | null): A | undefined => (x === null ? undefined : x)
// Output: t.Type<Option, number | undefined, t.mixed> const Output = mapOutput(Input, toUndefined)
assert.strictEqual(Output.encode(none), undefined) assert.strictEqual(Output.encode(some(1)), 1)
0.3.2
function nonEmptyArray
nonEmptyArray: <C extends t.Mixed>(codec: C, name?: string) => NonEmptyArrayC<C>;
0.5.0
function option
option: <C extends t.Mixed>(codec: C, name?: string) => OptionC<C>;
0.5.0
function optionFromNullable
optionFromNullable: <C extends t.Mixed>( codec: C, name?: string) => OptionFromNullableC<C>;
0.5.0
function readonlyMapFromEntries
readonlyMapFromEntries: <K extends t.Mixed, V extends t.Mixed>( keyCodec: K, KO: Ord<t.TypeOf<K>>, valueCodec: V, name?: string) => ReadonlyMapFromEntriesC<K, V>;
0.5.19
function readonlyNonEmptyArray
readonlyNonEmptyArray: <C extends t.Mixed>( codec: C, name?: string) => ReadonlyNonEmptyArrayC<C>;
0.5.7
function readonlySetFromArray
readonlySetFromArray: <C extends t.Mixed>( codec: C, O: Ord<t.TypeOf<C>>, name?: string) => ReadonlySetFromArrayC<C>;
0.5.7
function setFromArray
setFromArray: <C extends t.Mixed>( codec: C, O: Ord<t.TypeOf<C>>, name?: string) => SetFromArrayC<C>;
0.5.0
function withEncode
withEncode: <A, O, I, P>( codec: t.Type<A, O, I>, encode: (a: A) => P, name?: string) => t.Type<A, P, I>;
Returns a clone of the given codec which uses the given
encode
functionExample 1
import { withEncode } from 'io-ts-types/lib/withEncode' import * as t from 'io-ts' import { PathReporter } from 'io-ts/lib/PathReporter' import { right } from 'fp-ts/lib/Either'
const T = withEncode(t.number, String)
assert.deepStrictEqual(T.decode(1), right(1)) assert.deepStrictEqual(T.encode(1), '1') assert.deepStrictEqual(PathReporter.report(T.decode('str')), ['Invalid value "str" supplied to : number'])
0.5.12
function withFallback
withFallback: <C extends t.Any>(codec: C, a: t.TypeOf<C>, name?: string) => C;
Returns a clone of the given codec that always succeed using the given value
a
if the original codec failsExample 1
import { withFallback } from 'io-ts-types/lib/withFallback' import * as t from 'io-ts' import { right } from 'fp-ts/lib/Either'
const T = withFallback(t.number, -1)
assert.deepStrictEqual(T.decode(1), right(1)) assert.deepStrictEqual(T.decode('a'), right(-1))
0.5.0
function withMessage
withMessage: <C extends t.Any>( codec: C, message: (i: t.InputOf<C>, c: t.Context) => string) => C;
Returns a clone of the given codec that sets the given string as error messsage
Example 1
import { withMessage } from 'io-ts-types/lib/withMessage' import * as t from 'io-ts' import { PathReporter } from 'io-ts/lib/PathReporter' import { right } from 'fp-ts/lib/Either'
const T = withMessage(t.number, () => 'Invalid number')
assert.deepStrictEqual(T.decode(1), right(1)) assert.deepStrictEqual(PathReporter.report(T.decode(null)), ['Invalid number'])
0.4.3
function withValidate
withValidate: <C extends t.Any>( codec: C, validate: C['validate'], name?: string) => C;
Returns a clone of the given codec which uses the given
validate
functionExample 1
import { withValidate } from 'io-ts-types/lib/withValidate' import * as t from 'io-ts' import { PathReporter } from 'io-ts/lib/PathReporter' import { either, right } from 'fp-ts/lib/Either'
const T = withValidate(t.number, (u, c) => either.map(t.number.validate(u, c), n => n * 2))
assert.deepStrictEqual(T.decode(1), right(2)) assert.deepStrictEqual(PathReporter.report(T.decode(null)), ['Invalid value null supplied to : number'])
0.4.3
Interfaces
interface BigIntFromStringC
interface BigIntFromStringC extends t.Type<bigint, string, unknown> {}
0.5.11
interface BooleanFromNumberC
interface BooleanFromNumberC extends t.Type<boolean, number, unknown> {}
0.5.13
interface BooleanFromStringC
interface BooleanFromStringC extends t.Type<boolean, string, unknown> {}
0.5.0
interface DateC
interface DateC extends t.Type<Date, Date, unknown> {}
0.5.0
interface DateFromISOStringC
interface DateFromISOStringC extends t.Type<Date, string, unknown> {}
0.5.0
interface DateFromNumberC
interface DateFromNumberC extends t.Type<Date, number, unknown> {}
0.5.0
interface DateFromUnixTimeC
interface DateFromUnixTimeC extends t.Type<Date, number, unknown> {}
0.5.0
interface EitherC
interface EitherC<L extends t.Mixed, R extends t.Mixed> extends t.Type< Either<t.TypeOf<L>, t.TypeOf<R>>, EitherOutput<t.OutputOf<L>, t.OutputOf<R>>, unknown > {}
0.5.0
interface ExactHasLenses
interface ExactHasLenses extends t.ExactType<HasLenses> {}
0.5.0
interface IntFromStringC
interface IntFromStringC extends t.Type<t.Int, string, unknown> {}
0.4.4
interface JsonArray
interface JsonArray extends ReadonlyArray<Json> {}
0.5.14
interface JsonRecord
interface JsonRecord {}
0.5.14
index signature
readonly [key: string]: Json;
interface MapFromEntriesC
interface MapFromEntriesC<K extends t.Mixed, V extends t.Mixed> extends t.Type< Map<t.TypeOf<K>, t.TypeOf<V>>, Array<[t.OutputOf<K>, t.OutputOf<V>]>, unknown > {}
0.5.19
interface NonEmptyArrayC
interface NonEmptyArrayC<C extends t.Mixed> extends t.Type< NonEmptyArray<t.TypeOf<C>>, NonEmptyArray<t.OutputOf<C>>, unknown > {}
0.5.0
interface NonEmptyStringBrand
interface NonEmptyStringBrand {}
0.4.5
property NonEmptyString
readonly NonEmptyString: unique symbol;
interface NonEmptyStringC
interface NonEmptyStringC extends t.Type<NonEmptyString, string, unknown> {}
0.4.5
interface NumberFromStringC
interface NumberFromStringC extends t.Type<number, string, unknown> {}
0.5.0
interface OptionC
interface OptionC<C extends t.Mixed> extends t.Type<Option<t.TypeOf<C>>, OptionOutput<t.OutputOf<C>>, unknown> {}
Given a codec representing a type
A
, returns a codec representingOption<A>
that is able to deserialize the JSON representation of anOption
.Example 1
import { option } from 'io-ts-types/lib/option' import { right } from 'fp-ts/lib/Either' import { none, some } from 'fp-ts/lib/Option' import * as t from 'io-ts' import { PathReporter } from 'io-ts/lib/PathReporter'
const T = option(t.number)
assert.deepStrictEqual(T.decode(none), right(none)) assert.deepStrictEqual(T.decode(some(1)), right(some(1))) assert.deepStrictEqual(PathReporter.report(T.decode(some('a'))), ['Invalid value "a" supplied to : Option/1: Some/value: number'])
0.5.0
interface OptionFromNullableC
interface OptionFromNullableC<C extends t.Mixed> extends t.Type<O.Option<t.TypeOf<C>>, t.OutputOf<C> | null, unknown> {}
0.5.0
interface ReadonlyMapFromEntriesC
interface ReadonlyMapFromEntriesC<K extends t.Mixed, V extends t.Mixed> extends t.Type< ReadonlyMap<t.TypeOf<K>, t.TypeOf<V>>, ReadonlyArray<[t.OutputOf<K>, t.OutputOf<V>]>, unknown > {}
0.5.19
interface ReadonlyNonEmptyArray
interface ReadonlyNonEmptyArray<A> extends ReadonlyArray<A> {}
0.5.7
property 0
readonly 0: A;
interface ReadonlyNonEmptyArrayC
interface ReadonlyNonEmptyArrayC<C extends t.Mixed> extends t.Type< ReadonlyNonEmptyArray<t.TypeOf<C>>, ReadonlyNonEmptyArray<t.OutputOf<C>>, unknown > {}
0.5.7
interface ReadonlySetFromArrayC
interface ReadonlySetFromArrayC<C extends t.Mixed> extends t.Type< ReadonlySet<t.TypeOf<C>>, ReadonlyArray<t.OutputOf<C>>, unknown > {}
0.5.7
interface RegExpC
interface RegExpC extends t.Type<RegExp, RegExp, unknown> {}
0.4.4
interface SetFromArrayC
interface SetFromArrayC<C extends t.Mixed> extends t.Type<Set<t.TypeOf<C>>, Array<t.OutputOf<C>>, unknown> {}
0.5.0
Type Aliases
type EitherOutput
type EitherOutput<L, R> = LeftOutput<L> | RightOutput<R>;
0.5.18
type HasLenses
type HasLenses = t.InterfaceType<any> | ExactHasLenses;
0.5.0
type Json
type Json = boolean | number | string | null | JsonArray | JsonRecord;
Copied from
fp-ts/Either
module.0.5.14
type LeftOutput
type LeftOutput<L> = { _tag: 'Left'; left: L;};
0.5.18
type NonEmptyString
type NonEmptyString = t.Branded<string, NonEmptyStringBrand>;
0.4.5
type NoneOutput
type NoneOutput = t.OutputOf<typeof None>;
0.5.18
type OptionOutput
type OptionOutput<A> = NoneOutput | SomeOutput<A>;
0.5.18
type RightOutput
type RightOutput<R> = { _tag: 'Right'; right: R;};
0.5.18
type SomeOutput
type SomeOutput<A> = { _tag: 'Some'; value: A;};
0.5.18
type UUID
type UUID = t.Branded<string, UUIDBrand>;
0.4.6
Package Files (33)
- lib/BigIntFromString.d.ts
- lib/BooleanFromNumber.d.ts
- lib/BooleanFromString.d.ts
- lib/DateFromISOString.d.ts
- lib/DateFromNumber.d.ts
- lib/DateFromUnixTime.d.ts
- lib/IntFromString.d.ts
- lib/JsonFromString.d.ts
- lib/NonEmptyString.d.ts
- lib/NumberFromString.d.ts
- lib/UUID.d.ts
- lib/clone.d.ts
- lib/date.d.ts
- lib/either.d.ts
- lib/fromNewtype.d.ts
- lib/fromNullable.d.ts
- lib/fromRefinement.d.ts
- lib/getLenses.d.ts
- lib/index.d.ts
- lib/mapFromEntries.d.ts
- lib/mapOutput.d.ts
- lib/nonEmptyArray.d.ts
- lib/option.d.ts
- lib/optionFromNullable.d.ts
- lib/readonlyMapFromEntries.d.ts
- lib/readonlyNonEmptyArray.d.ts
- lib/readonlySetFromArray.d.ts
- lib/regexp.d.ts
- lib/setFromArray.d.ts
- lib/withEncode.d.ts
- lib/withFallback.d.ts
- lib/withMessage.d.ts
- lib/withValidate.d.ts
Dependencies (0)
No dependencies.
Dev Dependencies (0)
No dev dependencies.
Peer Dependencies (4)
Badge
To add a badge like this oneto your package's README, use the codes available below.
You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/io-ts-types
.
- Markdown[![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/io-ts-types)
- HTML<a href="https://www.jsdocs.io/package/io-ts-types"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>
- Updated .
Package analyzed in 5201 ms. - Missing or incorrect documentation? Open an issue for this package.