@middy/validator 5.0.0-alpha.0 → 5.0.0-alpha.2

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/README.md CHANGED
@@ -19,8 +19,9 @@
19
19
  <a href="https://snyk.io/test/github/middyjs/middy">
20
20
  <img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
21
21
  </a>
22
- <a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
23
- <img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
22
+ <a href="https://github.com/middyjs/middy/actions/workflows/sast.yml">
23
+ <img src="https://github.com/middyjs/middy/actions/workflows/sast.yml/badge.svg
24
+ ?branch=main&event=push" alt="CodeQL" style="max-width:100%;">
24
25
  </a>
25
26
  <a href="https://bestpractices.coreinfrastructure.org/projects/5280">
26
27
  <img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
package/index.js CHANGED
@@ -7,7 +7,7 @@ const defaults = {
7
7
  languages: {}
8
8
  };
9
9
  const validatorMiddleware = (opts = {})=>{
10
- const { eventSchema , contextSchema , responseSchema , defaultLanguage , languages } = {
10
+ const { eventSchema, contextSchema, responseSchema, defaultLanguage, languages } = {
11
11
  ...defaults,
12
12
  ...opts
13
13
  };
@@ -15,18 +15,26 @@ const validatorMiddleware = (opts = {})=>{
15
15
  if (eventSchema) {
16
16
  const validEvent = await eventSchema(request.event);
17
17
  if (!validEvent) {
18
- const localize = languages[request.event.preferredLanguage] ?? languages[defaultLanguage];
18
+ const localize = languages[request.context.preferredLanguage] ?? languages[defaultLanguage];
19
19
  localize?.(eventSchema.errors);
20
+ // Bad Request
20
21
  throw createError(400, 'Event object failed validation', {
21
- cause: eventSchema.errors
22
+ cause: {
23
+ pacakge: '@middy/validator',
24
+ data: eventSchema.errors
25
+ }
22
26
  });
23
27
  }
24
28
  }
25
29
  if (contextSchema) {
26
30
  const validContext = await contextSchema(request.context);
27
31
  if (!validContext) {
32
+ // Internal Server Error
28
33
  throw createError(500, 'Context object failed validation', {
29
- cause: contextSchema.errors
34
+ cause: {
35
+ package: '@middy/validator',
36
+ data: contextSchema.errors
37
+ }
30
38
  });
31
39
  }
32
40
  }
@@ -34,8 +42,12 @@ const validatorMiddleware = (opts = {})=>{
34
42
  const validatorMiddlewareAfter = async (request)=>{
35
43
  const validResponse = await responseSchema(request.response);
36
44
  if (!validResponse) {
45
+ // Internal Server Error
37
46
  throw createError(500, 'Response object failed validation', {
38
- cause: responseSchema.errors
47
+ cause: {
48
+ package: '@middy/validator',
49
+ data: responseSchema.errors
50
+ }
39
51
  });
40
52
  }
41
53
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/validator",
3
- "version": "5.0.0-alpha.0",
3
+ "version": "5.0.0-alpha.2",
4
4
  "description": "Validator middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -10,37 +10,26 @@
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,13 +67,20 @@
78
67
  "url": "https://github.com/sponsors/willfarrell"
79
68
  },
80
69
  "dependencies": {
81
- "@middy/util": "5.0.0-alpha.0",
82
- "ajv-cmd": "0.3.3"
70
+ "@middy/util": "5.0.0-alpha.2",
71
+ "ajv": "8.12.0",
72
+ "ajv-errors": "3.0.0",
73
+ "ajv-formats": "2.1.1",
74
+ "ajv-formats-draft2019": "1.6.1",
75
+ "ajv-ftl-i18n": "0.1.1",
76
+ "ajv-keywords": "5.1.0",
77
+ "fast-uri": "2.2.0"
83
78
  },
84
79
  "devDependencies": {
85
- "@middy/core": "5.0.0-alpha.0",
80
+ "@middy/core": "5.0.0-alpha.2",
86
81
  "@types/http-errors": "^2.0.0",
87
- "ajv-bsontype": "^1.0.7"
82
+ "ajv-bsontype": "^1.0.7",
83
+ "ajv-cmd": "0.3.4"
88
84
  },
89
- "gitHead": "08c35e3dba9efdad0b86666ce206ce302cc65d07"
85
+ "gitHead": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
90
86
  }
package/transpile.js CHANGED
@@ -1,12 +1,69 @@
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
+
5
+ const ajvDefaults = {
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
+
13
+ // This is pulled out due to it's performance cost (50-100ms on cold start)
14
+ // Precompile your schema during a build step is recommended.
15
+ export const transpileSchema = (schema, ajvOptions) => {
16
+ const options = { ...ajvDefaults, ...ajvOptions }
17
+ return compileSchema(schema, options)
18
+ }
19
+
20
+ export const transpileLocale = transpileFTL
21
+ */ import Ajv from 'ajv/dist/2020.js';
22
+ import ajvFormats from 'ajv-formats';
23
+ import ajvFormatsDraft2019 from 'ajv-formats-draft2019';
24
+ import ajvKeywords from 'ajv-keywords';
25
+ import ajvErrors from 'ajv-errors';
26
+ import uriResolver from 'fast-uri';
27
+ import { transpile } from 'ajv-ftl-i18n';
28
+ // import transpileFTL from 'ajv-cmd/ftl'
29
+ export const transpileFTL = transpile;
30
+ // *** Start `ajv-cmd/compile` *** //
31
+ // import compileSchema from 'ajv-cmd/compile'
32
+ const defaultOptions = {
33
+ uriResolver
34
+ };
35
+ const instance = (options = {})=>{
36
+ options = {
37
+ ...defaultOptions,
38
+ ...options,
39
+ keywords: []
40
+ };
41
+ const ajv = new Ajv(options);
42
+ ajvFormats(ajv);
43
+ ajvFormatsDraft2019(ajv);
44
+ ajvKeywords(ajv);
45
+ ajvErrors(ajv);
46
+ return ajv;
47
+ };
48
+ const compileSchema = (schema, options = {})=>{
49
+ options = {
50
+ ...defaultOptions,
51
+ ...options,
52
+ keywords: []
53
+ };
54
+ const ajv = instance(options);
55
+ return ajv.compile(schema);
56
+ };
57
+ // *** End `ajv-cmd/compile` *** //
3
58
  const ajvDefaults = {
4
59
  strict: true,
5
60
  coerceTypes: 'array',
6
61
  allErrors: true,
7
62
  useDefaults: 'empty',
8
- messages: true
63
+ messages: true // needs to be true to allow multi-locale errorMessage to work
9
64
  };
65
+ // This is pulled out due to it's performance cost (50-100ms on cold start)
66
+ // Precompile your schema during a build step is recommended.
10
67
  export const transpileSchema = (schema, ajvOptions)=>{
11
68
  const options = {
12
69
  ...ajvDefaults,
@@ -14,5 +71,4 @@ export const transpileSchema = (schema, ajvOptions)=>{
14
71
  };
15
72
  return compileSchema(schema, options);
16
73
  };
17
- export const transpileLocale = transpileFTL;
18
74
 
package/index.cjs DELETED
@@ -1,56 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", {
3
- value: true
4
- });
5
- Object.defineProperty(module, "exports", {
6
- enumerable: true,
7
- get: ()=>_default
8
- });
9
- const _util = require("@middy/util");
10
- const defaults = {
11
- eventSchema: undefined,
12
- contextSchema: undefined,
13
- responseSchema: undefined,
14
- defaultLanguage: 'en',
15
- languages: {}
16
- };
17
- const validatorMiddleware = (opts = {})=>{
18
- const { eventSchema , contextSchema , responseSchema , defaultLanguage , languages } = {
19
- ...defaults,
20
- ...opts
21
- };
22
- const validatorMiddlewareBefore = async (request)=>{
23
- if (eventSchema) {
24
- const validEvent = await eventSchema(request.event);
25
- if (!validEvent) {
26
- const localize = languages[request.event.preferredLanguage] ?? languages[defaultLanguage];
27
- localize?.(eventSchema.errors);
28
- throw (0, _util.createError)(400, 'Event object failed validation', {
29
- cause: eventSchema.errors
30
- });
31
- }
32
- }
33
- if (contextSchema) {
34
- const validContext = await contextSchema(request.context);
35
- if (!validContext) {
36
- throw (0, _util.createError)(500, 'Context object failed validation', {
37
- cause: contextSchema.errors
38
- });
39
- }
40
- }
41
- };
42
- const validatorMiddlewareAfter = async (request)=>{
43
- const validResponse = await responseSchema(request.response);
44
- if (!validResponse) {
45
- throw (0, _util.createError)(500, 'Response object failed validation', {
46
- cause: responseSchema.errors
47
- });
48
- }
49
- };
50
- return {
51
- before: eventSchema ?? contextSchema ? validatorMiddlewareBefore : undefined,
52
- after: responseSchema ? validatorMiddlewareAfter : undefined
53
- };
54
- };
55
- const _default = validatorMiddleware;
56
-
package/transpile.cjs DELETED
@@ -1,37 +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
- transpileSchema: ()=>transpileSchema,
13
- transpileLocale: ()=>transpileLocale
14
- });
15
- const _compile = _interopRequireDefault(require("ajv-cmd/compile"));
16
- const _ftl = _interopRequireDefault(require("ajv-cmd/ftl"));
17
- function _interopRequireDefault(obj) {
18
- return obj && obj.__esModule ? obj : {
19
- default: obj
20
- };
21
- }
22
- const ajvDefaults = {
23
- strict: true,
24
- coerceTypes: 'array',
25
- allErrors: true,
26
- useDefaults: 'empty',
27
- messages: true
28
- };
29
- const transpileSchema = (schema, ajvOptions)=>{
30
- const options = {
31
- ...ajvDefaults,
32
- ...ajvOptions
33
- };
34
- return (0, _compile.default)(schema, options);
35
- };
36
- const transpileLocale = _ftl.default;
37
-