-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Result } from 'true-myth'; | ||
|
||
import { generateSchemas, input, make, output } from '../../tests/helpers'; | ||
import { IsUnknown } from '../nestjs-swagger-dto'; | ||
|
||
describe('IsUnknown', () => { | ||
describe('single', () => { | ||
class Test { | ||
@IsUnknown() | ||
unknownField!: unknown; | ||
} | ||
|
||
it('generates correct schema', async () => { | ||
expect(await generateSchemas([Test])).toStrictEqual({ | ||
Test: { | ||
type: 'object', | ||
properties: { | ||
unknownField: { | ||
oneOf: [ | ||
{ type: 'string' }, | ||
{ type: 'number' }, | ||
{ type: 'integer' }, | ||
{ type: 'boolean' }, | ||
{ type: 'array' }, | ||
{ type: 'object' }, | ||
], | ||
}, | ||
}, | ||
required: ['unknownField'], | ||
}, | ||
}); | ||
}); | ||
|
||
it('accepts anything except null and undefined', async () => { | ||
expect(await input(Test, { unknownField: false })).toStrictEqual( | ||
Result.ok(make(Test, { unknownField: false })) | ||
); | ||
expect(await input(Test, { unknownField: 123 })).toStrictEqual( | ||
Result.ok(make(Test, { unknownField: 123 })) | ||
); | ||
expect(await input(Test, { unknownField: 'abc' })).toStrictEqual( | ||
Result.ok(make(Test, { unknownField: 'abc' })) | ||
); | ||
expect(await input(Test, { unknownField: [] })).toStrictEqual( | ||
Result.ok(make(Test, { unknownField: [] })) | ||
); | ||
expect(await input(Test, { unknownField: {} })).toStrictEqual( | ||
Result.ok(make(Test, { unknownField: {} })) | ||
); | ||
}); | ||
|
||
it('transforms to plain', async () => { | ||
const dto = make(Test, { unknownField: true }); | ||
expect(output(dto)).toStrictEqual({ unknownField: true }); | ||
}); | ||
|
||
it('rejects null and undefined by default', async () => { | ||
expect(await input(Test, { unknownField: null })).toStrictEqual( | ||
Result.err('unknownField should not be null or undefined') | ||
); | ||
expect(await input(Test, {})).toStrictEqual( | ||
Result.err('unknownField should not be null or undefined') | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { IsDefined } from 'class-validator'; | ||
|
||
import { BasePropertyOptions, compose } from '../core'; | ||
|
||
export const IsUnknown = ({ | ||
...base | ||
}: BasePropertyOptions & { | ||
example?: unknown; | ||
default?: unknown; | ||
} = {}): PropertyDecorator => | ||
compose( | ||
{ | ||
oneOf: [ | ||
{ type: 'string' }, | ||
{ type: 'number' }, | ||
{ type: 'integer' }, | ||
{ type: 'boolean' }, | ||
{ type: 'array' }, | ||
{ type: 'object' }, | ||
], | ||
}, | ||
base, | ||
IsDefined() | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters