@middy/validator 4.6.5 → 5.0.0-alpha.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.
package/index.js CHANGED
@@ -1,51 +1,75 @@
1
- import { createError } from '@middy/util';
1
+ import { createError } from '@middy/util'
2
+
2
3
  const defaults = {
3
- eventSchema: undefined,
4
- contextSchema: undefined,
5
- responseSchema: undefined,
6
- defaultLanguage: 'en',
7
- languages: {}
8
- };
9
- const validatorMiddleware = (opts = {})=>{
10
- const { eventSchema, contextSchema, responseSchema, defaultLanguage, languages } = {
11
- ...defaults,
12
- ...opts
13
- };
14
- const validatorMiddlewareBefore = async (request)=>{
15
- if (eventSchema) {
16
- const validEvent = await eventSchema(request.event);
17
- if (!validEvent) {
18
- const localize = languages[request.event.preferredLanguage] ?? languages[defaultLanguage];
19
- localize?.(eventSchema.errors);
20
- // Bad Request
21
- throw createError(400, 'Event object failed validation', {
22
- cause: eventSchema.errors
23
- });
24
- }
25
- }
26
- if (contextSchema) {
27
- const validContext = await contextSchema(request.context);
28
- if (!validContext) {
29
- // Internal Server Error
30
- throw createError(500, 'Context object failed validation', {
31
- cause: contextSchema.errors
32
- });
33
- }
34
- }
35
- };
36
- const validatorMiddlewareAfter = async (request)=>{
37
- const validResponse = await responseSchema(request.response);
38
- if (!validResponse) {
39
- // Internal Server Error
40
- throw createError(500, 'Response object failed validation', {
41
- cause: responseSchema.errors
42
- });
4
+ eventSchema: undefined,
5
+ contextSchema: undefined,
6
+ responseSchema: undefined,
7
+ defaultLanguage: 'en',
8
+ languages: {}
9
+ }
10
+
11
+ const validatorMiddleware = (opts = {}) => {
12
+ const {
13
+ eventSchema,
14
+ contextSchema,
15
+ responseSchema,
16
+ defaultLanguage,
17
+ languages
18
+ } = { ...defaults, ...opts }
19
+
20
+ const validatorMiddlewareBefore = async (request) => {
21
+ if (eventSchema) {
22
+ const validEvent = await eventSchema(request.event)
23
+
24
+ if (!validEvent) {
25
+ const localize =
26
+ languages[request.context.preferredLanguage] ??
27
+ languages[defaultLanguage]
28
+ localize?.(eventSchema.errors)
29
+
30
+ // Bad Request
31
+ throw createError(400, 'Event object failed validation', {
32
+ cause: {
33
+ pacakge: '@middy/validator',
34
+ data: eventSchema.errors
35
+ }
36
+ })
37
+ }
38
+ }
39
+
40
+ if (contextSchema) {
41
+ const validContext = await contextSchema(request.context)
42
+
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
+
55
+ const validatorMiddlewareAfter = async (request) => {
56
+ const validResponse = await responseSchema(request.response)
57
+
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
43
64
  }
44
- };
45
- return {
46
- before: eventSchema ?? contextSchema ? validatorMiddlewareBefore : undefined,
47
- after: responseSchema ? validatorMiddlewareAfter : undefined
48
- };
49
- };
50
- export default validatorMiddleware;
65
+ })
66
+ }
67
+ }
68
+ return {
69
+ before:
70
+ eventSchema ?? contextSchema ? validatorMiddlewareBefore : undefined,
71
+ after: responseSchema ? validatorMiddlewareAfter : undefined
72
+ }
73
+ }
51
74
 
75
+ export default validatorMiddleware
package/package.json CHANGED
@@ -1,46 +1,35 @@
1
1
  {
2
2
  "name": "@middy/validator",
3
- "version": "4.6.5",
3
+ "version": "5.0.0-alpha.1",
4
4
  "description": "Validator middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
7
- "node": ">=16"
7
+ "node": ">=18"
8
8
  },
9
9
  "engineStrict": true,
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
- "main": "./index.cjs",
14
13
  "module": "./index.js",
15
14
  "exports": {
16
15
  ".": {
17
16
  "import": {
18
17
  "types": "./index.d.ts",
19
18
  "default": "./index.js"
20
- },
21
- "require": {
22
- "types": "./index.d.ts",
23
- "default": "./index.cjs"
24
19
  }
25
20
  },
26
21
  "./transpile": {
27
22
  "import": {
28
23
  "types": "./transpile.d.ts",
29
24
  "default": "./transpile.js"
30
- },
31
- "require": {
32
- "types": "./transpile.d.ts",
33
- "default": "./transpile.cjs"
34
25
  }
35
26
  }
36
27
  },
