@middy/validator 6.1.5 → 6.2.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/index.d.ts +8 -8
- package/index.js +63 -63
- package/package.json +85 -90
- package/transpile.d.ts +7 -6
- package/transpile.js +28 -28
package/index.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import middy from
|
|
1
|
+
import type middy from "@middy/core";
|
|
2
2
|
|
|
3
3
|
interface Options {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
eventSchema?: Function | any;
|
|
5
|
+
contextSchema?: Function | any;
|
|
6
|
+
responseSchema?: Function | any;
|
|
7
|
+
defaultLanguage?: string;
|
|
8
|
+
languages?: object | any;
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
declare function validator
|
|
11
|
+
declare function validator(options?: Options): middy.MiddlewareObj;
|
|
12
12
|
|
|
13
|
-
export default validator
|
|
13
|
+
export default validator;
|
package/index.js
CHANGED
|
@@ -1,75 +1,75 @@
|
|
|
1
|
-
import { createError } from
|
|
1
|
+
import { createError } from "@middy/util";
|
|
2
2
|
|
|
3
3
|
const defaults = {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
4
|
+
eventSchema: undefined,
|
|
5
|
+
contextSchema: undefined,
|
|
6
|
+
responseSchema: undefined,
|
|
7
|
+
defaultLanguage: "en",
|
|
8
|
+
languages: {},
|
|
9
|
+
};
|
|
10
10
|
|
|
11
11
|
const validatorMiddleware = (opts = {}) => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
12
|
+
const {
|
|
13
|
+
eventSchema,
|
|
14
|
+
contextSchema,
|
|
15
|
+
responseSchema,
|
|
16
|
+
defaultLanguage,
|
|
17
|
+
languages,
|
|
18
|
+
} = { ...defaults, ...opts };
|
|
19
19
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
const validatorMiddlewareBefore = async (request) => {
|
|
21
|
+
if (eventSchema) {
|
|
22
|
+
const validEvent = await eventSchema(request.event);
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
24
|
+
if (!validEvent) {
|
|
25
|
+
const localize =
|
|
26
|
+
languages[request.context.preferredLanguage] ??
|
|
27
|
+
languages[defaultLanguage];
|
|
28
|
+
localize?.(eventSchema.errors);
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
30
|
+
// Bad Request
|
|
31
|
+
throw createError(400, "Event object failed validation", {
|
|
32
|
+
cause: {
|
|
33
|
+
package: "@middy/validator",
|
|
34
|
+
data: eventSchema.errors,
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
if (contextSchema) {
|
|
41
|
+
const validContext = await contextSchema(request.context);
|
|
42
42
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
43
|
+
if (!validContext) {
|
|
44
|
+
// Internal Server Error
|
|
45
|
+
throw createError(500, "Context object failed validation", {
|
|
46
|
+
cause: {
|
|
47
|
+
package: "@middy/validator",
|
|
48
|
+
data: contextSchema.errors,
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
54
|
|
|
55
|
-
|
|
56
|
-
|
|
55
|
+
const validatorMiddlewareAfter = async (request) => {
|
|
56
|
+
const validResponse = await responseSchema(request.response);
|
|
57
57
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}
|
|
58
|
+
if (!validResponse) {
|
|
59
|
+
// Internal Server Error
|
|
60
|
+
throw createError(500, "Response object failed validation", {
|
|
61
|
+
cause: {
|
|
62
|
+
package: "@middy/validator",
|
|
63
|
+
data: responseSchema.errors,
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
return {
|
|
69
|
+
before:
|
|
70
|
+
(eventSchema ?? contextSchema) ? validatorMiddlewareBefore : undefined,
|
|
71
|
+
after: responseSchema ? validatorMiddlewareAfter : undefined,
|
|
72
|
+
};
|
|
73
|
+
};
|
|
74
74
|
|
|
75
|
-
export default validatorMiddleware
|
|
75
|
+
export default validatorMiddleware;
|
package/package.json
CHANGED
|
@@ -1,92 +1,87 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
"@types/http-errors": "^2.0.0",
|
|
88
|
-
"ajv-bsontype": "^1.0.7",
|
|
89
|
-
"ajv-cmd": "^0.7.0"
|
|
90
|
-
},
|
|
91
|
-
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
2
|
+
"name": "@middy/validator",
|
|
3
|
+
"version": "6.2.0",
|
|
4
|
+
"description": "Validator middleware for the middy framework",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20"
|
|
8
|
+
},
|
|
9
|
+
"engineStrict": true,
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"module": "./index.js",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./index.d.ts",
|
|
18
|
+
"default": "./index.js"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"default": "./index.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"./transpile": {
|
|
25
|
+
"import": {
|
|
26
|
+
"types": "./transpile.d.ts",
|
|
27
|
+
"default": "./transpile.js"
|
|
28
|
+
},
|
|
29
|
+
"require": {
|
|
30
|
+
"default": "./transpile.js"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"types": "index.d.ts",
|
|
35
|
+
"files": ["index.js", "index.d.ts", "transpile.js", "transpile.d.ts"],
|
|
36
|
+
"scripts": {
|
|
37
|
+
"test": "npm run test:unit && npm run test:fuzz",
|
|
38
|
+
"test:unit": "node --test",
|
|
39
|
+
"test:fuzz": "node --test index.fuzz.js",
|
|
40
|
+
"test:perf": "node --test index.perf.js"
|
|
41
|
+
},
|
|
42
|
+
"license": "MIT",
|
|
43
|
+
"keywords": [
|
|
44
|
+
"Lambda",
|
|
45
|
+
"Middleware",
|
|
46
|
+
"Serverless",
|
|
47
|
+
"Framework",
|
|
48
|
+
"AWS",
|
|
49
|
+
"AWS Lambda",
|
|
50
|
+
"Middy",
|
|
51
|
+
"Validator",
|
|
52
|
+
"JSON Schema"
|
|
53
|
+
],
|
|
54
|
+
"author": {
|
|
55
|
+
"name": "Middy contributors",
|
|
56
|
+
"url": "https://github.com/middyjs/middy/graphs/contributors"
|
|
57
|
+
},
|
|
58
|
+
"repository": {
|
|
59
|
+
"type": "git",
|
|
60
|
+
"url": "git+https://github.com/middyjs/middy.git",
|
|
61
|
+
"directory": "packages/validator"
|
|
62
|
+
},
|
|
63
|
+
"bugs": {
|
|
64
|
+
"url": "https://github.com/middyjs/middy/issues"
|
|
65
|
+
},
|
|
66
|
+
"homepage": "https://middy.js.org",
|
|
67
|
+
"funding": {
|
|
68
|
+
"type": "github",
|
|
69
|
+
"url": "https://github.com/sponsors/willfarrell"
|
|
70
|
+
},
|
|
71
|
+
"dependencies": {
|
|
72
|
+
"@middy/util": "6.2.0",
|
|
73
|
+
"ajv": "8.17.1",
|
|
74
|
+
"ajv-errors": "3.0.0",
|
|
75
|
+
"ajv-formats": "3.0.1",
|
|
76
|
+
"ajv-formats-draft2019": "1.6.1",
|
|
77
|
+
"ajv-ftl-i18n": "0.1.1",
|
|
78
|
+
"ajv-keywords": "5.1.0"
|
|
79
|
+
},
|
|
80
|
+
"devDependencies": {
|
|
81
|
+
"@middy/core": "6.2.0",
|
|
82
|
+
"@types/http-errors": "^2.0.0",
|
|
83
|
+
"ajv-bsontype": "^1.0.7",
|
|
84
|
+
"ajv-cmd": "^0.7.0"
|
|
85
|
+
},
|
|
86
|
+
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
92
87
|
}
|
package/transpile.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import Ajv
|
|
1
|
+
import type Ajv from "ajv";
|
|
2
|
+
import type { Options as AjvOptions } from "ajv";
|
|
2
3
|
|
|
3
|
-
export function transpileSchema
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
): Ajv
|
|
4
|
+
export function transpileSchema(
|
|
5
|
+
schema: object,
|
|
6
|
+
ajvOptions?: Partial<AjvOptions>,
|
|
7
|
+
): Ajv;
|
|
7
8
|
|
|
8
|
-
export function transpileLocale
|
|
9
|
+
export function transpileLocale(src: string, options?: object | any): Function;
|
package/transpile.js
CHANGED
|
@@ -20,49 +20,49 @@ export const transpileSchema = (schema, ajvOptions) => {
|
|
|
20
20
|
export const transpileLocale = transpileFTL
|
|
21
21
|
*/
|
|
22
22
|
|
|
23
|
-
import
|
|
24
|
-
import ajvFormats from
|
|
25
|
-
import ajvFormatsDraft2019 from
|
|
26
|
-
import ajvKeywords from
|
|
27
|
-
import
|
|
23
|
+
import ajvErrors from "ajv-errors";
|
|
24
|
+
import ajvFormats from "ajv-formats";
|
|
25
|
+
import ajvFormatsDraft2019 from "ajv-formats-draft2019";
|
|
26
|
+
import ajvKeywords from "ajv-keywords";
|
|
27
|
+
import Ajv from "ajv/dist/2020.js";
|
|
28
28
|
|
|
29
|
-
import { transpile } from
|
|
29
|
+
import { transpile } from "ajv-ftl-i18n";
|
|
30
30
|
|
|
31
31
|
// import transpileFTL from 'ajv-cmd/ftl'
|
|
32
|
-
export const transpileFTL = transpile
|
|
32
|
+
export const transpileFTL = transpile;
|
|
33
33
|
|
|
34
34
|
// *** Start `ajv-cmd/compile` *** //
|
|
35
35
|
// import compileSchema from 'ajv-cmd/compile'
|
|
36
36
|
|
|
37
37
|
const instance = (options = {}) => {
|
|
38
|
-
|
|
38
|
+
Object.assign(options, { keywords: [] });
|
|
39
39
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
40
|
+
const ajv = new Ajv(options);
|
|
41
|
+
ajvFormats(ajv);
|
|
42
|
+
ajvFormatsDraft2019(ajv);
|
|
43
|
+
ajvKeywords(ajv);
|
|
44
|
+
ajvErrors(ajv);
|
|
45
|
+
return ajv;
|
|
46
|
+
};
|
|
47
47
|
|
|
48
48
|
const compileSchema = (schema, options = {}) => {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
49
|
+
Object.assign(options, { keywords: [] });
|
|
50
|
+
const ajv = instance(options);
|
|
51
|
+
return ajv.compile(schema);
|
|
52
|
+
};
|
|
53
53
|
// *** End `ajv-cmd/compile` *** //
|
|
54
54
|
|
|
55
55
|
const ajvDefaults = {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
56
|
+
strict: true,
|
|
57
|
+
coerceTypes: "array", // important for query string params
|
|
58
|
+
allErrors: true, // required for ajvErrors
|
|
59
|
+
useDefaults: "empty",
|
|
60
|
+
messages: true, // needs to be true to allow multi-locale errorMessage to work
|
|
61
|
+
};
|
|
62
62
|
|
|
63
63
|
// This is pulled out due to it's performance cost (50-100ms on cold start)
|
|
64
64
|
// Precompile your schema during a build step is recommended.
|
|
65
65
|
export const transpileSchema = (schema, ajvOptions) => {
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
66
|
+
const options = { ...ajvDefaults, ...ajvOptions };
|
|
67
|
+
return compileSchema(schema, options);
|
|
68
|
+
};
|