@tsoa/runtime
Advanced tools
Comparing version 6.1.5 to 6.2.0
@@ -214,2 +214,3 @@ import { Swagger } from './swagger/swagger'; | ||
esm?: boolean; | ||
bodyCoercion?: boolean; | ||
} |
@@ -1,4 +0,5 @@ | ||
import { Config } from '../config'; | ||
import { Config, RoutesConfig } from '../config'; | ||
export interface AdditionalProps { | ||
noImplicitAdditionalProperties: Exclude<Config['noImplicitAdditionalProperties'], undefined>; | ||
bodyCoercion: Exclude<RoutesConfig['bodyCoercion'], undefined>; | ||
} |
/// <reference types="node" /> | ||
import { AdditionalProps } from './additionalProps'; | ||
import { TsoaRoute } from './tsoa-route'; | ||
export declare function ValidateParam(property: TsoaRoute.PropertySchema, value: any, generatedModels: TsoaRoute.Models, name: string | undefined, fieldErrors: FieldErrors, parent: string | undefined, swaggerConfig: AdditionalProps): any; | ||
export declare function ValidateParam(property: TsoaRoute.PropertySchema, value: any, generatedModels: TsoaRoute.Models, name: string | undefined, fieldErrors: FieldErrors, isBodyParam: boolean, parent: string | undefined, config: AdditionalProps): any; | ||
export declare class ValidationService { | ||
private readonly models; | ||
constructor(models: TsoaRoute.Models); | ||
ValidateParam(property: TsoaRoute.PropertySchema, rawValue: any, name: string | undefined, fieldErrors: FieldErrors, parent: string | undefined, minimalSwaggerConfig: AdditionalProps): any; | ||
validateNestedObjectLiteral(name: string, value: any, fieldErrors: FieldErrors, swaggerConfig: AdditionalProps, nestedProperties: { | ||
private readonly config; | ||
constructor(models: TsoaRoute.Models, config: AdditionalProps); | ||
ValidateParam(property: TsoaRoute.PropertySchema, rawValue: any, name: string | undefined, fieldErrors: FieldErrors, isBodyParam: boolean, parent?: string): any; | ||
hasCorrectJsType(value: any, type: 'object' | 'boolean' | 'number' | 'string', isBodyParam: boolean): boolean; | ||
validateNestedObjectLiteral(name: string, value: any, fieldErrors: FieldErrors, isBodyParam: boolean, nestedProperties: { | ||
[name: string]: TsoaRoute.PropertySchema; | ||
} | undefined, additionalProperties: TsoaRoute.PropertySchema | boolean | undefined, parent: string): any; | ||
validateInt(name: string, value: any, fieldErrors: FieldErrors, validators?: IntegerValidator, parent?: string): number | undefined; | ||
validateFloat(name: string, value: any, fieldErrors: FieldErrors, validators?: FloatValidator, parent?: string): number | undefined; | ||
validateInt(name: string, value: any, fieldErrors: FieldErrors, isBodyParam: boolean, validators?: IntegerValidator, parent?: string): number | undefined; | ||
validateFloat(name: string, value: any, fieldErrors: FieldErrors, isBodyParam: boolean, validators?: FloatValidator, parent?: string): number | undefined; | ||
validateEnum(name: string, value: unknown, fieldErrors: FieldErrors, members?: Array<string | number | boolean | null>, parent?: string): unknown; | ||
validateDate(name: string, value: any, fieldErrors: FieldErrors, validators?: DateValidator, parent?: string): Date | undefined; | ||
validateDateTime(name: string, value: any, fieldErrors: FieldErrors, validators?: DateTimeValidator, parent?: string): Date | undefined; | ||
validateDate(name: string, value: any, fieldErrors: FieldErrors, isBodyParam: boolean, validators?: DateValidator, parent?: string): Date | undefined; | ||
validateDateTime(name: string, value: any, fieldErrors: FieldErrors, isBodyParam: boolean, validators?: DateTimeValidator, parent?: string): Date | undefined; | ||
validateString(name: string, value: any, fieldErrors: FieldErrors, validators?: StringValidator, parent?: string): string | undefined; | ||
validateBool(name: string, value: any, fieldErrors: FieldErrors, validators?: BooleanValidator, parent?: string): any; | ||
validateBool(name: string, value: any, fieldErrors: FieldErrors, isBodyParam: boolean, validators?: BooleanValidator, parent?: string): any; | ||
validateUndefined(name: string, value: any, fieldErrors: FieldErrors, parent?: string): undefined; | ||
validateArray(name: string, value: any[], fieldErrors: FieldErrors, swaggerConfig: AdditionalProps, schema?: TsoaRoute.PropertySchema, validators?: ArrayValidator, parent?: string): any[] | undefined; | ||
validateArray(name: string, value: any[], fieldErrors: FieldErrors, isBodyParam: boolean, schema?: TsoaRoute.PropertySchema, validators?: ArrayValidator, parent?: string): any[] | undefined; | ||
validateBuffer(_name: string, value: string): Buffer; | ||
validateUnion(name: string, value: any, fieldErrors: FieldErrors, swaggerConfig: AdditionalProps, property: TsoaRoute.PropertySchema, parent?: string): any; | ||
validateIntersection(name: string, value: any, fieldErrors: FieldErrors, swaggerConfig: AdditionalProps, subSchemas: TsoaRoute.PropertySchema[] | undefined, parent?: string): any; | ||
validateUnion(name: string, value: any, fieldErrors: FieldErrors, isBodyParam: boolean, property: TsoaRoute.PropertySchema, parent?: string): any; | ||
validateIntersection(name: string, value: any, fieldErrors: FieldErrors, isBodyParam: boolean, subSchemas: TsoaRoute.PropertySchema[] | undefined, parent?: string): any; | ||
private toModelLike; | ||
@@ -51,4 +53,4 @@ /** | ||
fieldErrors: FieldErrors; | ||
isBodyParam: boolean; | ||
parent?: string; | ||
minimalSwaggerConfig: AdditionalProps; | ||
}): any; | ||
@@ -55,0 +57,0 @@ } |
@@ -11,11 +11,12 @@ "use strict"; | ||
// for backwards compatibility with custom templates | ||
function ValidateParam(property, value, generatedModels, name = '', fieldErrors, parent = '', swaggerConfig) { | ||
return new ValidationService(generatedModels).ValidateParam(property, value, name, fieldErrors, parent, swaggerConfig); | ||
function ValidateParam(property, value, generatedModels, name = '', fieldErrors, isBodyParam, parent = '', config) { | ||
return new ValidationService(generatedModels, config).ValidateParam(property, value, name, fieldErrors, isBodyParam, parent); | ||
} | ||
exports.ValidateParam = ValidateParam; | ||
class ValidationService { | ||
constructor(models) { | ||
constructor(models, config) { | ||
this.models = models; | ||
this.config = config; | ||
} | ||
ValidateParam(property, rawValue, name = '', fieldErrors, parent = '', minimalSwaggerConfig) { | ||
ValidateParam(property, rawValue, name = '', fieldErrors, isBodyParam, parent = '') { | ||
let value = rawValue; | ||
@@ -54,23 +55,23 @@ // If undefined is allowed type, we can move to value validation | ||
case 'boolean': | ||
return this.validateBool(name, value, fieldErrors, property.validators, parent); | ||
return this.validateBool(name, value, fieldErrors, isBodyParam, property.validators, parent); | ||
case 'integer': | ||
case 'long': | ||
return this.validateInt(name, value, fieldErrors, property.validators, parent); | ||
return this.validateInt(name, value, fieldErrors, isBodyParam, property.validators, parent); | ||
case 'float': | ||
case 'double': | ||
return this.validateFloat(name, value, fieldErrors, property.validators, parent); | ||
return this.validateFloat(name, value, fieldErrors, isBodyParam, property.validators, parent); | ||
case 'enum': | ||
return this.validateEnum(name, value, fieldErrors, property.enums, parent); | ||
case 'array': | ||
return this.validateArray(name, value, fieldErrors, minimalSwaggerConfig, property.array, property.validators, parent); | ||
return this.validateArray(name, value, fieldErrors, isBodyParam, property.array, property.validators, parent); | ||
case 'date': | ||
return this.validateDate(name, value, fieldErrors, property.validators, parent); | ||
return this.validateDate(name, value, fieldErrors, isBodyParam, property.validators, parent); | ||
case 'datetime': | ||
return this.validateDateTime(name, value, fieldErrors, property.validators, parent); | ||
return this.validateDateTime(name, value, fieldErrors, isBodyParam, property.validators, parent); | ||
case 'buffer': | ||
return this.validateBuffer(name, value); | ||
case 'union': | ||
return this.validateUnion(name, value, fieldErrors, minimalSwaggerConfig, property, parent); | ||
return this.validateUnion(name, value, fieldErrors, isBodyParam, property, parent); | ||
case 'intersection': | ||
return this.validateIntersection(name, value, fieldErrors, minimalSwaggerConfig, property.subSchemas, parent); | ||
return this.validateIntersection(name, value, fieldErrors, isBodyParam, property.subSchemas, parent); | ||
case 'undefined': | ||
@@ -81,6 +82,6 @@ return this.validateUndefined(name, value, fieldErrors, parent); | ||
case 'nestedObjectLiteral': | ||
return this.validateNestedObjectLiteral(name, value, fieldErrors, minimalSwaggerConfig, property.nestedProperties, property.additionalProperties, parent); | ||
return this.validateNestedObjectLiteral(name, value, fieldErrors, isBodyParam, property.nestedProperties, property.additionalProperties, parent); | ||
default: | ||
if (property.ref) { | ||
return this.validateModel({ name, value, modelDefinition: this.models[property.ref], fieldErrors, parent, minimalSwaggerConfig }); | ||
return this.validateModel({ name, value, modelDefinition: this.models[property.ref], fieldErrors, isBodyParam, parent }); | ||
} | ||
@@ -90,3 +91,6 @@ return value; | ||
} | ||
validateNestedObjectLiteral(name, value, fieldErrors, swaggerConfig, nestedProperties, additionalProperties, parent) { | ||
hasCorrectJsType(value, type, isBodyParam) { | ||
return !isBodyParam || this.config.bodyCoercion || typeof value === type; | ||
} | ||
validateNestedObjectLiteral(name, value, fieldErrors, isBodyParam, nestedProperties, additionalProperties, parent) { | ||
if (typeof value !== 'object' || value === null || Array.isArray(value)) { | ||
@@ -106,5 +110,5 @@ fieldErrors[parent + name] = { | ||
} | ||
const propHandling = swaggerConfig.noImplicitAdditionalProperties; | ||
const propHandling = this.config.noImplicitAdditionalProperties; | ||
if (propHandling !== 'ignore') { | ||
const excessProps = this.getExcessPropertiesFor({ dataType: 'refObject', properties: nestedProperties, additionalProperties }, Object.keys(value), swaggerConfig); | ||
const excessProps = this.getExcessPropertiesFor({ dataType: 'refObject', properties: nestedProperties, additionalProperties }, Object.keys(value)); | ||
if (excessProps.length > 0) { | ||
@@ -125,3 +129,3 @@ if (propHandling === 'silently-remove-extras') { | ||
Object.keys(nestedProperties).forEach(key => { | ||
const validatedProp = this.ValidateParam(nestedProperties[key], value[key], key, fieldErrors, parent + name + '.', swaggerConfig); | ||
const validatedProp = this.ValidateParam(nestedProperties[key], value[key], key, fieldErrors, isBodyParam, parent + name + '.'); | ||
// Add value from validator if it's not undefined or if value is required and unfedined is valid type | ||
@@ -135,3 +139,3 @@ if (validatedProp !== undefined || (nestedProperties[key].dataType === 'undefined' && nestedProperties[key].required)) { | ||
keys.forEach(key => { | ||
const validatedProp = this.ValidateParam(additionalProperties, value[key], key, fieldErrors, parent + name + '.', swaggerConfig); | ||
const validatedProp = this.ValidateParam(additionalProperties, value[key], key, fieldErrors, isBodyParam, parent + name + '.'); | ||
// Add value from validator if it's not undefined or if value is required and unfedined is valid type | ||
@@ -148,4 +152,4 @@ if (validatedProp !== undefined || (additionalProperties.dataType === 'undefined' && additionalProperties.required)) { | ||
} | ||
validateInt(name, value, fieldErrors, validators, parent = '') { | ||
if (!validator_1.default.isInt(String(value))) { | ||
validateInt(name, value, fieldErrors, isBodyParam, validators, parent = '') { | ||
if (!this.hasCorrectJsType(value, 'number', isBodyParam) || !validator_1.default.isInt(String(value))) { | ||
let message = `invalid integer number`; | ||
@@ -190,4 +194,4 @@ if (validators) { | ||
} | ||
validateFloat(name, value, fieldErrors, validators, parent = '') { | ||
if (!validator_1.default.isFloat(String(value))) { | ||
validateFloat(name, value, fieldErrors, isBodyParam, validators, parent = '') { | ||
if (!this.hasCorrectJsType(value, 'number', isBodyParam) || !validator_1.default.isFloat(String(value))) { | ||
let message = 'invalid float number'; | ||
@@ -251,4 +255,4 @@ if (validators) { | ||
} | ||
validateDate(name, value, fieldErrors, validators, parent = '') { | ||
if (!validator_1.default.isISO8601(String(value), { strict: true })) { | ||
validateDate(name, value, fieldErrors, isBodyParam, validators, parent = '') { | ||
if (!this.hasCorrectJsType(value, 'string', isBodyParam) || !validator_1.default.isISO8601(String(value), { strict: true })) { | ||
const message = validators && validators.isDate && validators.isDate.errorMsg ? validators.isDate.errorMsg : `invalid ISO 8601 date format, i.e. YYYY-MM-DD`; | ||
@@ -287,4 +291,4 @@ fieldErrors[parent + name] = { | ||
} | ||
validateDateTime(name, value, fieldErrors, validators, parent = '') { | ||
if (!validator_1.default.isISO8601(String(value), { strict: true })) { | ||
validateDateTime(name, value, fieldErrors, isBodyParam, validators, parent = '') { | ||
if (!this.hasCorrectJsType(value, 'string', isBodyParam) || !validator_1.default.isISO8601(String(value), { strict: true })) { | ||
const message = validators && validators.isDateTime && validators.isDateTime.errorMsg ? validators.isDateTime.errorMsg : `invalid ISO 8601 datetime format, i.e. YYYY-MM-DDTHH:mm:ss`; | ||
@@ -365,15 +369,17 @@ fieldErrors[parent + name] = { | ||
} | ||
validateBool(name, value, fieldErrors, validators, parent = '') { | ||
if (value === undefined || value === null) { | ||
return false; | ||
} | ||
validateBool(name, value, fieldErrors, isBodyParam, validators, parent = '') { | ||
if (value === true || value === false) { | ||
return value; | ||
} | ||
if (String(value).toLowerCase() === 'true') { | ||
return true; | ||
if (!isBodyParam || this.config.bodyCoercion === true) { | ||
if (value === undefined || value === null) { | ||
return false; | ||
} | ||
if (String(value).toLowerCase() === 'true') { | ||
return true; | ||
} | ||
if (String(value).toLowerCase() === 'false') { | ||
return false; | ||
} | ||
} | ||
if (String(value).toLowerCase() === 'false') { | ||
return false; | ||
} | ||
const message = validators && validators.isBoolean && validators.isBoolean.errorMsg ? validators.isBoolean.errorMsg : `invalid boolean value`; | ||
@@ -397,4 +403,4 @@ fieldErrors[parent + name] = { | ||
} | ||
validateArray(name, value, fieldErrors, swaggerConfig, schema, validators, parent = '') { | ||
if (!schema || value === undefined) { | ||
validateArray(name, value, fieldErrors, isBodyParam, schema, validators, parent = '') { | ||
if ((isBodyParam && this.config.bodyCoercion === false && !Array.isArray(value)) || !schema || value === undefined) { | ||
const message = validators && validators.isArray && validators.isArray.errorMsg ? validators.isArray.errorMsg : `invalid array`; | ||
@@ -411,7 +417,7 @@ fieldErrors[parent + name] = { | ||
arrayValue = value.map((elementValue, index) => { | ||
return this.ValidateParam(schema, elementValue, `$${index}`, fieldErrors, name + '.', swaggerConfig); | ||
return this.ValidateParam(schema, elementValue, `$${index}`, fieldErrors, isBodyParam, name + '.'); | ||
}); | ||
} | ||
else { | ||
arrayValue = [this.ValidateParam(schema, value, '$0', fieldErrors, name + '.', swaggerConfig)]; | ||
arrayValue = [this.ValidateParam(schema, value, '$0', fieldErrors, isBodyParam, name + '.')]; | ||
} | ||
@@ -460,3 +466,3 @@ if (Object.keys(fieldErrors).length > previousErrors) { | ||
} | ||
validateUnion(name, value, fieldErrors, swaggerConfig, property, parent = '') { | ||
validateUnion(name, value, fieldErrors, isBodyParam, property, parent = '') { | ||
if (!property.subSchemas) { | ||
@@ -473,3 +479,3 @@ throw new Error('internal tsoa error: ' + | ||
const validateableValue = value ? JSON.parse(JSON.stringify(value)) : value; | ||
const cleanValue = this.ValidateParam({ ...subSchema, validators: { ...property.validators, ...subSchema.validators } }, validateableValue, name, subFieldError, parent, swaggerConfig); | ||
const cleanValue = this.ValidateParam({ ...subSchema, validators: { ...property.validators, ...subSchema.validators } }, validateableValue, name, subFieldError, isBodyParam, parent); | ||
subFieldErrors.push(subFieldError); | ||
@@ -486,3 +492,3 @@ if (Object.keys(subFieldError).length === 0) { | ||
} | ||
validateIntersection(name, value, fieldErrors, swaggerConfig, subSchemas, parent = '') { | ||
validateIntersection(name, value, fieldErrors, isBodyParam, subSchemas, parent = '') { | ||
if (!subSchemas) { | ||
@@ -497,3 +503,6 @@ throw new Error('internal tsoa error: ' + | ||
const subFieldError = {}; | ||
const cleanValue = this.ValidateParam(subSchema, JSON.parse(JSON.stringify(value)), name, subFieldError, parent, { noImplicitAdditionalProperties: 'silently-remove-extras' }); | ||
const cleanValue = new ValidationService(this.models, { | ||
noImplicitAdditionalProperties: 'silently-remove-extras', | ||
bodyCoercion: this.config.bodyCoercion, | ||
}).ValidateParam(subSchema, JSON.parse(JSON.stringify(value)), name, subFieldError, isBodyParam, parent); | ||
cleanValues = { | ||
@@ -516,3 +525,6 @@ ...cleanValues, | ||
const requiredPropError = {}; | ||
this.validateModel({ | ||
new ValidationService(this.models, { | ||
noImplicitAdditionalProperties: 'ignore', | ||
bodyCoercion: this.config.bodyCoercion, | ||
}).validateModel({ | ||
name, | ||
@@ -522,5 +534,3 @@ value: JSON.parse(JSON.stringify(value)), | ||
fieldErrors: requiredPropError, | ||
minimalSwaggerConfig: { | ||
noImplicitAdditionalProperties: 'ignore', | ||
}, | ||
isBodyParam, | ||
}); | ||
@@ -530,6 +540,6 @@ return requiredPropError; | ||
const schemasWithRequiredProps = schemas.filter(schema => Object.keys(getRequiredPropError(schema)).length === 0); | ||
if (swaggerConfig.noImplicitAdditionalProperties === 'ignore') { | ||
if (this.config.noImplicitAdditionalProperties === 'ignore') { | ||
return { ...value, ...cleanValues }; | ||
} | ||
if (swaggerConfig.noImplicitAdditionalProperties === 'silently-remove-extras') { | ||
if (this.config.noImplicitAdditionalProperties === 'silently-remove-extras') { | ||
if (schemasWithRequiredProps.length > 0) { | ||
@@ -546,3 +556,3 @@ return cleanValues; | ||
} | ||
if (schemasWithRequiredProps.length > 0 && schemasWithRequiredProps.some(schema => this.getExcessPropertiesFor(schema, Object.keys(value), swaggerConfig).length === 0)) { | ||
if (schemasWithRequiredProps.length > 0 && schemasWithRequiredProps.some(schema => this.getExcessPropertiesFor(schema, Object.keys(value)).length === 0)) { | ||
return cleanValues; | ||
@@ -640,3 +650,3 @@ } | ||
} | ||
getExcessPropertiesFor(modelDefinition, properties, config) { | ||
getExcessPropertiesFor(modelDefinition, properties) { | ||
const modelProperties = new Set(Object.keys(modelDefinition.properties)); | ||
@@ -646,3 +656,3 @@ if (modelDefinition.additionalProperties) { | ||
} | ||
else if (config.noImplicitAdditionalProperties === 'ignore') { | ||
else if (this.config.noImplicitAdditionalProperties === 'ignore') { | ||
return []; | ||
@@ -655,3 +665,3 @@ } | ||
validateModel(input) { | ||
const { name, value, modelDefinition, fieldErrors, parent = '', minimalSwaggerConfig: swaggerConfig } = input; | ||
const { name, value, modelDefinition, fieldErrors, isBodyParam, parent = '' } = input; | ||
const previousErrors = Object.keys(fieldErrors).length; | ||
@@ -663,3 +673,3 @@ if (modelDefinition) { | ||
if (modelDefinition.dataType === 'refAlias') { | ||
return this.ValidateParam(modelDefinition.type, value, name, fieldErrors, parent, swaggerConfig); | ||
return this.ValidateParam(modelDefinition.type, value, name, fieldErrors, isBodyParam, parent); | ||
} | ||
@@ -678,3 +688,3 @@ const fieldPath = parent + name; | ||
Object.entries(properties).forEach(([key, property]) => { | ||
const validatedParam = this.ValidateParam(property, value[key], key, fieldErrors, fieldPath + '.', swaggerConfig); | ||
const validatedParam = this.ValidateParam(property, value[key], key, fieldErrors, isBodyParam, fieldPath + '.'); | ||
// Add value from validator if it's not undefined or if value is required and unfedined is valid type | ||
@@ -695,3 +705,3 @@ if (validatedParam !== undefined || (property.dataType === 'undefined' && property.required)) { | ||
if (isAnExcessProperty(key)) { | ||
if (swaggerConfig.noImplicitAdditionalProperties === 'throw-on-extras') { | ||
if (this.config.noImplicitAdditionalProperties === 'throw-on-extras') { | ||
fieldErrors[`${fieldPath}.${key}`] = { | ||
@@ -702,10 +712,10 @@ message: `"${key}" is an excess property and therefore is not allowed`, | ||
} | ||
else if (swaggerConfig.noImplicitAdditionalProperties === 'silently-remove-extras') { | ||
else if (this.config.noImplicitAdditionalProperties === 'silently-remove-extras') { | ||
delete value[key]; | ||
} | ||
else if (swaggerConfig.noImplicitAdditionalProperties === 'ignore') { | ||
else if (this.config.noImplicitAdditionalProperties === 'ignore') { | ||
// then it's okay to have additionalProperties | ||
} | ||
else { | ||
(0, assertNever_1.assertNever)(swaggerConfig.noImplicitAdditionalProperties); | ||
(0, assertNever_1.assertNever)(this.config.noImplicitAdditionalProperties); | ||
} | ||
@@ -718,3 +728,3 @@ } | ||
if (isAnExcessProperty(key)) { | ||
const validatedValue = this.ValidateParam(additionalProperties, value[key], key, fieldErrors, fieldPath + '.', swaggerConfig); | ||
const validatedValue = this.ValidateParam(additionalProperties, value[key], key, fieldErrors, isBodyParam, fieldPath + '.'); | ||
// Add value from validator if it's not undefined or if value is required and unfedined is valid type | ||
@@ -721,0 +731,0 @@ if (validatedValue !== undefined || (additionalProperties.dataType === 'undefined' && additionalProperties.required)) { |
@@ -25,5 +25,2 @@ import { Request as ExRequest, Response as ExResponse, NextFunction as ExNext } from 'express'; | ||
export declare class ExpressTemplateService extends TemplateService<ExpressApiHandlerParameters, ExpressValidationArgsParameters, ExpressReturnHandlerParameters> { | ||
readonly models: any; | ||
private readonly minimalSwaggerConfig; | ||
constructor(models: any, minimalSwaggerConfig: any); | ||
apiHandler(params: ExpressApiHandlerParameters): Promise<void>; | ||
@@ -30,0 +27,0 @@ getValidatedArgs(params: ExpressValidationArgsParameters): any[]; |
@@ -7,7 +7,2 @@ "use strict"; | ||
class ExpressTemplateService extends templateService_1.TemplateService { | ||
constructor(models, minimalSwaggerConfig) { | ||
super(models); | ||
this.models = models; | ||
this.minimalSwaggerConfig = minimalSwaggerConfig; | ||
} | ||
async apiHandler(params) { | ||
@@ -41,16 +36,16 @@ const { methodName, controller, response, validatedArgs, successStatus, next } = params; | ||
const value = descriptor ? descriptor.value : undefined; | ||
return this.validationService.ValidateParam(param, value, name, fieldErrors, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, value, name, fieldErrors, false, undefined); | ||
} | ||
case 'query': | ||
return this.validationService.ValidateParam(param, request.query[name], name, fieldErrors, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, request.query[name], name, fieldErrors, false, undefined); | ||
case 'queries': | ||
return this.validationService.ValidateParam(param, request.query, name, fieldErrors, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, request.query, name, fieldErrors, false, undefined); | ||
case 'path': | ||
return this.validationService.ValidateParam(param, request.params[name], name, fieldErrors, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, request.params[name], name, fieldErrors, false, undefined); | ||
case 'header': | ||
return this.validationService.ValidateParam(param, request.header(name), name, fieldErrors, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, request.header(name), name, fieldErrors, false, undefined); | ||
case 'body': | ||
return this.validationService.ValidateParam(param, request.body, name, fieldErrors, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, request.body, name, fieldErrors, true, undefined); | ||
case 'body-prop': | ||
return this.validationService.ValidateParam(param, request.body[name], name, fieldErrors, 'body.', this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, request.body[name], name, fieldErrors, true, 'body.'); | ||
case 'formData': { | ||
@@ -63,9 +58,9 @@ const files = Object.values(args).filter(param => param.dataType === 'file'); | ||
} | ||
const fileArgs = this.validationService.ValidateParam(param, requestFiles[name], name, fieldErrors, undefined, this.minimalSwaggerConfig); | ||
const fileArgs = this.validationService.ValidateParam(param, requestFiles[name], name, fieldErrors, false, undefined); | ||
return fileArgs.length === 1 ? fileArgs[0] : fileArgs; | ||
} | ||
else if (param.dataType === 'array' && param.array && param.array.dataType === 'file') { | ||
return this.validationService.ValidateParam(param, request.files, name, fieldErrors, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, request.files, name, fieldErrors, false, undefined); | ||
} | ||
return this.validationService.ValidateParam(param, request.body[name], name, fieldErrors, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, request.body[name], name, fieldErrors, false, undefined); | ||
} | ||
@@ -72,0 +67,0 @@ case 'res': |
@@ -5,2 +5,3 @@ import { Request as HRequest, ResponseToolkit as HResponse } from '@hapi/hapi'; | ||
import { TemplateService } from '../templateService'; | ||
import { AdditionalProps } from '../../additionalProps'; | ||
type HapiApiHandlerParameters = { | ||
@@ -25,6 +26,6 @@ methodName: string; | ||
export declare class HapiTemplateService extends TemplateService<HapiApiHandlerParameters, HapiValidationArgsParameters, HapiReturnHandlerParameters> { | ||
readonly models: any; | ||
private readonly minimalSwaggerConfig; | ||
protected readonly models: TsoaRoute.Models; | ||
protected readonly config: AdditionalProps; | ||
private readonly hapi; | ||
constructor(models: any, minimalSwaggerConfig: any, hapi: { | ||
constructor(models: TsoaRoute.Models, config: AdditionalProps, hapi: { | ||
boomify: Function; | ||
@@ -31,0 +32,0 @@ isBoom: Function; |
@@ -8,6 +8,6 @@ "use strict"; | ||
class HapiTemplateService extends templateService_1.TemplateService { | ||
constructor(models, minimalSwaggerConfig, hapi) { | ||
super(models); | ||
constructor(models, config, hapi) { | ||
super(models, config); | ||
this.models = models; | ||
this.minimalSwaggerConfig = minimalSwaggerConfig; | ||
this.config = config; | ||
this.hapi = hapi; | ||
@@ -52,18 +52,18 @@ } | ||
const value = descriptor ? descriptor.value : undefined; | ||
return this.validationService.ValidateParam(param, value, name, errorFields, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, value, name, errorFields, false, undefined); | ||
} | ||
case 'query': | ||
return this.validationService.ValidateParam(param, request.query[name], name, errorFields, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, request.query[name], name, errorFields, false, undefined); | ||
case 'queries': | ||
return this.validationService.ValidateParam(param, request.query, name, errorFields, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, request.query, name, errorFields, false, undefined); | ||
case 'path': | ||
return this.validationService.ValidateParam(param, request.params[name], name, errorFields, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, request.params[name], name, errorFields, false, undefined); | ||
case 'header': | ||
return this.validationService.ValidateParam(param, request.headers[name], name, errorFields, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, request.headers[name], name, errorFields, false, undefined); | ||
case 'body': | ||
return this.validationService.ValidateParam(param, request.payload, name, errorFields, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, request.payload, name, errorFields, true, undefined); | ||
case 'body-prop': { | ||
const descriptor = Object.getOwnPropertyDescriptor(request.payload, name); | ||
const value = descriptor ? descriptor.value : undefined; | ||
return this.validationService.ValidateParam(param, value, name, errorFields, 'body.', this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, value, name, errorFields, true, 'body.'); | ||
} | ||
@@ -73,3 +73,3 @@ case 'formData': { | ||
const value = descriptor ? descriptor.value : undefined; | ||
return this.validationService.ValidateParam(param, value, name, errorFields, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, value, name, errorFields, false, undefined); | ||
} | ||
@@ -76,0 +76,0 @@ case 'res': |
@@ -25,5 +25,2 @@ import type { Context, Next } from 'koa'; | ||
export declare class KoaTemplateService extends TemplateService<KoaApiHandlerParameters, KoaValidationArgsParameters, KoaReturnHandlerParameters> { | ||
readonly models: any; | ||
private readonly minimalSwaggerConfig; | ||
constructor(models: any, minimalSwaggerConfig: any); | ||
apiHandler(params: KoaApiHandlerParameters): Promise<any>; | ||
@@ -30,0 +27,0 @@ getValidatedArgs(params: KoaValidationArgsParameters): any[]; |
@@ -8,7 +8,2 @@ "use strict"; | ||
class KoaTemplateService extends templateService_1.TemplateService { | ||
constructor(models, minimalSwaggerConfig) { | ||
super(models); | ||
this.models = models; | ||
this.minimalSwaggerConfig = minimalSwaggerConfig; | ||
} | ||
async apiHandler(params) { | ||
@@ -43,16 +38,16 @@ const { methodName, controller, context, validatedArgs, successStatus } = params; | ||
const value = descriptor ? descriptor.value : undefined; | ||
return this.validationService.ValidateParam(param, value, name, errorFields, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, value, name, errorFields, false, undefined); | ||
} | ||
case 'query': | ||
return this.validationService.ValidateParam(param, context.request.query[name], name, errorFields, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, context.request.query[name], name, errorFields, false, undefined); | ||
case 'queries': | ||
return this.validationService.ValidateParam(param, context.request.query, name, errorFields, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, context.request.query, name, errorFields, false, undefined); | ||
case 'path': | ||
return this.validationService.ValidateParam(param, context.params[name], name, errorFields, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, context.params[name], name, errorFields, false, undefined); | ||
case 'header': | ||
return this.validationService.ValidateParam(param, context.request.headers[name], name, errorFields, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, context.request.headers[name], name, errorFields, false, undefined); | ||
case 'body': { | ||
const descriptor = Object.getOwnPropertyDescriptor(context.request, 'body'); | ||
const value = descriptor ? descriptor.value : undefined; | ||
return this.validationService.ValidateParam(param, value, name, errorFields, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, value, name, errorFields, true, undefined); | ||
} | ||
@@ -62,3 +57,3 @@ case 'body-prop': { | ||
const value = descriptor ? descriptor.value[name] : undefined; | ||
return this.validationService.ValidateParam(param, value, name, errorFields, 'body.', this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, value, name, errorFields, true, 'body.'); | ||
} | ||
@@ -72,9 +67,9 @@ case 'formData': { | ||
} | ||
const fileArgs = this.validationService.ValidateParam(param, contextRequest.files[name], name, errorFields, undefined, this.minimalSwaggerConfig); | ||
const fileArgs = this.validationService.ValidateParam(param, contextRequest.files[name], name, errorFields, false, undefined); | ||
return fileArgs.length === 1 ? fileArgs[0] : fileArgs; | ||
} | ||
else if (param.dataType === 'array' && param.array && param.array.dataType === 'file') { | ||
return this.validationService.ValidateParam(param, contextRequest.files, name, errorFields, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, contextRequest.files, name, errorFields, false, undefined); | ||
} | ||
return this.validationService.ValidateParam(param, contextRequest.body[name], name, errorFields, undefined, this.minimalSwaggerConfig); | ||
return this.validationService.ValidateParam(param, contextRequest.body[name], name, errorFields, false, undefined); | ||
} | ||
@@ -81,0 +76,0 @@ case 'res': |
import { Controller } from '../../interfaces/controller'; | ||
import { TsoaRoute } from '../tsoa-route'; | ||
import { ValidationService } from '../templateHelpers'; | ||
import { AdditionalProps } from '../additionalProps'; | ||
export declare abstract class TemplateService<ApiHandlerParameters, ValidationArgsParameters, ReturnHandlerParameters> { | ||
protected readonly models: TsoaRoute.Models; | ||
protected readonly config: AdditionalProps; | ||
protected validationService: ValidationService; | ||
constructor(models: TsoaRoute.Models); | ||
constructor(models: TsoaRoute.Models, config: AdditionalProps); | ||
abstract apiHandler(params: ApiHandlerParameters): Promise<any>; | ||
@@ -9,0 +11,0 @@ abstract getValidatedArgs(params: ValidationArgsParameters): any[]; |
@@ -6,5 +6,6 @@ "use strict"; | ||
class TemplateService { | ||
constructor(models) { | ||
constructor(models, config) { | ||
this.models = models; | ||
this.validationService = new templateHelpers_1.ValidationService(models); | ||
this.config = config; | ||
this.validationService = new templateHelpers_1.ValidationService(models, config); | ||
} | ||
@@ -11,0 +12,0 @@ isController(object) { |
{ | ||
"name": "@tsoa/runtime", | ||
"description": "Build swagger-compliant REST APIs using TypeScript and Node", | ||
"version": "6.1.5", | ||
"version": "6.2.0", | ||
"main": "./dist/index.js", | ||
@@ -56,3 +56,3 @@ "typings": "./dist/index.d.ts", | ||
}, | ||
"gitHead": "efdb468a5cedcb9bef345f1eed776e8fc621b443" | ||
"gitHead": "b0187ed369c7e89f20e51ea30609d9494562fc8f" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
169999
3010