37
28
  "types": "index.d.ts",
38
29
  "files": [
39
30
  "index.js",
40
- "index.cjs",
41
31
  "index.d.ts",
42
32
  "transpile.js",
43
- "transpile.cjs",
44
33
  "transpile.d.ts"
45
34
  ],
46
35
  "scripts": {
@@ -78,9 +67,8 @@
78
67
  "url": "https://github.com/sponsors/willfarrell"
79
68
  },
80
69
  "dependencies": {
81
- "@middy/util": "4.6.5",
70
+ "@middy/util": "5.0.0-alpha.1",
82
71
  "ajv": "8.12.0",
83
- "ajv-cmd": "0.3.4",
84
72
  "ajv-errors": "3.0.0",
85
73
  "ajv-formats": "2.1.1",
86
74
  "ajv-formats-draft2019": "1.6.1",
@@ -89,9 +77,10 @@
89
77
  "fast-uri": "2.2.0"
90
78
  },
91
79
  "devDependencies": {
92
- "@middy/core": "4.6.5",
80
+ "@middy/core": "5.0.0-alpha.1",
93
81
  "@types/http-errors": "^2.0.0",
94
- "ajv-bsontype": "^1.0.7"
82
+ "ajv-bsontype": "^1.0.7",
83
+ "ajv-cmd": "0.3.4"
95
84
  },
96
- "gitHead": "573d7b0bb243d8c5a9bcb00cf29d031aa7a0c606"
85
+ "gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
97
86
  }
package/transpile.js CHANGED
@@ -1,22 +1,25 @@
1
- import compileSchema from 'ajv-cmd/compile';
2
- import transpileFTL from 'ajv-cmd/ftl';
1
+ /*
2
+ import compileSchema from 'ajv-cmd/compile'
3
+ import transpileFTL from 'ajv-cmd/ftl'
4
+
3
5
  const ajvDefaults = {
4
- strict: true,
5
- coerceTypes: 'array',
6
- allErrors: true,
7
- useDefaults: 'empty',
8
- messages: true // needs to be true to allow multi-locale errorMessage to work
9
- };
6
+ strict: true,
7
+ coerceTypes: 'array', // important for query string params
8
+ allErrors: true,
9
+ useDefaults: 'empty',
10
+ messages: true // needs to be true to allow multi-locale errorMessage to work
11
+ }
12
+
10
13
  // This is pulled out due to it's performance cost (50-100ms on cold start)
11
14
  // Precompile your schema during a build step is recommended.
12
- export const transpileSchema = (schema, ajvOptions)=>{
13
- const options = {
14
- ...ajvDefaults,
15
- ...ajvOptions
16
- };
17
- return compileSchema(schema, options);
18
- };
19
- export const transpileLocale = transpileFTL /*
15
+ export const transpileSchema = (schema, ajvOptions) => {
16
+ const options = { ...ajvDefaults, ...ajvOptions }
17
+ return compileSchema(schema, options)
18
+ }
19
+
20
+ export const transpileLocale = transpileFTL
21
+ */
22
+
20
23
  import Ajv from 'ajv/dist/2020.js'
21
24
  import ajvFormats from 'ajv-formats'
22
25
  import ajvFormatsDraft2019 from 'ajv-formats-draft2019'
@@ -54,5 +57,17 @@ const compileSchema = (schema, options = {}) => {
54
57
  }
55
58
  // *** End `ajv-cmd/compile` *** //
56
59
 
57
- */ ;
60
+ const ajvDefaults = {
61
+ strict: true,
62
+ coerceTypes: 'array', // important for query string params
63
+ allErrors: true,
64
+ useDefaults: 'empty',
65
+ messages: true // needs to be true to allow multi-locale errorMessage to work
66
+ }
58
67
 
