UNPKG

@joktec/mongo

Version:

JokTec - Mongo Service

68 lines 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StringProps = StringProps; const utils_1 = require("@joktec/utils"); const lodash_1 = require("lodash"); function StringProps(opts, swagger) { const decorators = []; const stringOption = { each: swagger.isArray }; if (!opts.isString) { const parseStringOption = (0, lodash_1.isArray)(opts.isString) ? { message: opts.isString[1] } : (0, lodash_1.isObject)(opts.isString) ? opts.isString : {}; stringOption.message = parseStringOption.message; } decorators.push((0, utils_1.IsString)(stringOption)); if (!(0, lodash_1.isNil)(opts.minlength)) { const minLength = (0, lodash_1.isNumber)(opts.minlength) ? [(0, utils_1.toInt)(opts.minlength)] : opts.minlength; const validatorOption = { each: swagger.isArray }; if (minLength[1]) validatorOption.message = minLength[1]; decorators.push((0, utils_1.MinLength)(minLength[0], validatorOption)); swagger.minLength = minLength[0]; } if (!(0, lodash_1.isNil)(opts.maxlength)) { const maxLength = (0, lodash_1.isNumber)(opts.maxlength) ? [(0, utils_1.toInt)(opts.maxlength)] : opts.maxlength; const validatorOption = { each: swagger.isArray }; if (maxLength[1]) validatorOption.message = maxLength[1]; decorators.push((0, utils_1.MaxLength)(maxLength[0], validatorOption)); swagger.maxLength = maxLength[0]; } if (opts.isEmail) { const defMsg = '$property is an invalid email format'; const emailOption = (0, lodash_1.isArray)(opts.isEmail) ? { message: opts.isEmail[1] || defMsg } : (0, lodash_1.isObject)(opts.isEmail) ? opts.isEmail : {}; decorators.push((0, utils_1.IsEmail)(emailOption, { each: swagger.isArray, message: emailOption.message })); swagger.format = 'email'; opts.validate = (v) => (0, lodash_1.isNil)(v) || (0, utils_1.isEmail)(v, emailOption); } if (opts.isPhone) { const defMsg = '$property is an invalid phone number format'; const phoneOption = (0, lodash_1.isArray)(opts.isPhone) ? { message: opts.isEmail[1] || defMsg } : (0, lodash_1.isObject)(opts.isEmail) ? opts.isEmail : {}; const strictMode = (0, utils_1.toBool)(phoneOption.strictMode, true); decorators.push((0, utils_1.IsMobilePhone)(phoneOption.locale, { strictMode }, { each: swagger.isArray, message: phoneOption.message })); opts.validate = (v) => (0, lodash_1.isNil)(v) || (0, utils_1.isMobilePhone)(v, phoneOption.locale); } if (opts.isHexColor) { const defMsg = '$property must be a hex color'; const hexOption = (0, lodash_1.isArray)(opts.isHexColor) ? { message: opts.isHexColor[1] || defMsg } : {}; decorators.push((0, utils_1.IsHexColor)({ each: swagger.isArray, message: hexOption.message })); } if (opts.isUrl) { const defMsg = '$property must be a url'; const urlOption = (0, lodash_1.isArray)(opts.isUrl) ? { message: opts.isUrl[1] || defMsg } : {}; decorators.push((0, utils_1.IsUrl)({}, { each: swagger.isArray, message: urlOption.message })); } return decorators; } //# sourceMappingURL=string.prop.js.map