validator overview

export interface Validator<A, B> {
  (f: (a: A) => B): (a: A) => Validation<B>
}

Validator<A, B> is a type that can take a function A => B and provide runtime validation to it.

Example

import * as t from 'io-ts'
import { isLeft, isRight } from 'fp-ts/lib/Either'
import { pipe } from 'fp-ts/lib/function'
import { fromCodec } from 'arena-fp-ts/validator'

const UserCodec = t.type({
  name: t.string,
  password: t.string,
})

type User = t.TypeOf<typeof UserCodec>

const ValidUser = { name: 'foo', password: 'bar' }
const InvalidUser = { name: 'foo' }

const f = (user: User) => user.password // User => string
const f2 = pipe(f, fromCodec(UserCodec)) // unknown => t.Validation<string>

assert.deepStrictEqual(isRight(f2(ValidUser as any)), true)
assert.deepStrictEqual(isLeft(f2(InvalidUser as any)), true)

Added in v0.0.1


Table of contents


constructors

fromCodec

Signature

export declare function fromCodec<A, B>(codec: Decoder<unknown, A>): Validator<A, B>

Added in v0.0.1

model

Validator (interface)

Signature

export interface Validator<A, B> {
  (f: (a: A) => B): (a: unknown) => E.Either<Error, B>
}

Added in v0.0.1