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
31 changes: 31 additions & 0 deletions __tests__/exclude.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,35 @@ 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: {
excludedInUser: {},
inherited: {},
inheritedInternal: {},
},
type: 'object',
required: ['inherited', 'inheritedInternal', 'excludedInUser'],
},
User: {
properties: {
excludedInUser: {},
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,
}