Skip to content

Commit

Permalink
feat: add option donotExclude
Browse files Browse the repository at this point in the history
  • Loading branch information
Innei committed Jun 4, 2022
1 parent 625eb19 commit 084b8a4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
28 changes: 28 additions & 0 deletions __tests__/exclude.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
},
})
})
})
8 changes: 5 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } = {}

Expand Down
7 changes: 7 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,18 @@ 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 = {
additionalConverters: {},
classValidatorMetadataStorage: getMetadataStorage(),
refPointerPrefix: '#/definitions/',
schemaNameField: 'name',
doNotExcludeDecorator: false,
}

0 comments on commit 084b8a4

Please sign in to comment.