Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/decorators/api-property.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ export function createApiPropertyDecorator(
};
}

if (options.pattern instanceof RegExp) {
options.pattern = options.pattern.source;
}
Comment on lines +87 to +89
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This adds new runtime behavior (converting options.pattern when it’s a RegExp), but there doesn’t appear to be a test asserting the generated schema’s pattern value. Please add a test that defines a DTO property with @ApiProperty({ pattern: /^[+]?abc$/ }) (and ideally one with flags, e.g. /abc/i) and verifies the resulting OpenAPI schema contains pattern: '^[+]?abc$' (and that flags are not emitted).

Copilot uses AI. Check for mistakes.

return createPropertyDecorator(
DECORATORS.API_MODEL_PROPERTIES,
options,
Expand Down
7 changes: 5 additions & 2 deletions lib/interfaces/schema-object-metadata.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ export type EnumAllowedTypes =
| Record<string, any>
| (() => any[] | Record<string, any>);

interface SchemaObjectCommonMetadata
extends Omit<SchemaObject, 'type' | 'required' | 'properties' | 'enum'> {
interface SchemaObjectCommonMetadata extends Omit<
SchemaObject,
'type' | 'required' | 'properties' | 'enum' | 'pattern'
> {
isArray?: boolean;
name?: string;
pattern?: string | RegExp;
enum?: EnumAllowedTypes;
}

Expand Down
63 changes: 63 additions & 0 deletions test/services/schema-object-factory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,69 @@ describe('SchemaObjectFactory', () => {
});
});

it('should convert RegExp pattern to string in schema', () => {
class RegExpPatternDto {
@ApiProperty({ pattern: /^[+]?abc$/ })
code: string;
}

const schemas: Record<string, SchemasObject> = {};
schemaObjectFactory.exploreModelSchema(RegExpPatternDto, schemas);

expect(schemas[RegExpPatternDto.name]).toEqual({
type: 'object',
properties: {
code: {
type: 'string',
pattern: '^[+]?abc$'
}
},
required: ['code']
});
});

it('should strip flags when converting RegExp pattern', () => {
class RegExpFlagsDto {
@ApiProperty({ pattern: /abc/i })
value: string;
}

const schemas: Record<string, SchemasObject> = {};
schemaObjectFactory.exploreModelSchema(RegExpFlagsDto, schemas);

expect(schemas[RegExpFlagsDto.name]).toEqual({
type: 'object',
properties: {
value: {
type: 'string',
pattern: 'abc'
}
},
required: ['value']
});
});

it('should keep string pattern unchanged', () => {
class StringPatternDto {
@ApiProperty({ pattern: '^[a-z]+$' })
slug: string;
}

const schemas: Record<string, SchemasObject> = {};
schemaObjectFactory.exploreModelSchema(StringPatternDto, schemas);

expect(schemas[StringPatternDto.name]).toEqual({
type: 'object',
properties: {
slug: {
type: 'string',
pattern: '^[a-z]+$'
}
},
required: ['slug']
});
});

it('should override base class metadata', () => {
class CreatUserDto {
@ApiProperty({ minLength: 0, required: true })
Expand Down