File tree Expand file tree Collapse file tree 4 files changed +47
-7
lines changed Expand file tree Collapse file tree 4 files changed +47
-7
lines changed Original file line number Diff line number Diff line change 1- import { pipe } from 'fp-ts/pipeable ' ;
1+ import { pipe } from 'fp-ts/function ' ;
22import * as E from 'fp-ts/Either' ;
33import * as R from 'fp-ts/Record' ;
44import * as t from 'io-ts' ;
@@ -101,12 +101,12 @@ export const flattened = <Props extends NestedProps>(
101101 const innerProps = props [ key ] ;
102102 flatProps = { ...flatProps , ...innerProps } ;
103103 }
104- const flatCodec = t . exact ( optionalized ( flatProps ) ) ;
104+ const flatCodec = t . exact ( optionalized ( flatProps ) , name ) ;
105105
106106 const nestedProps = R . map ( ( innerProps : t . Props ) => t . exact ( optionalized ( innerProps ) ) ) (
107107 props ,
108108 ) ;
109- const nestedCodec = t . strict ( nestedProps ) ;
109+ const nestedCodec = t . strict ( nestedProps , name ) ;
110110
111111 return new t . Type (
112112 name ,
Original file line number Diff line number Diff line change @@ -52,8 +52,8 @@ type EmitPropsErrors<P extends HttpRequestCombinatorProps> = {
5252
5353export function httpRequest <
5454 Props extends HttpRequestCombinatorProps & EmitPropsErrors < Props > ,
55- > ( props : Props ) {
56- return flattened ( 'httpRequest' , {
55+ > ( props : Props , name ?: string ) {
56+ return flattened ( name ?? 'httpRequest' , {
5757 query : { } ,
5858 params : { } ,
5959 ...( props as Omit < Props , 'query' | 'params' > ) ,
Original file line number Diff line number Diff line change 11import { describe , it } from 'node:test' ;
22import { strict as assert } from 'node:assert' ;
3-
3+ import * as PathReporter from 'io-ts/lib/PathReporter' ;
44import * as NEA from 'fp-ts/NonEmptyArray' ;
55import * as t from 'io-ts' ;
66import { nonEmptyArray , JsonFromString , NumberFromString } from 'io-ts-types' ;
7- import { assertRight } from './utils' ;
7+
8+ import { assertLeft , assertRight } from './utils' ;
89
910import { optional } from '../src/combinators' ;
1011import * as h from '../src/httpRequest' ;
@@ -138,4 +139,38 @@ describe('httpRequest', () => {
138139 // tslint:disable-next-line: no-unused-expression
139140 void _codec ;
140141 } ) ;
142+
143+ it ( 'Displays error with codec name on decode' , ( ) => {
144+ const request = h . httpRequest (
145+ {
146+ params : { } ,
147+ query : {
148+ foo : t . string ,
149+ } ,
150+ body : {
151+ bar : t . number ,
152+ } ,
153+ } ,
154+ 'TestRequestWithCodecName' ,
155+ ) ;
156+
157+ const test = {
158+ params : { } ,
159+ query : {
160+ foo : 'hello' ,
161+ } ,
162+ body : {
163+ bar : 'world' ,
164+ } ,
165+ } ;
166+
167+ const errors = assertLeft ( request . decode ( test ) ) ;
168+ const validationErrors = PathReporter . failure ( errors ) ;
169+ const validationMessage = validationErrors . join ( '\n' ) ;
170+
171+ assert (
172+ validationMessage . includes ( 'TestRequestWithCodecName' ) ,
173+ 'Expected error to include codec name' ,
174+ ) ;
175+ } ) ;
141176} ) ;
Original file line number Diff line number Diff line change @@ -7,6 +7,11 @@ export const assertRight = E.getOrElseW(() => {
77 throw new Error ( 'Failed to decode object' ) ;
88} ) ;
99
10+ export const assertLeft = < T > ( e : E . Either < t . Errors , T > ) => {
11+ assert ( E . isLeft ( e ) , 'Expected a failure, got a success' ) ;
12+ return e . left ;
13+ } ;
14+
1015export const assertEncodes = ( codec : t . Mixed , test : unknown , expected = test ) => {
1116 const encoded = codec . encode ( test ) ;
1217 assert . deepEqual ( encoded , expected ) ;
You can’t perform that action at this time.
0 commit comments