-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #124 from dtymon/feature/handle-unknown-types
Attempt to handle unsupported Joi types rather than omitting them
- Loading branch information
Showing
4 changed files
with
139 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import Joi from 'joi'; | ||
import { convertSchema } from '../..'; | ||
|
||
// Add a couple of extensions to Joi, one without specifying the base type | ||
const ExtendedJoi = Joi.extend(joi => { | ||
const ext: Joi.Extension = { | ||
type: 'objectId', | ||
base: joi.string().meta({ baseType: 'string' }) | ||
}; | ||
return ext; | ||
}).extend((joi: any) => { | ||
const ext: Joi.Extension = { | ||
type: 'dollars', | ||
base: joi.number() | ||
}; | ||
return ext; | ||
}); | ||
|
||
describe('Joi Extensions', () => { | ||
test('An extended type with baseType set in metadata', () => { | ||
const schema = Joi.object({ | ||
doStuff: ExtendedJoi.objectId() | ||
}).meta({ className: 'Test' }); | ||
|
||
const result = convertSchema({ debug: true }, schema); | ||
expect(result).not.toBeUndefined; | ||
// prettier-ignore | ||
expect(result?.content).toBe( | ||
[ | ||
'export interface Test {', | ||
' doStuff?: string;', | ||
'}' | ||
].join('\n') | ||
); | ||
}); | ||
|
||
test('An extended type with baseType not set in metadata', () => { | ||
const schema = Joi.object({ | ||
doStuff: ExtendedJoi.dollars() | ||
}).meta({ className: 'Test' }); | ||
|
||
const result = convertSchema({ debug: true }, schema); | ||
expect(result).not.toBeUndefined; | ||
// prettier-ignore | ||
expect(result?.content).toBe( | ||
[ | ||
'export interface Test {', | ||
' doStuff?: unknown;', | ||
'}' | ||
].join('\n') | ||
); | ||
}); | ||
}); |
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
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