diff --git a/__tests__/exclude.test.ts b/__tests__/exclude.test.ts index 45918b2..27fe602 100644 --- a/__tests__/exclude.test.ts +++ b/__tests__/exclude.test.ts @@ -55,4 +55,32 @@ describe('Exclude() decorator', () => { }, }) }) + + it('do not omits Exclude()-decorated properties from output schema', () => { + const schema = validationMetadatasToSchemas({ + classTransformerMetadataStorage: defaultMetadataStorage, + doNotExcludeDecorator: true, + }) + + expect(schema).toEqual({ + Parent: { + properties: { + inherited: {}, + inheritedInternal: {}, + }, + type: 'object', + required: ['inherited', 'inheritedInternal'], + }, + User: { + properties: { + id: { type: 'string' }, + inherited: {}, + inheritedInternal: {}, + internal: {}, + }, + type: 'object', + required: ['id', 'internal', 'inherited', 'inheritedInternal'], + }, + }) + }) }) diff --git a/src/index.ts b/src/index.ts index e43a42f..7e27c36 100644 --- a/src/index.ts +++ b/src/index.ts @@ -52,15 +52,17 @@ export function validationMetadataArrayToSchemas( ) ).forEach(([key, ownMetas]) => { const target = ownMetas[0].target as Function - const metas = ownMetas - .concat(getInheritedMetadatas(target, metadatas)) - .filter( + + let metas = ownMetas.concat(getInheritedMetadatas(target, metadatas)) + if (!options.doNotExcludeDecorator) { + metas = metas.filter( (propMeta) => !( isExcluded(propMeta, options) || isExcluded({ ...propMeta, target }, options) ) ) + } const properties: { [name: string]: SchemaObject } = {} diff --git a/src/options.ts b/src/options.ts index 02d0902..7b8b69d 100644 --- a/src/options.ts +++ b/src/options.ts @@ -40,6 +40,12 @@ export interface IOptions extends ValidatorOptions { * Defaults to `name`, i.e., class name. */ schemaNameField: string + + /** + * Do not exclude field which decorate with `Exclude` decorator. + * Defaults to `false` + */ + doNotExcludeDecorator: boolean } export const defaultOptions: IOptions = { @@ -47,4 +53,5 @@ export const defaultOptions: IOptions = { classValidatorMetadataStorage: getMetadataStorage(), refPointerPrefix: '#/definitions/', schemaNameField: 'name', + doNotExcludeDecorator: false, }