import { UnprocessableEntityException } from '@nestjs/common' ; import { Either , left , mapLeft , right } from 'fp-ts/lib/Either' ; import { pipe } from 'fp-ts/lib/function' ; import { fromEither , TaskEither } from 'fp-ts/lib/TaskEither' ; import { ValidationError } from 'io-ts' ; import { RuntypeBase } from 'runtypes/lib/runtype' ; param validator export const validateWith = < Input , Data > ( validator , dataKind ) => ( data ) : TaskEither < Error , Data > => { return pipe ( data , checkEither ( validator . check ) , mapLeft ( ( ) => new UnprocessableEntityException ( `The data ${ dataKind } is invalid` , `invalid-${ dataKind }` ) ) , fromEither ) ; } ; const checkEither = < D > ( check ) => ( value ) : Either < ValidationError , D > => { try { const checkedValue = check ( value ) ; return right ( checkedValue ) ; } catch ( error ) { return left ( error ) ; } } ;