Skip to content

Commit f221896

Browse files
committed
feat: add option donotExclude
1 parent 766c02d commit f221896

File tree

3 files changed

+39
-3
lines changed

3 files changed

+39
-3
lines changed

__tests__/exclude.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,32 @@ describe('Exclude() decorator', () => {
4747
},
4848
})
4949
})
50+
51+
it('do not omits Exclude()-decorated properties from output schema', () => {
52+
const schema = validationMetadatasToSchemas({
53+
classTransformerMetadataStorage: defaultMetadataStorage,
54+
doNotExcludeDecorator: true,
55+
})
56+
57+
expect(schema).toEqual({
58+
Parent: {
59+
properties: {
60+
inherited: {},
61+
inheritedInternal: {},
62+
},
63+
type: 'object',
64+
required: ['inherited', 'inheritedInternal'],
65+
},
66+
User: {
67+
properties: {
68+
id: { type: 'string' },
69+
inherited: {},
70+
inheritedInternal: {},
71+
internal: {},
72+
},
73+
type: 'object',
74+
required: ['id', 'internal', 'inherited', 'inheritedInternal'],
75+
},
76+
})
77+
})
5078
})

src/index.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ 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((propMeta) => !isExcluded(propMeta, options))
55+
let metas = ownMetas.concat(getInheritedMetadatas(target, metadatas))
56+
if (!options.doNotExcludeDecorator) {
57+
metas = metas.filter((propMeta) => !isExcluded(propMeta, options))
58+
}
5859

5960
const properties: { [name: string]: SchemaObject } = {}
6061

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)