Skip to content

Commit 3785ba0

Browse files
committed
Add outputRegexConstants docs
1 parent ed49143 commit 3785ba0

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

docs/rtk-query/usage/code-generation.mdx

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ interface SimpleUsage {
121121
endpointOverrides?: EndpointOverrides[]
122122
flattenArg?: boolean
123123
useEnumType?: boolean
124+
outputRegexConstants?: boolean
124125
httpResolverOptions?: SwaggerParser.HTTPResolverOptions
125126
}
126127

@@ -192,6 +193,47 @@ const withOverride: ConfigFile = {
192193

193194
Setting `hooks: true` will generate `useQuery` and `useMutation` hook exports. If you also want `useLazyQuery` hooks generated or more granular control, you can also pass an object in the shape of: `{ queries: boolean; lazyQueries: boolean; mutations: boolean }`.
194195

196+
#### Generating regex constants for schema patterns
197+
198+
If your OpenAPI schema uses the [`pattern` keyword](https://swagger.io/docs/specification/data-models/data-types/#pattern) to specify regex validation on string properties, you can export these patterns as JavaScript regex constants by setting `outputRegexConstants: true`.
199+
200+
```ts no-transpile title="openapi-config.ts"
201+
const config: ConfigFile = {
202+
schemaFile: 'https://petstore3.swagger.io/api/v3/openapi.json',
203+
apiFile: './src/store/emptyApi.ts',
204+
outputFile: './src/store/petApi.ts',
205+
outputRegexConstants: true,
206+
}
207+
```
208+
209+
For a schema with pattern-validated properties like:
210+
211+
```yaml
212+
User:
213+
type: object
214+
properties:
215+
email:
216+
type: string
217+
pattern: '^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
218+
phone:
219+
type: string
220+
pattern: '^\+?[1-9]\d{1,14}$'
221+
```
222+
223+
The codegen will generate:
224+
225+
```ts no-transpile title="Generated output"
226+
export const userEmailPattern =
227+
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
228+
export const userPhonePattern = /^\+?[1-9]\d{1,14}$/
229+
```
230+
231+
These constants can be used for client-side validation to ensure consistency with API expectations.
232+
233+
:::note
234+
Only string-type properties with non-empty `pattern` values will generate constants. The constant name follows the format `{typeName}{propertyName}Pattern` in camelCase.
235+
:::
236+
195237
#### Multiple output files
196238

197239
```ts no-transpile title="openapi-config.ts"

0 commit comments

Comments
 (0)