@loopback/rest 13.1.0 → 13.1.1
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.
|
@@ -7,11 +7,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
7
7
|
exports.validateValueAgainstSchema = exports.validateRequestBody = void 0;
|
|
8
8
|
const tslib_1 = require("tslib");
|
|
9
9
|
const openapi_v3_1 = require("@loopback/openapi-v3");
|
|
10
|
+
const node_util_1 = tslib_1.__importDefault(require("node:util"));
|
|
10
11
|
const debug_1 = tslib_1.__importDefault(require("debug"));
|
|
11
|
-
const util_1 = tslib_1.__importDefault(require("util"));
|
|
12
12
|
const __1 = require("..");
|
|
13
13
|
const ajv_factory_provider_1 = require("./ajv-factory.provider");
|
|
14
|
-
const toJsonSchema = require('@openapi-contrib/openapi-schema-to-json-schema');
|
|
14
|
+
const { openapiSchemaToJsonSchema: toJsonSchema, } = require('@openapi-contrib/openapi-schema-to-json-schema');
|
|
15
15
|
const debug = (0, debug_1.default)('loopback:rest:validation');
|
|
16
16
|
/**
|
|
17
17
|
* Check whether the request body is valid according to the provided OpenAPI schema.
|
|
@@ -26,23 +26,22 @@ const debug = (0, debug_1.default)('loopback:rest:validation');
|
|
|
26
26
|
async function validateRequestBody(body, requestBodySpec, globalSchemas = {}, options = ajv_factory_provider_1.DEFAULT_AJV_VALIDATION_OPTIONS) {
|
|
27
27
|
const required = requestBodySpec === null || requestBodySpec === void 0 ? void 0 : requestBodySpec.required;
|
|
28
28
|
if (required && body.value == null) {
|
|
29
|
-
|
|
29
|
+
throw Object.assign(new __1.HttpErrors.BadRequest('Request body is required'), {
|
|
30
30
|
code: 'MISSING_REQUIRED_PARAMETER',
|
|
31
31
|
parameterName: 'request body',
|
|
32
32
|
});
|
|
33
|
-
throw err;
|
|
34
33
|
}
|
|
35
34
|
if (!required && !body.value)
|
|
36
35
|
return;
|
|
37
36
|
const schema = body.schema;
|
|
38
37
|
/* istanbul ignore if */
|
|
39
38
|
if (debug.enabled) {
|
|
40
|
-
debug('Request body schema:',
|
|
39
|
+
debug('Request body schema:', node_util_1.default.inspect(schema, { depth: null }));
|
|
41
40
|
if (schema &&
|
|
42
41
|
(0, openapi_v3_1.isReferenceObject)(schema) &&
|
|
43
42
|
schema.$ref.startsWith('#/components/schemas/')) {
|
|
44
43
|
const ref = schema.$ref.slice('#/components/schemas/'.length);
|
|
45
|
-
debug(' referencing:',
|
|
44
|
+
debug(' referencing:', node_util_1.default.inspect(globalSchemas[ref], { depth: null }));
|
|
46
45
|
}
|
|
47
46
|
}
|
|
48
47
|
if (!schema)
|
|
@@ -63,7 +62,7 @@ function convertToJsonSchema(openapiSchema) {
|
|
|
63
62
|
delete jsonSchema['$schema'];
|
|
64
63
|
/* istanbul ignore if */
|
|
65
64
|
if (debug.enabled) {
|
|
66
|
-
debug('Converted OpenAPI schema to JSON schema: %s',
|
|
65
|
+
debug('Converted OpenAPI schema to JSON schema: %s', node_util_1.default.inspect(jsonSchema, { depth: null }));
|
|
67
66
|
}
|
|
68
67
|
return jsonSchema;
|
|
69
68
|
}
|
|
@@ -124,21 +123,19 @@ value, schema, globalSchemas = {}, options = {}) {
|
|
|
124
123
|
}
|
|
125
124
|
/* istanbul ignore if */
|
|
126
125
|
if (debug.enabled) {
|
|
127
|
-
debug('Invalid value: %s. Errors: %s',
|
|
126
|
+
debug('Invalid value: %s. Errors: %s', node_util_1.default.inspect(value, { depth: null }), node_util_1.default.inspect(validationErrors));
|
|
128
127
|
}
|
|
129
128
|
if (typeof options.ajvErrorTransformer === 'function') {
|
|
130
129
|
validationErrors = options.ajvErrorTransformer(validationErrors);
|
|
131
130
|
}
|
|
132
131
|
// Throw invalid request body error
|
|
133
132
|
if (options.source === 'body') {
|
|
134
|
-
|
|
135
|
-
throw error;
|
|
133
|
+
throw __1.RestHttpErrors.invalidRequestBody(buildErrorDetails(validationErrors));
|
|
136
134
|
}
|
|
137
135
|
// Throw invalid value error
|
|
138
|
-
|
|
136
|
+
throw __1.RestHttpErrors.invalidData(value, (_c = options.name) !== null && _c !== void 0 ? _c : '(unknown)', {
|
|
139
137
|
details: buildErrorDetails(validationErrors),
|
|
140
138
|
});
|
|
141
|
-
throw error;
|
|
142
139
|
}
|
|
143
140
|
exports.validateValueAgainstSchema = validateValueAgainstSchema;
|
|
144
141
|
function buildErrorDetails(validationErrors) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"request-body.validator.js","sourceRoot":"","sources":["../../src/validation/request-body.validator.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,8BAA8B;AAC9B,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,qDAM8B;
|
|
1
|
+
{"version":3,"file":"request-body.validator.js","sourceRoot":"","sources":["../../src/validation/request-body.validator.ts"],"names":[],"mappings":";AAAA,gFAAgF;AAChF,8BAA8B;AAC9B,+CAA+C;AAC/C,gEAAgE;;;;AAEhE,qDAM8B;AAC9B,kEAA6B;AAC7B,0DAAiC;AAGjC,0BAA2D;AAM3D,iEAGgC;AAEhC,MAAM,EACJ,yBAAyB,EAAE,YAAY,GACxC,GAAG,OAAO,CAAC,gDAAgD,CAAC,CAAC;AAE9D,MAAM,KAAK,GAAG,IAAA,eAAY,EAAC,0BAA0B,CAAC,CAAC;AAEvD;;;;;;;;;GASG;AACI,KAAK,UAAU,mBAAmB,CACvC,IAAiB,EACjB,eAAmC,EACnC,gBAA+B,EAAE,EACjC,UAA6B,qDAA8B;IAE3D,MAAM,QAAQ,GAAG,eAAe,aAAf,eAAe,uBAAf,eAAe,CAAE,QAAQ,CAAC;IAE3C,IAAI,QAAQ,IAAI,IAAI,CAAC,KAAK,IAAI,IAAI,EAAE;QAClC,MAAM,MAAM,CAAC,MAAM,CAAC,IAAI,cAAU,CAAC,UAAU,CAAC,0BAA0B,CAAC,EAAE;YACzE,IAAI,EAAE,4BAA4B;YAClC,aAAa,EAAE,cAAc;SAC9B,CAAC,CAAC;KACJ;IACD,IAAI,CAAC,Q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;AAhED,gEAgEC;AAED,SAAS,iBAAiB,CACxB,gBAA+B;IAE/B,OAAO,gBAAgB,CAAC,GAAG,CACzB,CAAC,CAAc,EAAyC,EAAE;;QACxD,OAAO;YACL,IAAI,EAAE,CAAC,CAAC,YAAY;YACpB,IAAI,EAAE,CAAC,CAAC,OAAO;YACf,OAAO,EAAE,MAAA,CAAC,CAAC,OAAO,mCAAI,6BAA6B,CAAC,CAAC,OAAO,EAAE;YAC9D,IAAI,EAAE,CAAC,CAAC,MAAM;SACf,CAAC;IACJ,CAAC,CACF,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAS,eAAe,CACtB,MAAoB,EACpB,gBAA+B,EAAE,EACjC,OAAY;IAEZ,MAAM,UAAU,GAAG,mBAAmB,CAAC,MAAM,CAAC,CAAC;IAE/C,kDAAkD;IAClD,MAAM,OAAO,GAAkB,EAAE,CAAC;IAClC,KAAK,MAAM,IAAI,IAAI,aAAa,EAAE;QAChC,8DAA8D;QAC9D,OAAO,CAAC,IAAI,CAAC,GAAG,EAAC,GAAG,aAAa,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,IAAI,EAAC,CAAC;KACxD;IACD,MAAM,aAAa,GAAgB,EAAC,UAAU,EAAE,EAAC,OAAO,EAAC,EAAE,GAAG,UAAU,EAAC,CAAC;IAE1E,mEAAmE;IACnE,aAAa,CAAC,MAAM,GAAG,IAAI,CAAC;IAE5B,OAAO,OAAO,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;AACxC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@loopback/rest",
|
|
3
3
|
"description": "Expose controllers as REST endpoints and route REST API requests to controller methods",
|
|
4
|
-
"version": "13.1.
|
|
4
|
+
"version": "13.1.1",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"types": "dist/index.d.ts",
|
|
@@ -38,10 +38,10 @@
|
|
|
38
38
|
"@loopback/core": "^5.0.0"
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@loopback/express": "^6.1.
|
|
42
|
-
"@loopback/http-server": "^5.1.
|
|
43
|
-
"@loopback/openapi-v3": "^9.1.
|
|
44
|
-
"@openapi-contrib/openapi-schema-to-json-schema": "^
|
|
41
|
+
"@loopback/express": "^6.1.1",
|
|
42
|
+
"@loopback/http-server": "^5.1.1",
|
|
43
|
+
"@loopback/openapi-v3": "^9.1.1",
|
|
44
|
+
"@openapi-contrib/openapi-schema-to-json-schema": "^4.0.5",
|
|
45
45
|
"@types/body-parser": "^1.19.2",
|
|
46
46
|
"@types/cors": "^2.8.13",
|
|
47
47
|
"@types/express": "^4.17.17",
|
|
@@ -65,27 +65,27 @@
|
|
|
65
65
|
"on-finished": "^2.4.1",
|
|
66
66
|
"path-to-regexp": "^6.2.1",
|
|
67
67
|
"qs": "^6.11.2",
|
|
68
|
-
"strong-error-handler": "^4.0.
|
|
68
|
+
"strong-error-handler": "^4.0.7",
|
|
69
69
|
"tslib": "^2.6.0",
|
|
70
70
|
"type-is": "^1.6.18",
|
|
71
71
|
"validator": "^13.9.0"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
|
-
"@loopback/build": "^10.1.
|
|
75
|
-
"@loopback/core": "^5.1.
|
|
76
|
-
"@loopback/eslint-config": "^14.0.
|
|
77
|
-
"@loopback/openapi-spec-builder": "^6.1.
|
|
78
|
-
"@loopback/repository": "^6.1.
|
|
79
|
-
"@loopback/testlab": "^6.1.
|
|
74
|
+
"@loopback/build": "^10.1.1",
|
|
75
|
+
"@loopback/core": "^5.1.1",
|
|
76
|
+
"@loopback/eslint-config": "^14.0.2",
|
|
77
|
+
"@loopback/openapi-spec-builder": "^6.1.1",
|
|
78
|
+
"@loopback/repository": "^6.1.1",
|
|
79
|
+
"@loopback/testlab": "^6.1.1",
|
|
80
80
|
"@types/debug": "^4.1.8",
|
|
81
81
|
"@types/js-yaml": "^4.0.5",
|
|
82
82
|
"@types/json-schema-compare": "^0.2.1",
|
|
83
83
|
"@types/lodash": "^4.14.195",
|
|
84
84
|
"@types/multer": "^1.4.7",
|
|
85
|
-
"@types/node": "^16.18.
|
|
85
|
+
"@types/node": "^16.18.38",
|
|
86
86
|
"@types/on-finished": "^2.3.1",
|
|
87
87
|
"@types/qs": "^6.9.7",
|
|
88
88
|
"multer": "^1.4.4"
|
|
89
89
|
},
|
|
90
|
-
"gitHead": "
|
|
90
|
+
"gitHead": "be0c53e30bbe9edf7753c4fcd7ab3199f7cd3b8c"
|
|
91
91
|
}
|
|
@@ -10,10 +10,10 @@ import {
|
|
|
10
10
|
SchemaObject,
|
|
11
11
|
SchemasObject,
|
|
12
12
|
} from '@loopback/openapi-v3';
|
|
13
|
-
import
|
|
14
|
-
import
|
|
15
|
-
import
|
|
16
|
-
import
|
|
13
|
+
import util from 'node:util';
|
|
14
|
+
import debugFactory from 'debug';
|
|
15
|
+
import Ajv, {AsyncSchema, AsyncValidateFunction, ErrorObject} from 'ajv';
|
|
16
|
+
import {AnyValidateFunction} from 'ajv/dist/types';
|
|
17
17
|
import {HttpErrors, RequestBody, RestHttpErrors} from '..';
|
|
18
18
|
import {
|
|
19
19
|
SchemaValidatorCache,
|
|
@@ -25,8 +25,11 @@ import {
|
|
|
25
25
|
DEFAULT_AJV_VALIDATION_OPTIONS,
|
|
26
26
|
} from './ajv-factory.provider';
|
|
27
27
|
|
|
28
|
-
const
|
|
29
|
-
|
|
28
|
+
const {
|
|
29
|
+
openapiSchemaToJsonSchema: toJsonSchema,
|
|
30
|
+
} = require('@openapi-contrib/openapi-schema-to-json-schema');
|
|
31
|
+
|
|
32
|
+
const debug = debugFactory('loopback:rest:validation');
|
|
30
33
|
|
|
31
34
|
/**
|
|
32
35
|
* Check whether the request body is valid according to the provided OpenAPI schema.
|
|
@@ -47,14 +50,10 @@ export async function validateRequestBody(
|
|
|
47
50
|
const required = requestBodySpec?.required;
|
|
48
51
|
|
|
49
52
|
if (required && body.value == null) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
parameterName: 'request body',
|
|
55
|
-
},
|
|
56
|
-
);
|
|
57
|
-
throw err;
|
|
53
|
+
throw Object.assign(new HttpErrors.BadRequest('Request body is required'), {
|
|
54
|
+
code: 'MISSING_REQUIRED_PARAMETER',
|
|
55
|
+
parameterName: 'request body',
|
|
56
|
+
});
|
|
58
57
|
}
|
|
59
58
|
if (!required && !body.value) return;
|
|
60
59
|
|
|
@@ -181,17 +180,15 @@ export async function validateValueAgainstSchema(
|
|
|
181
180
|
|
|
182
181
|
// Throw invalid request body error
|
|
183
182
|
if (options.source === 'body') {
|
|
184
|
-
|
|
183
|
+
throw RestHttpErrors.invalidRequestBody(
|
|
185
184
|
buildErrorDetails(validationErrors),
|
|
186
185
|
);
|
|
187
|
-
throw error;
|
|
188
186
|
}
|
|
189
187
|
|
|
190
188
|
// Throw invalid value error
|
|
191
|
-
|
|
189
|
+
throw RestHttpErrors.invalidData(value, options.name ?? '(unknown)', {
|
|
192
190
|
details: buildErrorDetails(validationErrors),
|
|
193
191
|
});
|
|
194
|
-
throw error;
|
|
195
192
|
}
|
|
196
193
|
|
|
197
194
|
function buildErrorDetails(
|