-
-
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 #142 from kjb085/master
Add support for Joi.any().cast() to string and number types
- Loading branch information
Showing
6 changed files
with
77 additions
and
1 deletion.
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,45 @@ | ||
import { existsSync, readFileSync, rmdirSync } from 'fs'; | ||
|
||
import { convertFromDirectory } from '../../index'; | ||
|
||
describe('Cast primitive types', () => { | ||
const typeOutputDirectory = './src/__tests__/cast/interfaces'; | ||
|
||
beforeAll(() => { | ||
if (existsSync(typeOutputDirectory)) { | ||
rmdirSync(typeOutputDirectory, { recursive: true }); | ||
} | ||
}); | ||
|
||
test('casts all variations from file', async () => { | ||
const result = await convertFromDirectory({ | ||
schemaDirectory: './src/__tests__/cast/schemas', | ||
typeOutputDirectory | ||
}); | ||
|
||
expect(result).toBe(true); | ||
|
||
const oneContent = readFileSync(`${typeOutputDirectory}/One.ts`).toString(); | ||
|
||
expect(oneContent).toBe( | ||
`/** | ||
* This file was automatically generated by joi-to-typescript | ||
* Do not modify this file manually | ||
*/ | ||
export type NumberType = number; | ||
export interface Numbers { | ||
bool: number; | ||
day: number; | ||
} | ||
export type StringType = string; | ||
export interface Strings { | ||
num: string; | ||
} | ||
` | ||
); | ||
}); | ||
}); |
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,14 @@ | ||
import Joi from 'joi'; | ||
|
||
export const NumberSchema = Joi.object({ | ||
bool: Joi.boolean().cast('number').required(), | ||
day: Joi.date().cast('number').required(), | ||
}).meta({ className: 'Numbers' }); | ||
|
||
export const StringSchema = Joi.object({ | ||
num: Joi.number().cast('string').required() | ||
}).meta({ className: 'Strings' }) | ||
|
||
export const StringType = Joi.number().cast('string') | ||
|
||
export const NumberType = Joi.boolean().cast('number') |
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