@nest-omni/core 1.0.57 → 2.0.1-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.history/src/common/boilerplate.polyfill_20250521135408.ts +202 -0
- package/.history/src/common/boilerplate.polyfill_20250712165919.ts +254 -0
- package/.history/src/common/boilerplate.polyfill_20250712165924.ts +254 -0
- package/.history/src/common/boilerplate.polyfill_20250712165927.ts +254 -0
- package/.history/src/common/boilerplate.polyfill_20250712170044.ts +265 -0
- package/.history/src/common/boilerplate.polyfill_20250712170049.ts +265 -0
- package/.history/src/common/boilerplate.polyfill_20250712170053.ts +265 -0
- package/.history/src/common/boilerplate.polyfill_20250712170610.ts +280 -0
- package/.history/src/common/boilerplate.polyfill_20250712170614.ts +280 -0
- package/.history/src/common/boilerplate.polyfill_20250712170739.ts +265 -0
- package/.history/src/common/boilerplate.polyfill_20250712170801.ts +291 -0
- package/.history/src/common/boilerplate.polyfill_20250712170809.ts +317 -0
- package/.history/src/common/boilerplate.polyfill_20250712170816.ts +319 -0
- package/.history/src/common/boilerplate.polyfill_20250712170820.ts +318 -0
- package/.history/src/common/boilerplate.polyfill_20250712170824.ts +318 -0
- package/.history/src/common/boilerplate.polyfill_20250712171040.ts +289 -0
- package/README.md +16 -0
- package/build.sh +45 -0
- package/{common → dist/common}/abstract.entity.d.ts +1 -0
- package/{common → dist/common}/abstract.entity.js +13 -0
- package/{decorators → dist/decorators}/field.decorators.d.ts +1 -1
- package/dist/package.json +71 -0
- package/dist/setup/bootstrap.setup.d.ts +2 -0
- package/{setup → dist/setup}/bootstrap.setup.js +45 -27
- package/{shared → dist/shared}/serviceRegistryModule.js +4 -11
- package/{shared → dist/shared}/services/api-config.service.d.ts +3 -15
- package/{shared → dist/shared}/services/api-config.service.js +15 -42
- package/{shared → dist/shared}/services/index.d.ts +0 -1
- package/{shared → dist/shared}/services/index.js +0 -1
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/yarn.lock +644 -0
- package/package.json +1 -3
- package/src/.hygen.js +116 -0
- package/src/common/abstract-client.service.ts +56 -0
- package/src/common/abstract.entity.ts +82 -0
- package/src/common/boilerplate.polyfill.ts +202 -0
- package/src/common/dto/abstract.dto.ts +61 -0
- package/src/common/dto/bulk-id.dto.ts +13 -0
- package/src/common/dto/create-translation.dto.ts +10 -0
- package/src/common/dto/index.ts +6 -0
- package/src/common/dto/page-meta.dto.ts +37 -0
- package/src/common/dto/page-options.dto.ts +35 -0
- package/src/common/dto/page.dto.ts +36 -0
- package/src/common/index.ts +7 -0
- package/src/common/snake-naming.strategy.ts +64 -0
- package/src/common/types.ts +44 -0
- package/src/common/utils.ts +45 -0
- package/src/constants/index.ts +2 -0
- package/src/constants/language-code.ts +7 -0
- package/src/constants/order.ts +4 -0
- package/src/decorators/api-page-ok-response.decorator.ts +32 -0
- package/src/decorators/auth-user.decorator.ts +16 -0
- package/src/decorators/controller.decorator.ts +8 -0
- package/src/decorators/field.decorators.ts +856 -0
- package/src/decorators/http.decorators.ts +10 -0
- package/src/decorators/index.ts +12 -0
- package/src/decorators/operation.decorator.ts +4 -0
- package/src/decorators/property.decorators.ts +62 -0
- package/src/decorators/public-route.decorator.ts +8 -0
- package/src/decorators/swagger.schema.ts +117 -0
- package/src/decorators/timestamp-column.decorator.ts +29 -0
- package/src/decorators/transform.decorators.ts +159 -0
- package/src/decorators/translate.decorator.ts +19 -0
- package/src/decorators/use-dto.decorator.ts +11 -0
- package/src/decorators/user-check.decorator.ts +30 -0
- package/src/decorators/user.decorator.ts +59 -0
- package/src/decorators/user_auth.decorator.ts +15 -0
- package/src/decorators/validator.decorators.ts +98 -0
- package/src/exceptions/file-not-image.exception.ts +7 -0
- package/src/exceptions/index.ts +2 -0
- package/src/exceptions/page-type.exception.ts +7 -0
- package/src/exceptions/user-not-found.exception.ts +7 -0
- package/src/filters/bad-request.filter.ts +77 -0
- package/src/filters/constraint-errors.ts +5 -0
- package/src/filters/index.ts +3 -0
- package/src/filters/query-failed.filter.ts +32 -0
- package/src/health-checker/health-checker.controller.ts +51 -0
- package/src/health-checker/health-checker.module.ts +13 -0
- package/src/health-checker/health-indicators/service.indicator.ts +44 -0
- package/src/helpers/common.helper.ts +49 -0
- package/src/helpers/date.helper.ts +64 -0
- package/src/helpers/index.ts +2 -0
- package/src/i18n/en_US/validation.json +41 -0
- package/src/i18n/zh_CN/validation.json +41 -0
- package/src/index.ts +11 -0
- package/src/interceptors/index.ts +2 -0
- package/src/interceptors/language-interceptor.service.ts +28 -0
- package/src/interceptors/translation-interceptor.service.ts +43 -0
- package/src/interfaces/IApiFile.ts +4 -0
- package/src/interfaces/IFile.ts +8 -0
- package/src/interfaces/ITranslationDecoratorInterface.ts +3 -0
- package/src/interfaces/index.ts +3 -0
- package/src/interfaces/response.ts +37 -0
- package/src/middlewares/index.ts +4 -0
- package/src/middlewares/omni-auth.middleware.ts +10 -0
- package/src/middlewares/powerby.middleware.ts +6 -0
- package/src/middlewares/request-id.middleware.ts +28 -0
- package/src/middlewares/xml.middleware.ts +14 -0
- package/src/providers/context.provider.ts +109 -0
- package/src/providers/generator.provider.ts +66 -0
- package/src/providers/index.ts +2 -0
- package/src/setup/bootstrap.setup.ts +254 -0
- package/src/setup/index.ts +1 -0
- package/src/shared/index.ts +2 -0
- package/src/shared/serviceRegistryModule.ts +135 -0
- package/src/shared/services/api-config.service.ts +336 -0
- package/src/shared/services/generator.service.ts +13 -0
- package/src/shared/services/index.ts +4 -0
- package/src/shared/services/translation.service.ts +59 -0
- package/src/shared/services/validator.service.ts +10 -0
- package/src/validator-json/decorators.ts +63 -0
- package/src/validator-json/default.ts +257 -0
- package/src/validator-json/defaultConverters.ts +402 -0
- package/src/validator-json/index.ts +4 -0
- package/src/validator-json/options.ts +50 -0
- package/src/validators/file-mimetype.validator.ts +34 -0
- package/src/validators/index.ts +6 -0
- package/src/validators/is-exists.validator.ts +60 -0
- package/src/validators/is-unique.validator.ts +93 -0
- package/src/validators/phone-country-code.validator.ts +36 -0
- package/src/validators/same-as.validator.ts +27 -0
- package/src/validators/skip-empty.validator.ts +25 -0
- package/tsconfig.json +31 -0
- package/tsconfig.lib.json +16 -0
- package/setup/bootstrap.setup.d.ts +0 -2
- package/shared/services/aws-s3.service.d.ts +0 -10
- package/shared/services/aws-s3.service.js +0 -56
- package/tsconfig.tsbuildinfo +0 -1
- /package/{.hygen → dist/.hygen}/new/crud/controller.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/crud/create.dto.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/crud/entity.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/crud/get.dto.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/crud/index-dto.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/crud/index-response.dto.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/crud/module.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/crud/prompt.js +0 -0
- /package/{.hygen → dist/.hygen}/new/crud/service.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/crud/update.dto.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/controller.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/create-command.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/create.dto.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/dto.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/entity.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/get-query.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/module.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/not-found.exception.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/page-options.dto.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/prompt.js +0 -0
- /package/{.hygen → dist/.hygen}/new/module/repository.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/service.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/translation-dto.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/translation-entity.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/translation-repository.ejs.t +0 -0
- /package/{.hygen → dist/.hygen}/new/module/update.dto.ejs.t +0 -0
- /package/{.hygen.js → dist/.hygen.js} +0 -0
- /package/{common → dist/common}/abstract-client.service.d.ts +0 -0
- /package/{common → dist/common}/abstract-client.service.js +0 -0
- /package/{common → dist/common}/boilerplate.polyfill.d.ts +0 -0
- /package/{common → dist/common}/boilerplate.polyfill.js +0 -0
- /package/{common → dist/common}/dto/abstract.dto.d.ts +0 -0
- /package/{common → dist/common}/dto/abstract.dto.js +0 -0
- /package/{common → dist/common}/dto/bulk-id.dto.d.ts +0 -0
- /package/{common → dist/common}/dto/bulk-id.dto.js +0 -0
- /package/{common → dist/common}/dto/create-translation.dto.d.ts +0 -0
- /package/{common → dist/common}/dto/create-translation.dto.js +0 -0
- /package/{common → dist/common}/dto/index.d.ts +0 -0
- /package/{common → dist/common}/dto/index.js +0 -0
- /package/{common → dist/common}/dto/page-meta.dto.d.ts +0 -0
- /package/{common → dist/common}/dto/page-meta.dto.js +0 -0
- /package/{common → dist/common}/dto/page-options.dto.d.ts +0 -0
- /package/{common → dist/common}/dto/page-options.dto.js +0 -0
- /package/{common → dist/common}/dto/page.dto.d.ts +0 -0
- /package/{common → dist/common}/dto/page.dto.js +0 -0
- /package/{common → dist/common}/index.d.ts +0 -0
- /package/{common → dist/common}/index.js +0 -0
- /package/{common → dist/common}/snake-naming.strategy.d.ts +0 -0
- /package/{common → dist/common}/snake-naming.strategy.js +0 -0
- /package/{common → dist/common}/types.d.ts +0 -0
- /package/{common → dist/common}/types.js +0 -0
- /package/{common → dist/common}/utils.d.ts +0 -0
- /package/{common → dist/common}/utils.js +0 -0
- /package/{constants → dist/constants}/index.d.ts +0 -0
- /package/{constants → dist/constants}/index.js +0 -0
- /package/{constants → dist/constants}/language-code.d.ts +0 -0
- /package/{constants → dist/constants}/language-code.js +0 -0
- /package/{constants → dist/constants}/order.d.ts +0 -0
- /package/{constants → dist/constants}/order.js +0 -0
- /package/{decorators → dist/decorators}/api-page-ok-response.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/api-page-ok-response.decorator.js +0 -0
- /package/{decorators → dist/decorators}/auth-user.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/auth-user.decorator.js +0 -0
- /package/{decorators → dist/decorators}/controller.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/controller.decorator.js +0 -0
- /package/{decorators → dist/decorators}/field.decorators.js +0 -0
- /package/{decorators → dist/decorators}/http.decorators.d.ts +0 -0
- /package/{decorators → dist/decorators}/http.decorators.js +0 -0
- /package/{decorators → dist/decorators}/index.d.ts +0 -0
- /package/{decorators → dist/decorators}/index.js +0 -0
- /package/{decorators → dist/decorators}/operation.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/operation.decorator.js +0 -0
- /package/{decorators → dist/decorators}/property.decorators.d.ts +0 -0
- /package/{decorators → dist/decorators}/property.decorators.js +0 -0
- /package/{decorators → dist/decorators}/public-route.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/public-route.decorator.js +0 -0
- /package/{decorators → dist/decorators}/swagger.schema.d.ts +0 -0
- /package/{decorators → dist/decorators}/swagger.schema.js +0 -0
- /package/{decorators → dist/decorators}/timestamp-column.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/timestamp-column.decorator.js +0 -0
- /package/{decorators → dist/decorators}/transform.decorators.d.ts +0 -0
- /package/{decorators → dist/decorators}/transform.decorators.js +0 -0
- /package/{decorators → dist/decorators}/translate.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/translate.decorator.js +0 -0
- /package/{decorators → dist/decorators}/use-dto.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/use-dto.decorator.js +0 -0
- /package/{decorators → dist/decorators}/user-check.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/user-check.decorator.js +0 -0
- /package/{decorators → dist/decorators}/user.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/user.decorator.js +0 -0
- /package/{decorators → dist/decorators}/user_auth.decorator.d.ts +0 -0
- /package/{decorators → dist/decorators}/user_auth.decorator.js +0 -0
- /package/{decorators → dist/decorators}/validator.decorators.d.ts +0 -0
- /package/{decorators → dist/decorators}/validator.decorators.js +0 -0
- /package/{exceptions → dist/exceptions}/file-not-image.exception.d.ts +0 -0
- /package/{exceptions → dist/exceptions}/file-not-image.exception.js +0 -0
- /package/{exceptions → dist/exceptions}/index.d.ts +0 -0
- /package/{exceptions → dist/exceptions}/index.js +0 -0
- /package/{exceptions → dist/exceptions}/page-type.exception.d.ts +0 -0
- /package/{exceptions → dist/exceptions}/page-type.exception.js +0 -0
- /package/{exceptions → dist/exceptions}/user-not-found.exception.d.ts +0 -0
- /package/{exceptions → dist/exceptions}/user-not-found.exception.js +0 -0
- /package/{filters → dist/filters}/bad-request.filter.d.ts +0 -0
- /package/{filters → dist/filters}/bad-request.filter.js +0 -0
- /package/{filters → dist/filters}/constraint-errors.d.ts +0 -0
- /package/{filters → dist/filters}/constraint-errors.js +0 -0
- /package/{filters → dist/filters}/index.d.ts +0 -0
- /package/{filters → dist/filters}/index.js +0 -0
- /package/{filters → dist/filters}/query-failed.filter.d.ts +0 -0
- /package/{filters → dist/filters}/query-failed.filter.js +0 -0
- /package/{health-checker → dist/health-checker}/health-checker.controller.d.ts +0 -0
- /package/{health-checker → dist/health-checker}/health-checker.controller.js +0 -0
- /package/{health-checker → dist/health-checker}/health-checker.module.d.ts +0 -0
- /package/{health-checker → dist/health-checker}/health-checker.module.js +0 -0
- /package/{health-checker → dist/health-checker}/health-indicators/service.indicator.d.ts +0 -0
- /package/{health-checker → dist/health-checker}/health-indicators/service.indicator.js +0 -0
- /package/{helpers → dist/helpers}/common.helper.d.ts +0 -0
- /package/{helpers → dist/helpers}/common.helper.js +0 -0
- /package/{helpers → dist/helpers}/date.helper.d.ts +0 -0
- /package/{helpers → dist/helpers}/date.helper.js +0 -0
- /package/{helpers → dist/helpers}/index.d.ts +0 -0
- /package/{helpers → dist/helpers}/index.js +0 -0
- /package/{i18n → dist/i18n}/en_US/validation.json +0 -0
- /package/{i18n → dist/i18n}/zh_CN/validation.json +0 -0
- /package/{index.d.ts → dist/index.d.ts} +0 -0
- /package/{index.js → dist/index.js} +0 -0
- /package/{interceptors → dist/interceptors}/index.d.ts +0 -0
- /package/{interceptors → dist/interceptors}/index.js +0 -0
- /package/{interceptors → dist/interceptors}/language-interceptor.service.d.ts +0 -0
- /package/{interceptors → dist/interceptors}/language-interceptor.service.js +0 -0
- /package/{interceptors → dist/interceptors}/translation-interceptor.service.d.ts +0 -0
- /package/{interceptors → dist/interceptors}/translation-interceptor.service.js +0 -0
- /package/{interfaces → dist/interfaces}/IApiFile.d.ts +0 -0
- /package/{interfaces → dist/interfaces}/IApiFile.js +0 -0
- /package/{interfaces → dist/interfaces}/IFile.d.ts +0 -0
- /package/{interfaces → dist/interfaces}/IFile.js +0 -0
- /package/{interfaces → dist/interfaces}/ITranslationDecoratorInterface.d.ts +0 -0
- /package/{interfaces → dist/interfaces}/ITranslationDecoratorInterface.js +0 -0
- /package/{interfaces → dist/interfaces}/index.d.ts +0 -0
- /package/{interfaces → dist/interfaces}/index.js +0 -0
- /package/{interfaces → dist/interfaces}/response.d.ts +0 -0
- /package/{interfaces → dist/interfaces}/response.js +0 -0
- /package/{middlewares → dist/middlewares}/index.d.ts +0 -0
- /package/{middlewares → dist/middlewares}/index.js +0 -0
- /package/{middlewares → dist/middlewares}/omni-auth.middleware.d.ts +0 -0
- /package/{middlewares → dist/middlewares}/omni-auth.middleware.js +0 -0
- /package/{middlewares → dist/middlewares}/powerby.middleware.d.ts +0 -0
- /package/{middlewares → dist/middlewares}/powerby.middleware.js +0 -0
- /package/{middlewares → dist/middlewares}/request-id.middleware.d.ts +0 -0
- /package/{middlewares → dist/middlewares}/request-id.middleware.js +0 -0
- /package/{middlewares → dist/middlewares}/xml.middleware.d.ts +0 -0
- /package/{middlewares → dist/middlewares}/xml.middleware.js +0 -0
- /package/{providers → dist/providers}/context.provider.d.ts +0 -0
- /package/{providers → dist/providers}/context.provider.js +0 -0
- /package/{providers → dist/providers}/generator.provider.d.ts +0 -0
- /package/{providers → dist/providers}/generator.provider.js +0 -0
- /package/{providers → dist/providers}/index.d.ts +0 -0
- /package/{providers → dist/providers}/index.js +0 -0
- /package/{setup → dist/setup}/index.d.ts +0 -0
- /package/{setup → dist/setup}/index.js +0 -0
- /package/{shared → dist/shared}/index.d.ts +0 -0
- /package/{shared → dist/shared}/index.js +0 -0
- /package/{shared → dist/shared}/serviceRegistryModule.d.ts +0 -0
- /package/{shared → dist/shared}/services/generator.service.d.ts +0 -0
- /package/{shared → dist/shared}/services/generator.service.js +0 -0
- /package/{shared → dist/shared}/services/translation.service.d.ts +0 -0
- /package/{shared → dist/shared}/services/translation.service.js +0 -0
- /package/{shared → dist/shared}/services/validator.service.d.ts +0 -0
- /package/{shared → dist/shared}/services/validator.service.js +0 -0
- /package/{validator-json → dist/validator-json}/decorators.d.ts +0 -0
- /package/{validator-json → dist/validator-json}/decorators.js +0 -0
- /package/{validator-json → dist/validator-json}/default.d.ts +0 -0
- /package/{validator-json → dist/validator-json}/default.js +0 -0
- /package/{validator-json → dist/validator-json}/defaultConverters.d.ts +0 -0
- /package/{validator-json → dist/validator-json}/defaultConverters.js +0 -0
- /package/{validator-json → dist/validator-json}/index.d.ts +0 -0
- /package/{validator-json → dist/validator-json}/index.js +0 -0
- /package/{validator-json → dist/validator-json}/options.d.ts +0 -0
- /package/{validator-json → dist/validator-json}/options.js +0 -0
- /package/{validators → dist/validators}/file-mimetype.validator.d.ts +0 -0
- /package/{validators → dist/validators}/file-mimetype.validator.js +0 -0
- /package/{validators → dist/validators}/index.d.ts +0 -0
- /package/{validators → dist/validators}/index.js +0 -0
- /package/{validators → dist/validators}/is-exists.validator.d.ts +0 -0
- /package/{validators → dist/validators}/is-exists.validator.js +0 -0
- /package/{validators → dist/validators}/is-unique.validator.d.ts +0 -0
- /package/{validators → dist/validators}/is-unique.validator.js +0 -0
- /package/{validators → dist/validators}/phone-country-code.validator.d.ts +0 -0
- /package/{validators → dist/validators}/phone-country-code.validator.js +0 -0
- /package/{validators → dist/validators}/same-as.validator.d.ts +0 -0
- /package/{validators → dist/validators}/same-as.validator.js +0 -0
- /package/{validators → dist/validators}/skip-empty.validator.d.ts +0 -0
- /package/{validators → dist/validators}/skip-empty.validator.js +0 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import * as cv from 'class-validator';
|
|
2
|
+
import { ValidationMetadata } from 'class-validator/types/metadata/ValidationMetadata';
|
|
3
|
+
import { groupBy, isEmpty, isString, reject } from 'lodash';
|
|
4
|
+
import { defaultConverters } from './defaultConverters';
|
|
5
|
+
import { defaultOptions, IOptions } from './options';
|
|
6
|
+
import { I18nService } from 'nestjs-i18n';
|
|
7
|
+
|
|
8
|
+
export { JSONSchema } from './decorators';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Convert an array of class-validator metadata into JSON Schema definitions.
|
|
12
|
+
*/
|
|
13
|
+
export function validationMetadataArrayToSchemas(
|
|
14
|
+
metadatas: ValidationMetadata[],
|
|
15
|
+
i18n,
|
|
16
|
+
userOptions?: Partial<IOptions>,
|
|
17
|
+
): any {
|
|
18
|
+
const options: IOptions = {
|
|
19
|
+
...defaultOptions,
|
|
20
|
+
...userOptions,
|
|
21
|
+
};
|
|
22
|
+
const rules = [];
|
|
23
|
+
|
|
24
|
+
Object.entries(
|
|
25
|
+
groupBy(
|
|
26
|
+
metadatas,
|
|
27
|
+
({ target }) =>
|
|
28
|
+
target[options.schemaNameField as keyof typeof target] ??
|
|
29
|
+
(target as Function).name,
|
|
30
|
+
),
|
|
31
|
+
).forEach(([key, ownMetas]) => {
|
|
32
|
+
const target = ownMetas[0].target as Function;
|
|
33
|
+
const metas = ownMetas
|
|
34
|
+
.concat(getInheritedMetadatas(target, metadatas))
|
|
35
|
+
.filter(
|
|
36
|
+
(propMeta) =>
|
|
37
|
+
!(
|
|
38
|
+
isExcluded(propMeta, options) ||
|
|
39
|
+
isExcluded({ ...propMeta, target }, options)
|
|
40
|
+
),
|
|
41
|
+
);
|
|
42
|
+
|
|
43
|
+
const required = getRequiredPropNames(target, metas, options);
|
|
44
|
+
|
|
45
|
+
Object.entries(groupBy(metas, 'propertyName')).forEach(
|
|
46
|
+
([propName, propMetas]) => {
|
|
47
|
+
const schema = applyConverters(propMetas, options, i18n);
|
|
48
|
+
const filterSchema = schema.filter(
|
|
49
|
+
(item) => !['conditionalValidation', 'notEquals'].includes(item.type),
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
const fieldType = getFirstNonNullDataType(filterSchema);
|
|
53
|
+
rules.push({
|
|
54
|
+
field: propName,
|
|
55
|
+
fieldType,
|
|
56
|
+
required: required.includes(propName),
|
|
57
|
+
constraints: filterSchema,
|
|
58
|
+
});
|
|
59
|
+
},
|
|
60
|
+
);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
return rules;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function getFirstNonNullDataType(data: any[]): string | undefined {
|
|
67
|
+
for (const item of data) {
|
|
68
|
+
const dataType = item.dataType;
|
|
69
|
+
delete item.dataType;
|
|
70
|
+
if (dataType) {
|
|
71
|
+
return dataType;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Generate JSON Schema definitions from the target object constructor.
|
|
79
|
+
*/
|
|
80
|
+
export function targetConstructorToSchema(
|
|
81
|
+
targetConstructor: Function,
|
|
82
|
+
i18n: I18nService,
|
|
83
|
+
userOptions?: Partial<IOptions>,
|
|
84
|
+
): any {
|
|
85
|
+
const options: IOptions = {
|
|
86
|
+
...defaultOptions,
|
|
87
|
+
...userOptions,
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const storage = options.classValidatorMetadataStorage;
|
|
91
|
+
let metadatas = storage.getTargetValidationMetadatas(
|
|
92
|
+
targetConstructor,
|
|
93
|
+
'',
|
|
94
|
+
true,
|
|
95
|
+
false,
|
|
96
|
+
);
|
|
97
|
+
metadatas = populateMetadatasWithConstraints(storage, metadatas);
|
|
98
|
+
return validationMetadataArrayToSchemas(metadatas, i18n, userOptions);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function populateMetadatasWithConstraints(
|
|
102
|
+
storage: cv.MetadataStorage,
|
|
103
|
+
metadatas: ValidationMetadata[],
|
|
104
|
+
): ValidationMetadata[] {
|
|
105
|
+
return metadatas.map((meta) => {
|
|
106
|
+
if (meta.constraintCls) {
|
|
107
|
+
const constraint = storage.getTargetValidatorConstraints(
|
|
108
|
+
meta.constraintCls,
|
|
109
|
+
);
|
|
110
|
+
if (constraint.length > 0) {
|
|
111
|
+
return { ...meta, type: constraint[0].name };
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return { ...meta };
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Return target class' inherited validation metadatas, with original metadatas
|
|
120
|
+
* given precedence over inherited ones in case of duplicates.
|
|
121
|
+
*
|
|
122
|
+
* Adapted from `class-validator` source.
|
|
123
|
+
*
|
|
124
|
+
* @param target Target child class.
|
|
125
|
+
* @param metadatas All class-validator metadata objects.
|
|
126
|
+
*/
|
|
127
|
+
function getInheritedMetadatas(
|
|
128
|
+
target: Function,
|
|
129
|
+
metadatas: ValidationMetadata[],
|
|
130
|
+
) {
|
|
131
|
+
return metadatas.filter(
|
|
132
|
+
(d) =>
|
|
133
|
+
d.target instanceof Function &&
|
|
134
|
+
target.prototype instanceof d.target &&
|
|
135
|
+
!metadatas.find(
|
|
136
|
+
(m) =>
|
|
137
|
+
m.propertyName === d.propertyName &&
|
|
138
|
+
m.target === target &&
|
|
139
|
+
m.type === d.type,
|
|
140
|
+
),
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Convert a property's class-validator metadata into a JSON Schema property.
|
|
146
|
+
*/
|
|
147
|
+
function applyConverters(
|
|
148
|
+
propertyMetadatas: ValidationMetadata[],
|
|
149
|
+
options: IOptions,
|
|
150
|
+
i18n: I18nService,
|
|
151
|
+
): any {
|
|
152
|
+
const converters = { ...defaultConverters, ...options.additionalConverters };
|
|
153
|
+
|
|
154
|
+
const convert = (meta: ValidationMetadata) => {
|
|
155
|
+
const typeMeta = options.classTransformerMetadataStorage?.findTypeMetadata(
|
|
156
|
+
meta.target as Function,
|
|
157
|
+
meta.propertyName,
|
|
158
|
+
);
|
|
159
|
+
const isMap =
|
|
160
|
+
typeMeta &&
|
|
161
|
+
typeMeta.reflectedType &&
|
|
162
|
+
new typeMeta.reflectedType() instanceof Map;
|
|
163
|
+
|
|
164
|
+
const converter =
|
|
165
|
+
converters[meta.type] || converters[cv.ValidationTypes.CUSTOM_VALIDATION];
|
|
166
|
+
|
|
167
|
+
const converterResult =
|
|
168
|
+
typeof converter === 'function' ? converter(meta, options) : converter;
|
|
169
|
+
|
|
170
|
+
const items = {};
|
|
171
|
+
let message = '';
|
|
172
|
+
// @ts-ignore
|
|
173
|
+
const originMessage = typeof meta.message === 'function' ? meta.message(meta) : meta.message;
|
|
174
|
+
if (isString(originMessage)) {
|
|
175
|
+
const [translationKey, argsString] = originMessage.split('|');
|
|
176
|
+
const args = !!argsString ? JSON.parse(argsString) : {};
|
|
177
|
+
message = i18n.translate(translationKey, {
|
|
178
|
+
...options,
|
|
179
|
+
args: {
|
|
180
|
+
constraints: meta.constraints,
|
|
181
|
+
...args,
|
|
182
|
+
},
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
items['type'] = meta.type;
|
|
186
|
+
items['dataType'] = converterResult?.type || undefined;
|
|
187
|
+
items['attrs'] = meta.constraints || [];
|
|
188
|
+
items['message'] = message;
|
|
189
|
+
|
|
190
|
+
if (meta.each && isMap) {
|
|
191
|
+
return {
|
|
192
|
+
additionalProperties: {
|
|
193
|
+
...items,
|
|
194
|
+
},
|
|
195
|
+
type: 'object',
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
return meta.each ? { items, type: 'array' } : items;
|
|
199
|
+
};
|
|
200
|
+
// const constraints = [];
|
|
201
|
+
const constraints = propertyMetadatas.map((meta) => {
|
|
202
|
+
return convert(meta);
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
return reject(constraints, (item) => isEmpty(item));
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Check whether property is excluded with class-transformer `@Exclude` decorator. */
|
|
209
|
+
function isExcluded(
|
|
210
|
+
propertyMetadata: ValidationMetadata,
|
|
211
|
+
options: IOptions,
|
|
212
|
+
): boolean {
|
|
213
|
+
return !!options.classTransformerMetadataStorage?.findExcludeMetadata(
|
|
214
|
+
propertyMetadata.target as Function,
|
|
215
|
+
propertyMetadata.propertyName,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Get the required property names of a validated class.
|
|
221
|
+
* @param target Validation target class.
|
|
222
|
+
* @param metadatas Validation metadata objects of the validated class.
|
|
223
|
+
* @param options Global class-validator options.
|
|
224
|
+
*/
|
|
225
|
+
function getRequiredPropNames(
|
|
226
|
+
target: Function,
|
|
227
|
+
metadatas: ValidationMetadata[],
|
|
228
|
+
options: IOptions,
|
|
229
|
+
) {
|
|
230
|
+
function isDefined(metas: ValidationMetadata[]) {
|
|
231
|
+
return (
|
|
232
|
+
metas && metas.some(({ type }) => type === cv.ValidationTypes.IS_DEFINED)
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function isOptional(metas: ValidationMetadata[]) {
|
|
237
|
+
return (
|
|
238
|
+
metas &&
|
|
239
|
+
metas.some(({ type }) =>
|
|
240
|
+
[cv.ValidationTypes.CONDITIONAL_VALIDATION, cv.IS_EMPTY].includes(type),
|
|
241
|
+
)
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
return (
|
|
246
|
+
Object.entries(groupBy(metadatas, (m) => m.propertyName))
|
|
247
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
248
|
+
.filter(([_, metas]) => {
|
|
249
|
+
const own = metas.filter((m) => m.target === target);
|
|
250
|
+
const inherited = metas.filter((m) => m.target !== target);
|
|
251
|
+
return options.skipMissingProperties
|
|
252
|
+
? isDefined(own) || (!isOptional(own) && isDefined(inherited))
|
|
253
|
+
: !(isOptional(own) || isOptional(inherited));
|
|
254
|
+
})
|
|
255
|
+
.map(([name]) => name)
|
|
256
|
+
);
|
|
257
|
+
}
|
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
// tslint:disable:no-submodule-imports
|
|
2
|
+
import * as cv from 'class-validator';
|
|
3
|
+
import type { ValidationMetadata } from 'class-validator/types/metadata/ValidationMetadata';
|
|
4
|
+
import 'reflect-metadata';
|
|
5
|
+
|
|
6
|
+
import { IOptions } from './options';
|
|
7
|
+
|
|
8
|
+
export interface ISchemaConverters {
|
|
9
|
+
[validatorType: string]: any;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type SchemaConverter = (
|
|
13
|
+
meta: ValidationMetadata,
|
|
14
|
+
options: IOptions,
|
|
15
|
+
) => any;
|
|
16
|
+
|
|
17
|
+
export const defaultConverters: ISchemaConverters = {
|
|
18
|
+
[cv.ValidationTypes.CUSTOM_VALIDATION]: (meta, options) => {
|
|
19
|
+
if (typeof meta.target === 'function') {
|
|
20
|
+
const type = getPropType(meta.target.prototype, meta.propertyName);
|
|
21
|
+
return targetToSchema(type, options);
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
[cv.ValidationTypes.NESTED_VALIDATION]: (meta, options) => {
|
|
25
|
+
if (typeof meta.target === 'function') {
|
|
26
|
+
const typeMeta = options.classTransformerMetadataStorage
|
|
27
|
+
? options.classTransformerMetadataStorage.findTypeMetadata(
|
|
28
|
+
meta.target,
|
|
29
|
+
meta.propertyName,
|
|
30
|
+
)
|
|
31
|
+
: null;
|
|
32
|
+
const childType = typeMeta
|
|
33
|
+
? typeMeta.typeFunction()
|
|
34
|
+
: getPropType(meta.target.prototype, meta.propertyName);
|
|
35
|
+
return targetToSchema(childType, options);
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
[cv.ValidationTypes.WHITELIST]: {},
|
|
39
|
+
[cv.ValidationTypes.CONDITIONAL_VALIDATION]: {},
|
|
40
|
+
[cv.ValidationTypes.IS_DEFINED]: {
|
|
41
|
+
not: { type: 'null' },
|
|
42
|
+
},
|
|
43
|
+
[cv.EQUALS]: (meta) => {
|
|
44
|
+
const schema = constraintToSchema(meta.constraints[0]);
|
|
45
|
+
if (schema) {
|
|
46
|
+
return { ...schema, enum: [meta.constraints[0]] };
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
[cv.NOT_EQUALS]: (meta) => {
|
|
50
|
+
const schema = constraintToSchema(meta.constraints[0]);
|
|
51
|
+
if (schema) {
|
|
52
|
+
return { not: { ...schema, enum: [meta.constraints[0]] } };
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
[cv.IS_EMPTY]: {
|
|
56
|
+
type: 'string',
|
|
57
|
+
anyOf: [
|
|
58
|
+
{ type: 'string', enum: [''] },
|
|
59
|
+
{
|
|
60
|
+
not: {
|
|
61
|
+
anyOf: [
|
|
62
|
+
{ type: 'string' },
|
|
63
|
+
{ type: 'number' },
|
|
64
|
+
{ type: 'boolean' },
|
|
65
|
+
{ type: 'integer' },
|
|
66
|
+
{ type: 'array' },
|
|
67
|
+
{ type: 'object' },
|
|
68
|
+
],
|
|
69
|
+
},
|
|
70
|
+
nullable: true,
|
|
71
|
+
},
|
|
72
|
+
],
|
|
73
|
+
},
|
|
74
|
+
[cv.IS_NOT_EMPTY]: {
|
|
75
|
+
minLength: 1,
|
|
76
|
+
type: 'string',
|
|
77
|
+
},
|
|
78
|
+
[cv.IS_IN]: (meta) => {
|
|
79
|
+
const [head, ...rest]: any[] = meta.constraints[0].map(constraintToSchema);
|
|
80
|
+
if (head && rest.every((item) => item.type === head.type)) {
|
|
81
|
+
return { ...head, enum: meta.constraints[0] };
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
type: 'string',
|
|
85
|
+
};
|
|
86
|
+
},
|
|
87
|
+
[cv.IS_NOT_IN]: (meta) => {
|
|
88
|
+
const [head, ...rest]: any[] = meta.constraints[0].map(constraintToSchema);
|
|
89
|
+
if (head && rest.every((item) => item.type === head.type)) {
|
|
90
|
+
return { not: { ...head, enum: meta.constraints[0] } };
|
|
91
|
+
}
|
|
92
|
+
return {
|
|
93
|
+
type: 'string',
|
|
94
|
+
};
|
|
95
|
+
},
|
|
96
|
+
[cv.IS_BOOLEAN]: {
|
|
97
|
+
type: 'boolean',
|
|
98
|
+
},
|
|
99
|
+
[cv.IS_DATE]: {
|
|
100
|
+
type: 'string',
|
|
101
|
+
oneOf: [
|
|
102
|
+
{ format: 'date', type: 'string' },
|
|
103
|
+
{ format: 'date-time', type: 'string' },
|
|
104
|
+
],
|
|
105
|
+
},
|
|
106
|
+
[cv.IS_NUMBER]: {
|
|
107
|
+
type: 'number',
|
|
108
|
+
},
|
|
109
|
+
[cv.IS_STRING]: {
|
|
110
|
+
type: 'string',
|
|
111
|
+
},
|
|
112
|
+
[cv.IS_DATE_STRING]: {
|
|
113
|
+
pattern: '\\d{4}-[01]\\d-[0-3]\\dT[0-2]\\d:[0-5]\\d:[0-5]\\d.\\d+Z?',
|
|
114
|
+
type: 'string',
|
|
115
|
+
},
|
|
116
|
+
[cv.IS_ARRAY]: {
|
|
117
|
+
items: {},
|
|
118
|
+
type: 'array',
|
|
119
|
+
},
|
|
120
|
+
[cv.IS_INT]: {
|
|
121
|
+
type: 'integer',
|
|
122
|
+
},
|
|
123
|
+
[cv.IS_ENUM]: (meta) => {
|
|
124
|
+
return {
|
|
125
|
+
enum: Object.values(meta.constraints[0]),
|
|
126
|
+
type: 'string',
|
|
127
|
+
};
|
|
128
|
+
},
|
|
129
|
+
[cv.IS_DIVISIBLE_BY]: (meta) => ({
|
|
130
|
+
multipleOf: meta.constraints[0],
|
|
131
|
+
type: 'number',
|
|
132
|
+
}),
|
|
133
|
+
[cv.IS_POSITIVE]: {
|
|
134
|
+
exclusiveMinimum: 0,
|
|
135
|
+
type: 'number',
|
|
136
|
+
},
|
|
137
|
+
[cv.IS_NEGATIVE]: {
|
|
138
|
+
exclusiveMaximum: 0,
|
|
139
|
+
type: 'number',
|
|
140
|
+
},
|
|
141
|
+
[cv.MIN]: (meta) => ({
|
|
142
|
+
minimum: meta.constraints[0],
|
|
143
|
+
type: 'number',
|
|
144
|
+
}),
|
|
145
|
+
[cv.MAX]: (meta) => ({
|
|
146
|
+
maximum: meta.constraints[0],
|
|
147
|
+
type: 'number',
|
|
148
|
+
}),
|
|
149
|
+
[cv.MIN_DATE]: (meta) => ({
|
|
150
|
+
description: `After ${meta.constraints[0].toJSON()}`,
|
|
151
|
+
oneOf: [
|
|
152
|
+
{ format: 'date', type: 'string' },
|
|
153
|
+
{ format: 'date-time', type: 'string' },
|
|
154
|
+
],
|
|
155
|
+
}),
|
|
156
|
+
[cv.MAX_DATE]: (meta) => ({
|
|
157
|
+
description: `Before ${meta.constraints[0].toJSON()}`,
|
|
158
|
+
oneOf: [
|
|
159
|
+
{ format: 'date', type: 'string' },
|
|
160
|
+
{ format: 'date-time', type: 'string' },
|
|
161
|
+
],
|
|
162
|
+
}),
|
|
163
|
+
[cv.IS_BOOLEAN_STRING]: {
|
|
164
|
+
enum: ['true', 'false'],
|
|
165
|
+
type: 'string',
|
|
166
|
+
},
|
|
167
|
+
[cv.IS_NUMBER_STRING]: {
|
|
168
|
+
pattern: '^[-+]?[0-9]+$',
|
|
169
|
+
type: 'string',
|
|
170
|
+
},
|
|
171
|
+
[cv.CONTAINS]: (meta) => ({
|
|
172
|
+
pattern: meta.constraints[0],
|
|
173
|
+
type: 'string',
|
|
174
|
+
}),
|
|
175
|
+
[cv.NOT_CONTAINS]: (meta) => ({
|
|
176
|
+
not: { pattern: meta.constraints[0] },
|
|
177
|
+
type: 'string',
|
|
178
|
+
}),
|
|
179
|
+
[cv.IS_ALPHA]: {
|
|
180
|
+
pattern: '^[a-zA-Z]+$',
|
|
181
|
+
type: 'string',
|
|
182
|
+
},
|
|
183
|
+
[cv.IS_ALPHANUMERIC]: {
|
|
184
|
+
pattern: '^[0-9a-zA-Z]+$',
|
|
185
|
+
type: 'string',
|
|
186
|
+
},
|
|
187
|
+
[cv.IS_ASCII]: {
|
|
188
|
+
pattern: '^[\\x00-\\x7F]+$',
|
|
189
|
+
type: 'string',
|
|
190
|
+
},
|
|
191
|
+
[cv.IS_BASE64]: {
|
|
192
|
+
format: 'base64',
|
|
193
|
+
type: 'string',
|
|
194
|
+
},
|
|
195
|
+
[cv.IS_BYTE_LENGTH]: {
|
|
196
|
+
type: 'string',
|
|
197
|
+
},
|
|
198
|
+
[cv.IS_CREDIT_CARD]: {
|
|
199
|
+
format: 'credit-card',
|
|
200
|
+
type: 'string',
|
|
201
|
+
},
|
|
202
|
+
[cv.IS_CURRENCY]: {
|
|
203
|
+
format: 'currency',
|
|
204
|
+
type: 'string',
|
|
205
|
+
},
|
|
206
|
+
[cv.IS_EMAIL]: {
|
|
207
|
+
format: 'email',
|
|
208
|
+
type: 'string',
|
|
209
|
+
},
|
|
210
|
+
[cv.IS_FQDN]: {
|
|
211
|
+
format: 'hostname',
|
|
212
|
+
type: 'string',
|
|
213
|
+
},
|
|
214
|
+
[cv.IS_FULL_WIDTH]: {
|
|
215
|
+
pattern:
|
|
216
|
+
'[^\\u0020-\\u007E\\uFF61-\\uFF9F\\uFFA0-\\uFFDC\\uFFE8-\\uFFEE0-9a-zA-Z]',
|
|
217
|
+
type: 'string',
|
|
218
|
+
},
|
|
219
|
+
[cv.IS_HALF_WIDTH]: {
|
|
220
|
+
pattern:
|
|
221
|
+
'[\\u0020-\\u007E\\uFF61-\\uFF9F\\uFFA0-\\uFFDC\\uFFE8-\\uFFEE0-9a-zA-Z]',
|
|
222
|
+
type: 'string',
|
|
223
|
+
},
|
|
224
|
+
[cv.IS_VARIABLE_WIDTH]: {
|
|
225
|
+
type: 'string',
|
|
226
|
+
},
|
|
227
|
+
[cv.IS_HEX_COLOR]: {
|
|
228
|
+
pattern: '^#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})$',
|
|
229
|
+
type: 'string',
|
|
230
|
+
},
|
|
231
|
+
[cv.IS_HEXADECIMAL]: {
|
|
232
|
+
pattern: '^[0-9a-fA-F]+$',
|
|
233
|
+
type: 'string',
|
|
234
|
+
},
|
|
235
|
+
[cv.IS_IP]: (meta) => ({
|
|
236
|
+
format: 'ipv' + (meta.constraints[0] === '6' ? 6 : 4),
|
|
237
|
+
type: 'string',
|
|
238
|
+
}),
|
|
239
|
+
[cv.IS_ISBN]: {
|
|
240
|
+
format: 'isbn',
|
|
241
|
+
type: 'string',
|
|
242
|
+
},
|
|
243
|
+
[cv.IS_ISIN]: {
|
|
244
|
+
format: 'isin',
|
|
245
|
+
type: 'string',
|
|
246
|
+
},
|
|
247
|
+
[cv.IS_ISO8601]: {
|
|
248
|
+
oneOf: [
|
|
249
|
+
{ format: 'date', type: 'string' },
|
|
250
|
+
{ format: 'date-time', type: 'string' },
|
|
251
|
+
],
|
|
252
|
+
},
|
|
253
|
+
[cv.IS_JSON]: {
|
|
254
|
+
format: 'json',
|
|
255
|
+
type: 'string',
|
|
256
|
+
},
|
|
257
|
+
[cv.IS_LOWERCASE]: {
|
|
258
|
+
type: 'string',
|
|
259
|
+
},
|
|
260
|
+
[cv.IS_MOBILE_PHONE]: {
|
|
261
|
+
format: 'mobile-phone',
|
|
262
|
+
type: 'string',
|
|
263
|
+
},
|
|
264
|
+
[cv.IS_MONGO_ID]: {
|
|
265
|
+
pattern: '^[0-9a-fA-F]{24}$',
|
|
266
|
+
type: 'string',
|
|
267
|
+
},
|
|
268
|
+
[cv.IS_MULTIBYTE]: {
|
|
269
|
+
pattern: '[^\\x00-\\x7F]',
|
|
270
|
+
type: 'string',
|
|
271
|
+
},
|
|
272
|
+
[cv.IS_SURROGATE_PAIR]: {
|
|
273
|
+
pattern: '[\\uD800-\\uDBFF][\\uDC00-\\uDFFF]',
|
|
274
|
+
type: 'string',
|
|
275
|
+
},
|
|
276
|
+
[cv.IS_URL]: {
|
|
277
|
+
format: 'url',
|
|
278
|
+
type: 'string',
|
|
279
|
+
},
|
|
280
|
+
[cv.IS_UUID]: {
|
|
281
|
+
format: 'uuid',
|
|
282
|
+
type: 'string',
|
|
283
|
+
},
|
|
284
|
+
[cv.IS_LENGTH]: (meta) => {
|
|
285
|
+
const [minLength, maxLength] = meta.constraints;
|
|
286
|
+
if (maxLength || maxLength === 0) {
|
|
287
|
+
return { min: minLength, max: maxLength, type: 'Range' };
|
|
288
|
+
}
|
|
289
|
+
return { min: minLength, type: 'Range' };
|
|
290
|
+
},
|
|
291
|
+
[cv.IS_UPPERCASE]: {
|
|
292
|
+
type: 'string',
|
|
293
|
+
},
|
|
294
|
+
[cv.IS_OBJECT]: {
|
|
295
|
+
type: 'object',
|
|
296
|
+
},
|
|
297
|
+
[cv.IS_NOT_EMPTY_OBJECT]: {
|
|
298
|
+
type: 'object',
|
|
299
|
+
minProperties: 1,
|
|
300
|
+
},
|
|
301
|
+
[cv.MIN_LENGTH]: (meta) => ({
|
|
302
|
+
minLength: meta.constraints[0],
|
|
303
|
+
type: 'string',
|
|
304
|
+
}),
|
|
305
|
+
[cv.MAX_LENGTH]: (meta) => ({
|
|
306
|
+
maxLength: meta.constraints[0],
|
|
307
|
+
type: 'string',
|
|
308
|
+
}),
|
|
309
|
+
[cv.MATCHES]: (meta) => ({
|
|
310
|
+
pattern: meta.constraints[0].source,
|
|
311
|
+
type: 'string',
|
|
312
|
+
}),
|
|
313
|
+
[cv.IS_MILITARY_TIME]: {
|
|
314
|
+
pattern: '^([01]\\d|2[0-3]):?([0-5]\\d)$',
|
|
315
|
+
type: 'string',
|
|
316
|
+
},
|
|
317
|
+
[cv.ARRAY_CONTAINS]: (meta) => {
|
|
318
|
+
const schemas: any[] = meta.constraints[0].map(constraintToSchema);
|
|
319
|
+
if (schemas.length > 0 && schemas.every((s) => s && s.type)) {
|
|
320
|
+
return {
|
|
321
|
+
not: {
|
|
322
|
+
anyOf: schemas.map((d, i) => ({
|
|
323
|
+
items: {
|
|
324
|
+
not: {
|
|
325
|
+
...d,
|
|
326
|
+
enum: [meta.constraints[0][i]],
|
|
327
|
+
},
|
|
328
|
+
},
|
|
329
|
+
})),
|
|
330
|
+
},
|
|
331
|
+
type: 'array',
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
return { items: {}, type: 'array' };
|
|
335
|
+
},
|
|
336
|
+
[cv.ARRAY_NOT_CONTAINS]: (meta) => {
|
|
337
|
+
const schemas: any[] = meta.constraints[0].map(constraintToSchema);
|
|
338
|
+
if (schemas.length > 0 && schemas.every((s) => s && s.type)) {
|
|
339
|
+
return {
|
|
340
|
+
items: {
|
|
341
|
+
not: {
|
|
342
|
+
anyOf: schemas.map((d, i) => ({
|
|
343
|
+
...d,
|
|
344
|
+
enum: [meta.constraints[0][i]],
|
|
345
|
+
})),
|
|
346
|
+
},
|
|
347
|
+
},
|
|
348
|
+
type: 'array',
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
return { items: {}, type: 'array' };
|
|
352
|
+
},
|
|
353
|
+
[cv.ARRAY_NOT_EMPTY]: {
|
|
354
|
+
items: {},
|
|
355
|
+
minItems: 1,
|
|
356
|
+
type: 'array',
|
|
357
|
+
},
|
|
358
|
+
[cv.ARRAY_MIN_SIZE]: (meta) => ({
|
|
359
|
+
items: {},
|
|
360
|
+
minItems: meta.constraints[0],
|
|
361
|
+
type: 'array',
|
|
362
|
+
}),
|
|
363
|
+
[cv.ARRAY_MAX_SIZE]: (meta) => ({
|
|
364
|
+
items: {},
|
|
365
|
+
maxItems: meta.constraints[0],
|
|
366
|
+
type: 'array',
|
|
367
|
+
}),
|
|
368
|
+
[cv.ARRAY_UNIQUE]: {
|
|
369
|
+
items: {},
|
|
370
|
+
type: 'array',
|
|
371
|
+
uniqueItems: true,
|
|
372
|
+
},
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
function getPropType(target: object, property: string) {
|
|
376
|
+
return Reflect.getMetadata('design:type', target, property);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
function constraintToSchema(primitive: any): any | void {
|
|
380
|
+
const primitives = ['string', 'number', 'boolean'];
|
|
381
|
+
const type = typeof primitive;
|
|
382
|
+
if (primitives.includes(type)) {
|
|
383
|
+
return { type: type as 'string' | 'number' | 'boolean' };
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function targetToSchema(type: any, options: IOptions): any {
|
|
388
|
+
if (typeof type === 'function') {
|
|
389
|
+
if (
|
|
390
|
+
type.prototype === String.prototype ||
|
|
391
|
+
type.prototype === Symbol.prototype
|
|
392
|
+
) {
|
|
393
|
+
return { type: 'string' };
|
|
394
|
+
} else if (type.prototype === Number.prototype) {
|
|
395
|
+
return { type: 'number' };
|
|
396
|
+
} else if (type.prototype === Boolean.prototype) {
|
|
397
|
+
return { type: 'boolean' };
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
return { $ref: options.refPointerPrefix + type.name };
|
|
401
|
+
}
|
|
402
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { MetadataStorage as ClassTransformerMetadataStorage } from 'class-transformer/types/MetadataStorage'; // tslint:disable-line:no-submodule-imports
|
|
2
|
+
import {
|
|
3
|
+
getMetadataStorage,
|
|
4
|
+
MetadataStorage,
|
|
5
|
+
ValidatorOptions,
|
|
6
|
+
} from 'class-validator';
|
|
7
|
+
|
|
8
|
+
import { ISchemaConverters } from './defaultConverters';
|
|
9
|
+
|
|
10
|
+
export interface IOptions extends ValidatorOptions {
|
|
11
|
+
/**
|
|
12
|
+
* A map of additional metadata-to-schema converters that can be used to
|
|
13
|
+
* supplement or override the default ones. The key should correspond to the
|
|
14
|
+
* 'type' property of a ValidationMetadata object.
|
|
15
|
+
*/
|
|
16
|
+
additionalConverters: ISchemaConverters;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Metadata storage instance of class-transformer. This value can be
|
|
20
|
+
* optionally defined in order for class-transformer's @Type decorators to
|
|
21
|
+
* have effect in JSON Schema generation.
|
|
22
|
+
*/
|
|
23
|
+
classTransformerMetadataStorage?: ClassTransformerMetadataStorage;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Metadata storage instance of class-validator. This value can be
|
|
27
|
+
* optionally defined in order to override the default storage used
|
|
28
|
+
* to parse decorator metadata.
|
|
29
|
+
*/
|
|
30
|
+
classValidatorMetadataStorage: MetadataStorage;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* A prefix added to all `$ref` JSON pointers referencing other schemas.
|
|
34
|
+
* Defaults to '#/definitions/'.
|
|
35
|
+
*/
|
|
36
|
+
refPointerPrefix: string;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Property used to group schemas.
|
|
40
|
+
* Defaults to `name`, i.e., class name.
|
|
41
|
+
*/
|
|
42
|
+
schemaNameField: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const defaultOptions: IOptions = {
|
|
46
|
+
additionalConverters: {},
|
|
47
|
+
classValidatorMetadataStorage: getMetadataStorage(),
|
|
48
|
+
refPointerPrefix: '#/definitions/',
|
|
49
|
+
schemaNameField: 'name',
|
|
50
|
+
};
|