68
+ // This is pulled out due to it's performance cost (50-100ms on cold start)
69
+ // Precompile your schema during a build step is recommended.
70
+ export const transpileSchema = (schema, ajvOptions) => {
71
+ const options = { ...ajvDefaults, ...ajvOptions }
72
+ return compileSchema(schema, options)
73
+ }
package/index.cjs DELETED
@@ -1,61 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(module, "exports", {
6
- enumerable: true,
7
- get: function() {
8
- return _default;
9
- }
10
- });
11
- const _util = require("@middy/util");
12
- const defaults = {
13
- eventSchema: undefined,
14
- contextSchema: undefined,
15
- responseSchema: undefined,
16
- defaultLanguage: 'en',
17
- languages: {}
18
- };
19
- const validatorMiddleware = (opts = {})=>{
20
- const { eventSchema, contextSchema, responseSchema, defaultLanguage, languages } = {
21
- ...defaults,
22
- ...opts
23
- };
24
- const validatorMiddlewareBefore = async (request)=>{
25
- if (eventSchema) {
26
- const validEvent = await eventSchema(request.event);
27
- if (!validEvent) {
28
- const localize = languages[request.event.preferredLanguage] ?? languages[defaultLanguage];
29
- localize?.(eventSchema.errors);
30
- // Bad Request
31
- throw (0, _util.createError)(400, 'Event object failed validation', {
32
- cause: eventSchema.errors
33
- });
34
- }
35
- }
36
- if (contextSchema) {
37
- const validContext = await contextSchema(request.context);
38
- if (!validContext) {
39
- // Internal Server Error
40
- throw (0, _util.createError)(500, 'Context object failed validation', {
41
- cause: contextSchema.errors
42
- });
43
- }
44
- }
45
- };
46
- const validatorMiddlewareAfter = async (request)=>{
47
- const validResponse = await responseSchema(request.response);
48
- if (!validResponse) {
49
- // Internal Server Error
50
- throw (0, _util.createError)(500, 'Response object failed validation', {
51
- cause: responseSchema.errors
52
- });
53
- }
54
- };
55
- return {
56
- before: eventSchema ?? contextSchema ? validatorMiddlewareBefore : undefined,
57
- after: responseSchema ? validatorMiddlewareAfter : undefined
58
- };
59
- };
60
- const _default = validatorMiddleware;
61
-
package/transpile.cjs DELETED
@@ -1,79 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- function _export(target, all) {
6
- for(var name in all)Object.defineProperty(target, name, {
7
- enumerable: true,
8
- get: all[name]
9
- });
10
- }
11
- _export(exports, {
12
- transpileLocale: function() {
13
- return transpileLocale;
14
- },
15
- transpileSchema: function() {
16
- return transpileSchema;
17
- }
18
- });
19
- const _compile = /*#__PURE__*/ _interop_require_default(require("ajv-cmd/compile"));
20
- const _ftl = /*#__PURE__*/ _interop_require_default(require("ajv-cmd/ftl"));
21
- function _interop_require_default(obj) {
22
- return obj && obj.__esModule ? obj : {
23
- default: obj
24
- };
25
- }
26
- const ajvDefaults = {
27
- strict: true,
28
- coerceTypes: 'array',
29
- allErrors: true,
30
- useDefaults: 'empty',
31
- messages: true // needs to be true to allow multi-locale errorMessage to work
32
- };
33
- const transpileSchema = (schema, ajvOptions)=>{
34
- const options = {
35
- ...ajvDefaults,
36
- ...ajvOptions
37
- };
38
- return (0, _compile.default)(schema, options);
39
- };
40
- const transpileLocale = _ftl /*
41
- import Ajv from 'ajv/dist/2020.js'
42
- import ajvFormats from 'ajv-formats'
43
- import ajvFormatsDraft2019 from 'ajv-formats-draft2019'
44
- import ajvKeywords from 'ajv-keywords'
45
- import ajvErrors from 'ajv-errors'
46
- import uriResolver from 'fast-uri'
47
-
48
- import { transpile } from 'ajv-ftl-i18n'
49
-
50
- // import transpileFTL from 'ajv-cmd/ftl'
51
- export const transpileFTL = transpile
52
-
53
- // *** Start `ajv-cmd/compile` *** //
54
- // import compileSchema from 'ajv-cmd/compile'
55
-
56
- const defaultOptions = {
57
- uriResolver // faster than default
58
- }
59
-
60
- const instance = (options = {}) => {
61
- options = { ...defaultOptions, ...options, keywords: [] }
62
-
63
- const ajv = new Ajv(options)
64
- ajvFormats(ajv)
65
- ajvFormatsDraft2019(ajv)
66
- ajvKeywords(ajv)
67
- ajvErrors(ajv)
68
- return ajv
69
- }
70
-
71
- const compileSchema = (schema, options = {}) => {
72
- options = { ...defaultOptions, ...options, keywords: [] }
73
- const ajv = instance(options)
74
- return ajv.compile(schema)
75
- }
76
- // *** End `ajv-cmd/compile` *** //
77
-
78
- */ .default;
79
-