diff --git a/packages/rest/src/coercion/coerce-parameter.ts b/packages/rest/src/coercion/coerce-parameter.ts index 9d3819549f49..b4cff64410e5 100644 --- a/packages/rest/src/coercion/coerce-parameter.ts +++ b/packages/rest/src/coercion/coerce-parameter.ts @@ -6,7 +6,7 @@ import {ParameterObject, isReferenceObject} from '@loopback/openapi-v3-types'; import {Validator} from './validator'; import * as debugModule from 'debug'; -import {HttpErrorMessage} from '../'; +import {RestHttpError} from '../'; const debug = debugModule('loopback:rest:coercion'); @@ -43,9 +43,8 @@ export function coerceParameter(data: string, spec: ParameterObject) { case 'number': const coercedData = data ? Number(data) : undefined; if (coercedData === undefined) return; - if (isNaN(coercedData)) - throw HttpErrorMessage.invalidData(data, spec.name); - return coercedData; + if (isNaN(coercedData)) throw RestHttpError.invalidData(data, spec.name); + return coercedData; case 'long': return Number(data); case 'integer': diff --git a/packages/rest/src/coercion/http-error-message.ts b/packages/rest/src/coercion/rest-http-error.ts similarity index 91% rename from packages/rest/src/coercion/http-error-message.ts rename to packages/rest/src/coercion/rest-http-error.ts index a1d1c43fa7d0..211f67f0f8fb 100644 --- a/packages/rest/src/coercion/http-error-message.ts +++ b/packages/rest/src/coercion/rest-http-error.ts @@ -1,5 +1,5 @@ import * as HttpErrors from 'http-errors'; -export namespace HttpErrorMessage { +export namespace RestHttpError { export function invalidData(data: T, name: string) { const msg = `Invalid data ${JSON.stringify(data)} for parameter ${name}!`; return new HttpErrors.BadRequest(msg); diff --git a/packages/rest/src/coercion/validator.ts b/packages/rest/src/coercion/validator.ts index fe092143e982..85c62d57f5e5 100644 --- a/packages/rest/src/coercion/validator.ts +++ b/packages/rest/src/coercion/validator.ts @@ -4,7 +4,7 @@ // License text available at https://opensource.org/licenses/MIT import {ParameterObject} from '@loopback/openapi-v3-types'; -import {HttpErrorMessage} from '../'; +import {RestHttpError} from '../'; /** * A set of options to pass into the validator functions @@ -42,7 +42,7 @@ export class Validator { if (this.isAbsent(value)) { if (this.isRequired(opts)) { const name = this.ctx.parameterSpec.name; - throw HttpErrorMessage.missingRequired(name); + throw RestHttpError.missingRequired(name); } else { return; } diff --git a/packages/rest/src/index.ts b/packages/rest/src/index.ts index 16a775c8cb62..7137560ed2d6 100644 --- a/packages/rest/src/index.ts +++ b/packages/rest/src/index.ts @@ -39,4 +39,4 @@ export * from './rest.component'; export * from './rest.server'; export * from './sequence'; export * from '@loopback/openapi-v3'; -export * from './coercion/http-error-message'; +export * from './coercion/rest-http-error'; diff --git a/packages/rest/test/unit/coercion/paramStringToNumber.unit.ts b/packages/rest/test/unit/coercion/paramStringToNumber.unit.ts index ddc52ac9c77b..8262cd5afef4 100644 --- a/packages/rest/test/unit/coercion/paramStringToNumber.unit.ts +++ b/packages/rest/test/unit/coercion/paramStringToNumber.unit.ts @@ -6,7 +6,7 @@ import {test} from './utils'; import {ParameterLocation} from '@loopback/openapi-v3-types'; import * as HttpErrors from 'http-errors'; -import {HttpErrorMessage} from './../../../'; +import {RestHttpError} from './../../../'; const NUMBER_PARAM = { in: 'path', @@ -48,10 +48,10 @@ describe('coerce param from string to number - required', () => { context('empty values trigger ERROR_BAD_REQUEST', () => { // null, '' sent from request are converted to raw value '' - test( + test( REQUIRED_NUMBER_PARAM, '', - HttpErrorMessage.missingRequired(REQUIRED_NUMBER_PARAM.name), + RestHttpError.missingRequired(REQUIRED_NUMBER_PARAM.name), ); }); }); @@ -85,13 +85,13 @@ describe('coerce param from string to number - optional', () => { test( NUMBER_PARAM, 'text', - HttpErrorMessage.invalidData('text', NUMBER_PARAM.name), + RestHttpError.invalidData('text', NUMBER_PARAM.name), ); // {a: true}, [1,2] are converted to object test( NUMBER_PARAM, {a: true}, - HttpErrorMessage.invalidData({a: true}, NUMBER_PARAM.name), + RestHttpError.invalidData({a: true}, NUMBER_PARAM.name), ); }); });