Skip to content

Commit

Permalink
feat(msw): take string pattern property into account when generating …
Browse files Browse the repository at this point in the history
…mocks (orval-labs#1398)

Co-authored-by: Alfred Jonsson <[email protected]>
  • Loading branch information
AllieJonsson and Alfred Jonsson authored May 25, 2024
1 parent c799462 commit e32d4ec
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
6 changes: 4 additions & 2 deletions packages/mock/src/faker/getters/scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import {
mergeDeep,
MockOptions,
} from '@orval/core';
import { MockDefinition, MockSchemaObject } from '../../types';
import { DEFAULT_FORMAT_MOCK } from '../constants';
import {
getNullable,
resolveMockOverride,
resolveMockValue,
} from '../resolvers';
import { MockDefinition, MockSchemaObject } from '../../types';
import { getMockObject } from './object';
import { DEFAULT_FORMAT_MOCK } from '../constants';

export const getMockScalar = ({
item,
Expand Down Expand Up @@ -241,6 +241,8 @@ export const getMockScalar = ({
value = item.path?.endsWith('[]')
? `faker.helpers.arrayElements(${enumValue})`
: `faker.helpers.arrayElement(${enumValue})`;
} else if (item.pattern) {
value = `faker.helpers.fromRegExp('${item.pattern}')`;
}

return {
Expand Down
14 changes: 14 additions & 0 deletions tests/configs/swr.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,18 @@ export default defineConfig({
target: '../specifications/optional-request-body.yaml',
},
},
pattern: {
output: {
target: '../generated/swr/pattern/endpoints.ts',
schemas: '../generated/swr/pattern/model',
client: 'swr',
mock: true,
},
input: {
target: '../specifications/pattern.yaml',
override: {
transformer: '../transformers/add-version.js',
},
},
},
});
25 changes: 25 additions & 0 deletions tests/specifications/pattern.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
openapi: '3.0.0'
info:
version: 1.0.0
title: Pattern
paths:
/example:
get:
summary: Example
responses:
'200':
description: 'Example'
content:
application/json:
schema:
$ref: '#/components/schemas/Node'
components:
schemas:
Node:
type: object
required:
- guid
properties:
guid:
type: string
pattern: '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'

0 comments on commit e32d4ec

Please sign in to comment.