-
-
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 #356 from danilvalov/master
Add support for simple object pattern in schema
- Loading branch information
Showing
5 changed files
with
100 additions
and
47 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,74 @@ | ||
import Joi from 'joi'; | ||
|
||
import { convertSchema } from '../index'; | ||
|
||
describe('test `Joi.object().pattern()`', () => { | ||
test('`pattern(Joi.string(), Joi.AnySchema())`', () => { | ||
const schema = Joi.object() | ||
.pattern(Joi.string(), Joi.array().items(Joi.object({ | ||
id: Joi.string().required(), | ||
propertyName1: Joi.boolean().required() | ||
}))) | ||
.description('a test pattern schema definition'); | ||
|
||
const result = convertSchema({}, schema, 'TestSchema'); | ||
expect(result).not.toBeUndefined; | ||
expect(result?.content).toBe(`/** | ||
* a test pattern schema definition | ||
*/ | ||
export interface TestSchema { | ||
[pattern: string]: { | ||
id: string; | ||
propertyName1: boolean; | ||
}[]; | ||
}`); | ||
}); | ||
|
||
test('`pattern(Joi.string(), Joi.number())`', () => { | ||
const schema = Joi.object() | ||
.description('a test deep pattern schema definition') | ||
.pattern(Joi.string(), Joi.number().description('Number Property')); | ||
|
||
const result = convertSchema({}, schema, 'TestSchema'); | ||
expect(result).not.toBeUndefined; | ||
expect(result?.content).toBe(`/** | ||
* a test deep pattern schema definition | ||
*/ | ||
export interface TestSchema { | ||
/** | ||
* Number Property | ||
*/ | ||
[pattern: string]: number; | ||
}`); | ||
}); | ||
|
||
test('`pattern(/^test$/, Joi.AnySchema())`', () => { | ||
const schema = Joi.object({ | ||
name: Joi.string(), | ||
}) | ||
.description('a test regex pattern schema definition') | ||
.pattern(Joi.string(), { | ||
name: Joi.string().optional(), | ||
propertyName1: Joi.object().pattern(/^test$/, Joi.object({ | ||
propertyName2: Joi.boolean() | ||
})).required() | ||
}); | ||
|
||
const result = convertSchema({ sortPropertiesByName: false }, schema, 'TestSchema'); | ||
expect(result).not.toBeUndefined; | ||
expect(result?.content).toBe(`/** | ||
* a test regex pattern schema definition | ||
*/ | ||
export interface TestSchema { | ||
name?: string; | ||
[pattern: string]: { | ||
name?: string; | ||
propertyName1: { | ||
[pattern: string]: { | ||
propertyName2?: boolean; | ||
}; | ||
}; | ||
}; | ||
}`); | ||
}); | ||
}); |
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