Skip to content

Commit 084b8a4

Browse files
committed
feat: add option donotExclude
1 parent 625eb19 commit 084b8a4

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

__tests__/exclude.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,32 @@ describe('Exclude() decorator', () => {
5555
},
5656
})
5757
})
58+
59+
it('do not omits Exclude()-decorated properties from output schema', () => {
60+
const schema = validationMetadatasToSchemas({
61+
classTransformerMetadataStorage: defaultMetadataStorage,
62+
doNotExcludeDecorator: true,
63+
})
64+
65+
expect(schema).toEqual({
66+
Parent: {
67+
properties: {
68+
inherited: {},
69+
inheritedInternal: {},
70+
},
71+
type: 'object',
72+
required: ['inherited', 'inheritedInternal'],
73+
},
74+
User: {
75+
properties: {
76+
id: { type: 'string' },
77+
inherited: {},
78+
inheritedInternal: {},
79+
internal: {},
80+
},
81+
type: 'object',
82+
required: ['id', 'internal', 'inherited', 'inheritedInternal'],
83+
},
84+
})
85+
})
5886
})

src/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,17 @@ export function validationMetadataArrayToSchemas(
5252
)
5353
).forEach(([key, ownMetas]) => {
5454
const target = ownMetas[0].target as Function
55-
const metas = ownMetas
56-
.concat(getInheritedMetadatas(target, metadatas))
57-
.filter(
55+
56+
let metas = ownMetas.concat(getInheritedMetadatas(target, metadatas))
57+
if (!options.doNotExcludeDecorator) {
58+
metas = metas.filter(
5859
(propMeta) =>
5960
!(
6061
isExcluded(propMeta, options) ||
6162
isExcluded({ ...propMeta, target }, options)
6263
)
6364
)
65+
}
6466

6567
const properties: { [name: string]: SchemaObject } = {}
6668

src/options.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,18 @@ export interface IOptions extends ValidatorOptions {
4040
* Defaults to `name`, i.e., class name.
4141
*/
4242
schemaNameField: string
43+
44+
/**
45+
* Do not exclude field which decorate with `Exclude` decorator.
46+
* Defaults to `false`
47+
*/
48+
doNotExcludeDecorator: boolean
4349
}
4450

4551
export const defaultOptions: IOptions = {
4652
additionalConverters: {},
4753
classValidatorMetadataStorage: getMetadataStorage(),
4854
refPointerPrefix: '#/definitions/',
4955
schemaNameField: 'name',
56+
doNotExcludeDecorator: false,
5057
}

0 commit comments

Comments
 (0)