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 ' ;
2
2
import * as E from 'fp-ts/Either' ;
3
3
import * as R from 'fp-ts/Record' ;
4
4
import * as t from 'io-ts' ;
@@ -101,12 +101,12 @@ export const flattened = <Props extends NestedProps>(
101
101
const innerProps = props [ key ] ;
102
102
flatProps = { ...flatProps , ...innerProps } ;
103
103
}
104
- const flatCodec = t . exact ( optionalized ( flatProps ) ) ;
104
+ const flatCodec = t . exact ( optionalized ( flatProps ) , name ) ;
105
105
106
106
const nestedProps = R . map ( ( innerProps : t . Props ) => t . exact ( optionalized ( innerProps ) ) ) (
107
107
props ,
108
108
) ;
109
- const nestedCodec = t . strict ( nestedProps ) ;
109
+ const nestedCodec = t . strict ( nestedProps , name ) ;
110
110
111
111
return new t . Type (
112
112
name ,
Original file line number Diff line number Diff line change @@ -52,8 +52,8 @@ type EmitPropsErrors<P extends HttpRequestCombinatorProps> = {
52
52
53
53
export function httpRequest <
54
54
Props extends HttpRequestCombinatorProps & EmitPropsErrors < Props > ,
55
- > ( props : Props ) {
56
- return flattened ( 'httpRequest' , {
55
+ > ( props : Props , name ?: string ) {
56
+ return flattened ( name ?? 'httpRequest' , {
57
57
query : { } ,
58
58
params : { } ,
59
59
...( props as Omit < Props , 'query' | 'params' > ) ,
Original file line number Diff line number Diff line change 1
1
import { describe , it } from 'node:test' ;
2
2
import { strict as assert } from 'node:assert' ;
3
-
3
+ import * as PathReporter from 'io-ts/lib/PathReporter' ;
4
4
import * as NEA from 'fp-ts/NonEmptyArray' ;
5
5
import * as t from 'io-ts' ;
6
6
import { nonEmptyArray , JsonFromString , NumberFromString } from 'io-ts-types' ;
7
- import { assertRight } from './utils' ;
7
+
8
+ import { assertLeft , assertRight } from './utils' ;
8
9
9
10
import { optional } from '../src/combinators' ;
10
11
import * as h from '../src/httpRequest' ;
@@ -138,4 +139,38 @@ describe('httpRequest', () => {
138
139
// tslint:disable-next-line: no-unused-expression
139
140
void _codec ;
140
141
} ) ;
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
+ } ) ;
141
176
} ) ;
Original file line number Diff line number Diff line change @@ -7,6 +7,11 @@ export const assertRight = E.getOrElseW(() => {
7
7
throw new Error ( 'Failed to decode object' ) ;
8
8
} ) ;
9
9
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
+
10
15
export const assertEncodes = ( codec : t . Mixed , test : unknown , expected = test ) => {
11
16
const encoded = codec . encode ( test ) ;
12
17
assert . deepEqual ( encoded , expected ) ;
You can’t perform that action at this time.
0 commit comments