class-validator-jsonschema
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -5,3 +5,3 @@ import { SchemaObject } from 'openapi3-ts'; | ||
export declare type DecoratorSchema = SchemaObject | ((source: SchemaObject, options: IOptions) => SchemaObject); | ||
export declare function JSONSchema(schema: DecoratorSchema): (target: object, key?: string | undefined) => void; | ||
export declare function getMetadataSchema(target: object, key?: string): DecoratorSchema; | ||
export declare function JSONSchema(schema: DecoratorSchema): (target: object | Function, key?: string | undefined) => void; | ||
export declare function getMetadataSchema(target: object | Function, key: string): DecoratorSchema; |
@@ -7,3 +7,8 @@ "use strict"; | ||
return (target, key) => { | ||
setMetadataSchema(schema, target, key); | ||
if (key) { | ||
setMetadataSchema(schema, target.constructor, key); | ||
} | ||
else { | ||
setMetadataSchema(schema, target, target.name); | ||
} | ||
}; | ||
@@ -13,13 +18,8 @@ } | ||
function getMetadataSchema(target, key) { | ||
const schema = key | ||
? Reflect.getMetadata(SCHEMA_KEY, target.constructor, key) | ||
: Reflect.getMetadata(SCHEMA_KEY, target.constructor); | ||
return schema || {}; | ||
return Reflect.getMetadata(SCHEMA_KEY, target.constructor, key) || {}; | ||
} | ||
exports.getMetadataSchema = getMetadataSchema; | ||
function setMetadataSchema(value, target, key) { | ||
return key | ||
? Reflect.defineMetadata(SCHEMA_KEY, value, target.constructor, key) | ||
: Reflect.defineMetadata(SCHEMA_KEY, value, target.constructor); | ||
return Reflect.defineMetadata(SCHEMA_KEY, value, target, key); | ||
} | ||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiZGVjb3JhdG9ycy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3NyYy9kZWNvcmF0b3JzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7O0FBQ0EsNEJBQXlCO0FBSXpCLE1BQU0sVUFBVSxHQUFHLE1BQU0sQ0FBQyx1Q0FBdUMsQ0FBQyxDQUFBO0FBb0JsRSx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O0FBRUEsNEJBQXlCO0FBSXpCLE1BQU0sVUFBVSxHQUFHLE1BQU0sQ0FBQyx1Q0FBdUMsQ0FBQyxDQUFBO0FBb0JsRSxvQkFBMkIsTUFBdUI7SUFDaEQsTUFBTSxDQUFDLENBQUMsTUFBeUIsRUFBRSxHQUFZLEVBQUUsRUFBRTtRQUNqRCxFQUFFLENBQUMsQ0FBQyxHQUFHLENBQUMsQ0FBQyxDQUFDO1lBQ1IsaUJBQWlCLENBQUMsTUFBTSxFQUFFLE1BQU0sQ0FBQyxXQUFXLEVBQUUsR0FBRyxDQUFDLENBQUE7UUFDcEQsQ0FBQztRQUFDLElBQUksQ0FBQyxDQUFDO1lBQ04saUJBQWlCLENBQUMsTUFBTSxFQUFFLE1BQU0sRUFBRyxNQUFtQixDQUFDLElBQUksQ0FBQyxDQUFBO1FBQzlELENBQUM7SUFDSCxDQUFDLENBQUE7QUFDSCxDQUFDO0FBUkQsZ0NBUUM7QUFLRCwyQkFDRSxNQUF5QixFQUN6QixHQUFXO0lBRVgsTUFBTSxDQUFDLE9BQU8sQ0FBQyxXQUFXLENBQUMsVUFBVSxFQUFFLE1BQU0sQ0FBQyxXQUFXLEVBQUUsR0FBRyxDQUFDLElBQUksRUFBRSxDQUFBO0FBQ3ZFLENBQUM7QUFMRCw4Q0FLQztBQUtELDJCQUNFLEtBQXNCLEVBQ3RCLE1BQXlCLEVBQ3pCLEdBQVc7SUFFWCxNQUFNLENBQUMsT0FBTyxDQUFDLGNBQWMsQ0FBQyxVQUFVLEVBQUUsS0FBSyxFQUFFLE1BQU0sRUFBRSxHQUFHLENBQUMsQ0FBQTtBQUMvRCxDQUFDIn0= |
@@ -15,5 +15,6 @@ "use strict"; | ||
.groupBy('target.name') | ||
.mapValues(schemaMetas => { | ||
const target = schemaMetas[0].target; | ||
const properties = _(schemaMetas) | ||
.mapValues(ownMetas => { | ||
const target = ownMetas[0].target; | ||
const metas = ownMetas.concat(getInheritedMetadatas(target, metadatas)); | ||
const properties = _(metas) | ||
.groupBy('propertyName') | ||
@@ -27,6 +28,6 @@ .mapValues((propMetas, propKey) => { | ||
properties, | ||
required: getRequiredPropNames(schemaMetas, options), | ||
required: getRequiredPropNames(target, metas, options), | ||
type: 'object' | ||
}; | ||
return applyDecorators(definitionSchema, target.constructor, options); | ||
return applyDecorators(definitionSchema, target, options, target.name); | ||
}) | ||
@@ -37,2 +38,11 @@ .value(); | ||
exports.validationMetadatasToSchemas = validationMetadatasToSchemas; | ||
function getInheritedMetadatas(target, metadatas) { | ||
return metadatas.filter(d => d.target instanceof Function && | ||
target.prototype instanceof d.target && | ||
!_.find(metadatas, { | ||
propertyName: d.propertyName, | ||
target, | ||
type: d.type | ||
})); | ||
} | ||
function applyConverters(propertyMetadatas, options) { | ||
@@ -57,13 +67,16 @@ const converters = Object.assign({}, defaultConverters_1.defaultConverters, options.additionalConverters); | ||
} | ||
function getRequiredPropNames(metadatas, options) { | ||
const optionalValidators = [ | ||
class_validator_1.ValidationTypes.CONDITIONAL_VALIDATION, | ||
class_validator_1.ValidationTypes.IS_EMPTY | ||
]; | ||
function getRequiredPropNames(target, metadatas, options) { | ||
function isDefined(metas) { | ||
return _.some(metas, { type: class_validator_1.ValidationTypes.IS_DEFINED }); | ||
} | ||
function isOptional(metas) { | ||
return _.some(metas, ({ type }) => _.includes([class_validator_1.ValidationTypes.CONDITIONAL_VALIDATION, class_validator_1.ValidationTypes.IS_EMPTY], type)); | ||
} | ||
return _(metadatas) | ||
.groupBy('propertyName') | ||
.pickBy(d => { | ||
.pickBy(metas => { | ||
const [own, inherited] = _.partition(metas, d => d.target === target); | ||
return options.skipMissingProperties | ||
? _.some(d, { type: class_validator_1.ValidationTypes.IS_DEFINED }) | ||
: !_.some(d, ({ type }) => _.includes(optionalValidators, type)); | ||
? isDefined(own) || (!isOptional(own) && isDefined(inherited)) | ||
: !(isOptional(own) || isOptional(inherited)); | ||
}) | ||
@@ -73,2 +86,2 @@ .keys() | ||
} | ||
//# sourceMappingURL=data:application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csRUFBRSxTQUFTLENBQUMsR0FBRyxDQUFDLENBQUMsU0FBUyxDQUFDLEtBQUssRUFBRSxDQUFDLENBQUMsRUFBRSxDQUFDLENBQUMsQ0FBQyxNQUFNLEtBQUssTUFBTSxDQUFDLENBQUE7UUFDckUsTUFBTSxDQUFDLE9BQU8sQ0FBQyxxQkFBcUI7WUFDbEMsQ0FBQyxDQUFDLFNBQVMsQ0FBQyxHQUFHLENBQUMsSUFBSSxDQUFDLENBQUMsVUFBVSxDQUFDLEdBQUcsQ0FBQyxJQUFJLFNBQVMsQ0FBQyxTQUFTLENBQUMsQ0FBQztZQUM5RCxDQUFDLENBQUMsQ0FBQyxDQUFDLFVBQVUsQ0FBQyxHQUFHLENBQUMsSUFBSSxVQUFVLENBQUMsU0FBUyxDQUFDLENBQUMsQ0FBQTtJQUNqRCxDQUFDLENBQUM7U0FDRCxJQUFJLEVBQUU7U0FDTixLQUFLLEVBQUUsQ0FBQTtBQUNaLENBQUMifQ== |
@@ -9,2 +9,6 @@ # Changelog | ||
## [1.1.2] - 2018-08-19 | ||
### Fixed | ||
- Add missing handling for [inherited validation decorators](https://github.com/typestack/class-validator#inheriting-validation-decorators) [#3](https://github.com/epiphone/class-validator-jsonschema/pull/3) | ||
## [1.1.1] - 2018-03-14 | ||
@@ -11,0 +15,0 @@ ### Fixed |
{ | ||
"name": "class-validator-jsonschema", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Convert class-validator-decorated classes into JSON schema", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -184,2 +184,6 @@ # class-validator-jsonschema | ||
`JSONSchema` decorators also flow down from parent classes into [inherited validation decorators](https://github.com/typestack/class-validator#inheriting-validation-decorators). Note though that if the inherited class uses `JSONSchema` to redecorate a property from the parent class, the parent class `JSONSchema` gets overwritten - i.e. there's no merging logic. | ||
#### Custom handlers | ||
Alternatively `JSONSchema` can take a function of type `(existingSchema: SchemaObject, options: IOptions) => SchemaObject`. The return value of this function is then **not** automatically merged into existing schema (i.e. the one derived from `class-validator` decorators). Instead you can handle merging yourself in whichever way is preferred, the idea being that removal of existing keywords and other more complex overwrite scenarios can be implemented here. | ||
@@ -186,0 +190,0 @@ |
@@ -0,1 +1,2 @@ | ||
// tslint:disable:ban-types | ||
import { SchemaObject } from 'openapi3-ts' | ||
@@ -27,4 +28,8 @@ import 'reflect-metadata' | ||
export function JSONSchema(schema: DecoratorSchema) { | ||
return (target: object, key?: string) => { | ||
setMetadataSchema(schema, target, key) | ||
return (target: object | Function, key?: string) => { | ||
if (key) { | ||
setMetadataSchema(schema, target.constructor, key) // Property metadata | ||
} else { | ||
setMetadataSchema(schema, target, (target as Function).name) // Class metadata | ||
} | ||
} | ||
@@ -37,9 +42,6 @@ } | ||
export function getMetadataSchema( | ||
target: object, | ||
key?: string | ||
target: object | Function, | ||
key: string | ||
): DecoratorSchema { | ||
const schema = key | ||
? Reflect.getMetadata(SCHEMA_KEY, target.constructor, key) | ||
: Reflect.getMetadata(SCHEMA_KEY, target.constructor) | ||
return schema || {} | ||
return Reflect.getMetadata(SCHEMA_KEY, target.constructor, key) || {} | ||
} | ||
@@ -52,8 +54,6 @@ | ||
value: DecoratorSchema, | ||
target: object, | ||
key?: string | ||
target: object | Function, | ||
key: string | ||
) { | ||
return key | ||
? Reflect.defineMetadata(SCHEMA_KEY, value, target.constructor, key) | ||
: Reflect.defineMetadata(SCHEMA_KEY, value, target.constructor) | ||
return Reflect.defineMetadata(SCHEMA_KEY, value, target, key) | ||
} |
@@ -29,5 +29,7 @@ // tslint:disable:no-submodule-imports ban-types | ||
.groupBy('target.name') | ||
.mapValues(schemaMetas => { | ||
const target = schemaMetas[0].target as Function | ||
const properties = _(schemaMetas) | ||
.mapValues(ownMetas => { | ||
const target = ownMetas[0].target as Function | ||
const metas = ownMetas.concat(getInheritedMetadatas(target, metadatas)) | ||
const properties = _(metas) | ||
.groupBy('propertyName') | ||
@@ -42,7 +44,7 @@ .mapValues((propMetas, propKey) => { | ||
properties, | ||
required: getRequiredPropNames(schemaMetas, options), | ||
required: getRequiredPropNames(target, metas, options), | ||
type: 'object' | ||
} | ||
return applyDecorators(definitionSchema, target.constructor, options) | ||
return applyDecorators(definitionSchema, target, options, target.name) | ||
}) | ||
@@ -55,2 +57,27 @@ .value() | ||
/** | ||
* Return target class' inherited validation metadatas, with original metadatas | ||
* given precedence over inherited ones in case of duplicates. | ||
* | ||
* Adapted from `class-validator` source. | ||
* | ||
* @param target Target child class. | ||
* @param metadatas All class-validator metadata objects. | ||
*/ | ||
function getInheritedMetadatas( | ||
target: Function, | ||
metadatas: ValidationMetadata[] | ||
) { | ||
return metadatas.filter( | ||
d => | ||
d.target instanceof Function && | ||
target.prototype instanceof d.target && | ||
!_.find(metadatas, { | ||
propertyName: d.propertyName, | ||
target, | ||
type: d.type | ||
}) | ||
) | ||
} | ||
/** | ||
* Convert a property's class-validator metadata into a JSON Schema property. | ||
@@ -86,3 +113,3 @@ */ | ||
options: IOptions, | ||
propertyName?: string | ||
propertyName: string | ||
): SchemaObject { | ||
@@ -97,18 +124,30 @@ const additionalSchema = getMetadataSchema(target.prototype, propertyName) | ||
* Get the required property names of a validated class. | ||
* @param target Validation target class. | ||
* @param metadatas Validation metadata objects of the validated class. | ||
* @param options Global class-validator options. | ||
*/ | ||
function getRequiredPropNames( | ||
target: Function, | ||
metadatas: ValidationMetadata[], | ||
options: IOptions | ||
) { | ||
const optionalValidators = [ | ||
ValidationTypes.CONDITIONAL_VALIDATION, | ||
ValidationTypes.IS_EMPTY | ||
] | ||
function isDefined(metas: ValidationMetadata[]) { | ||
return _.some(metas, { type: ValidationTypes.IS_DEFINED }) | ||
} | ||
function isOptional(metas: ValidationMetadata[]) { | ||
return _.some(metas, ({ type }) => | ||
_.includes( | ||
[ValidationTypes.CONDITIONAL_VALIDATION, ValidationTypes.IS_EMPTY], | ||
type | ||
) | ||
) | ||
} | ||
return _(metadatas) | ||
.groupBy('propertyName') | ||
.pickBy(d => { | ||
.pickBy(metas => { | ||
const [own, inherited] = _.partition(metas, d => d.target === target) | ||
return options.skipMissingProperties | ||
? _.some(d, { type: ValidationTypes.IS_DEFINED }) | ||
: !_.some(d, ({ type }) => _.includes(optionalValidators, type)) | ||
? isDefined(own) || (!isOptional(own) && isDefined(inherited)) | ||
: !(isOptional(own) || isOptional(inherited)) | ||
}) | ||
@@ -115,0 +154,0 @@ .keys() |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
189321
28
1939
216