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 Feb 16, 2022
1 parent 766c02d commit f221896
Show file tree
Hide file tree
Showing 3 changed files with 39 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 @@ -47,4 +47,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'],
},
})
})
})
7 changes: 4 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@ export function validationMetadataArrayToSchemas(
)
).forEach(([key, ownMetas]) => {
const target = ownMetas[0].target as Function
const metas = ownMetas
.concat(getInheritedMetadatas(target, metadatas))
.filter((propMeta) => !isExcluded(propMeta, options))
let metas = ownMetas.concat(getInheritedMetadatas(target, metadatas))
if (!options.doNotExcludeDecorator) {
metas = metas.filter((propMeta) => !isExcluded(propMeta, 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 f221896

Please sign in to comment.