forked from epiphone/class-validator-jsonschema
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix empty child classes issue (epiphone#106)
- Loading branch information
1 parent
4821a25
commit d3c2ca3
Showing
2 changed files
with
68 additions
and
3 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 |
---|---|---|
|
@@ -13,7 +13,11 @@ import { | |
} from 'class-validator' | ||
import _get from 'lodash.get' | ||
|
||
import { JSONSchema, validationMetadatasToSchemas } from '../src' | ||
import { | ||
JSONSchema, | ||
targetConstructorToSchema, | ||
validationMetadatasToSchemas, | ||
} from '../src' | ||
|
||
@JSONSchema({ | ||
description: 'Contains email, password and phone', | ||
|
@@ -43,7 +47,6 @@ class BaseContent { | |
@JSONSchema({ | ||
title: 'User object', | ||
}) | ||
// @ts-ignore: not referenced | ||
class User extends BaseContent { | ||
@MinLength(10) | ||
@MaxLength(20) | ||
|
@@ -65,6 +68,9 @@ class User extends BaseContent { | |
@Contains('hello') welcome: string | ||
} | ||
|
||
// @ts-ignore: not referenced | ||
class Admin extends User {} | ||
|
||
const metadatas = _get(getFromContainer(MetadataStorage), 'validationMetadatas') | ||
|
||
describe('Inheriting validation decorators', () => { | ||
|
@@ -140,4 +146,43 @@ describe('Inheriting validation decorators', () => { | |
expect(schemas.BaseContent.required).toEqual(['email', 'phone']) | ||
expect(schemas.User.required).toEqual(['password', 'email']) | ||
}) | ||
|
||
it('inherits and merges validation decorators from multiple parent classes and empty child class', () => { | ||
const schema = targetConstructorToSchema(Admin) | ||
|
||
expect(schema).toEqual({ | ||
properties: { | ||
email: { | ||
default: '[email protected]', | ||
format: 'email', | ||
type: 'string', | ||
not: { type: 'null' }, | ||
}, | ||
name: { | ||
maxLength: 20, | ||
minLength: 10, | ||
type: 'string', | ||
}, | ||
password: { | ||
description: 'Password field - required!', | ||
minLength: 20, | ||
type: 'string', | ||
not: { type: 'null' }, | ||
}, | ||
phone: { | ||
format: 'mobile-phone', | ||
title: 'Mobile phone number', | ||
type: 'string', | ||
not: { type: 'null' }, | ||
}, | ||
welcome: { | ||
pattern: 'hello', | ||
type: 'string', | ||
}, | ||
}, | ||
required: ['name', 'welcome', 'email'], | ||
title: 'User object', | ||
type: 'object', | ||
}) | ||
}) | ||
}) |
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