Skip to content

Commit ca9add2

Browse files
committed
fix: yup type inference bug
Closes #12
1 parent 1205cfe commit ca9add2

File tree

5 files changed

+58
-56
lines changed

5 files changed

+58
-56
lines changed

examples/sst-v2/packages/functions/src/api-yup/models.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ export const GetTodoPath = object({
55
})
66

77
export const ListQuery = object({
8-
skip: number().optional(),
9-
take: number().optional(),
8+
skip: number(),
9+
take: number(),
1010
})
1111

1212
export const CreateTodoRequest = object({
13-
id: string(),
14-
title: string(),
15-
description: string(),
16-
due: date(),
13+
id: string().required(),
14+
title: string().required(),
15+
description: string().required(),
16+
due: date().required(),
1717
})
1818

1919
export const TodoResponse = object({

examples/sst-v2/packages/functions/tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"module": "esnext",
55
"moduleResolution": "node",
66
"baseUrl": ".",
7+
"strictNullChecks": true,
8+
"strictFunctionTypes": false,
79
"allowSyntheticDefaultImports": true,
810
"paths": {
911
"@sst-v2/core/*": ["../core/src/*"],

package/src/core/parsers.ts

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
/**
2-
* parsers.ts
3-
*
42
* Schema parsing / validation types
53
* We use duck typing here, so that we don't need to pull in the package deps.
64
*/
@@ -11,52 +9,7 @@ export type ZodSchemaLike<TInput> = {
119
}
1210

1311
export type YupSchemaLike<TInput> = {
14-
validate(value: TInput, options?: any): Promise<any>
12+
validate(value: unknown, options?: any): Promise<TInput>
1513
}
1614

1715
export type Schema<T> = ZodSchemaLike<T> | YupSchemaLike<T>
18-
19-
/**
20-
* Api parsing options
21-
*/
22-
export interface ApiParser<TResponse, TRequest, TPath, TQuery> {
23-
/**
24-
* Request body parser
25-
*/
26-
request?: Schema<TRequest>
27-
28-
/**
29-
* Response parser.
30-
*
31-
* If you would like to skip runtime validation of this schema, set the validateResponses property to false.
32-
*
33-
* @example
34-
* ```typescript
35-
* parser: {
36-
* response: z.object({})
37-
* }
38-
* ```
39-
*/
40-
response?: Schema<TResponse>
41-
42-
/**
43-
* URI path parser
44-
*/
45-
path?: Schema<TPath>
46-
47-
/**
48-
* URI querystring parser
49-
*/
50-
query?: Schema<TQuery>
51-
52-
/**
53-
* Response validation setting
54-
*
55-
* - 'never' - do not perform response validation
56-
* - 'warn' - log warning and proceed
57-
* - 'error' - raise 500 error
58-
*
59-
* @default 'error'
60-
*/
61-
validateResponses?: 'never' | 'warn' | 'error'
62-
}

package/src/integrations/api/types.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Options as CorsOptions } from '@middy/http-cors'
2-
import { Context, FuncyOptions } from 'package/src/core'
2+
import { Context, FuncyOptions } from '@core'
33
import { APIGatewayProxyStructuredResultV2 } from 'aws-lambda'
44
// WORKAROUND: Found a strange behaviour where linking via alias will break implicit typing..
5-
import { ApiParser } from '../../core/parsers'
5+
import { Schema } from '../../core/parsers'
66

77
//export type APIGatewayResult = APIGatewayProxyStructuredResultV2 | APIGatewayProxyResult
88

@@ -125,6 +125,51 @@ export interface FuncyApiOptions<
125125
}
126126
}
127127

128+
/**
129+
* Api parsing options
130+
*/
131+
export interface ApiParser<TResponse, TRequest, TPath, TQuery> {
132+
/**
133+
* Request body parser
134+
*/
135+
request?: Schema<TRequest>
136+
137+
/**
138+
* Response parser.
139+
*
140+
* If you would like to skip runtime validation of this schema, set the validateResponses property to false.
141+
*
142+
* @example
143+
* ```typescript
144+
* parser: {
145+
* response: z.object({})
146+
* }
147+
* ```
148+
*/
149+
response?: Schema<TResponse>
150+
151+
/**
152+
* URI path parser
153+
*/
154+
path?: Schema<TPath>
155+
156+
/**
157+
* URI querystring parser
158+
*/
159+
query?: Schema<TQuery>
160+
161+
/**
162+
* Response validation setting
163+
*
164+
* - 'never' - do not perform response validation
165+
* - 'warn' - log warning and proceed
166+
* - 'error' - raise 500 error
167+
*
168+
* @default 'error'
169+
*/
170+
validateResponses?: 'never' | 'warn' | 'error'
171+
}
172+
128173
// NOTE: Not exported from middy packages
129174
interface SecurityOptions {
130175
dnsPrefetchControl?: {

tsconfig.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
"baseUrl": ".",
77
"allowSyntheticDefaultImports": true,
88
"declaration": true,
9+
"strictNullChecks": true,
10+
"strictFunctionTypes": false,
911
"outDir": "./dist",
1012
"paths": {
1113
"@core": ["package/src/core"],

0 commit comments

Comments
 (0)