From 084b8a4c48834ff38037aa37534daeee9adc3037 Mon Sep 17 00:00:00 2001 From: Innei Date: Wed, 16 Feb 2022 21:14:31 +0800 Subject: [PATCH] feat: add option donotExclude --- __tests__/exclude.test.ts | 28 ++++++++++++++++++++++++++++ src/index.ts | 8 +++++--- src/options.ts | 7 +++++++ 3 files changed, 40 insertions(+), 3 deletions(-) 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, }