UNPKG

io-ts-extra

Version:

Adds pattern matching, optional properties, and several other helpers and types, to io-ts.

32 lines 1.35 kB
import { TypeOf, RefinementC, Context, Any } from 'io-ts'; /** * Like io-ts's refinement type but: * 1. Not deprecated (see https://github.com/gcanti/io-ts/issues/373) * 2. Passes in `Context` to the predicate argument, so you can check parent key names etc. * 3. Optionally allows returning another io-ts codec instead of a boolean for better error messages. * * @example * const CloudResources = narrow( * t.type({ * database: t.type({username: t.string, password: t.string}), * service: t.type({dbConnectionString: t.string}), * }), * ({database}) => t.type({ * service: t.type({dbConnectionString: t.literal(`${database.username}:${database.password}`)}), * }) * ) * * const valid = CloudResources.decode({ * database: {username: 'user', password: 'pass'}, * service: {dbConnectionString: 'user:pass'}, * }) * // returns a `Right` * * const invalid = CloudResources.decode({ * database: {username: 'user', password: 'pass'}, * service: {dbConnectionString: 'user:wrongpassword'}, * }) * // returns a `Left` - service.dbConnectionString expected "user:pass", but got "user:wrongpassword" */ export declare const narrow: <C extends Any, D extends Any>(codec: C, predicate: (value: TypeOf<C>, context: Context) => boolean | D, name?: string) => RefinementC<C>; //# sourceMappingURL=narrow.d.ts.map