@middy/validator 5.0.0-alpha.0 → 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/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
@@ -1,48 +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
- throw createError(400, 'Event object failed validation', {
21
- cause: eventSchema.errors
22
- });
23
- }
24
- }
25
- if (contextSchema) {
26
- const validContext = await contextSchema(request.context);
27
- if (!validContext) {
28
- throw createError(500, 'Context object failed validation', {
29
- cause: contextSchema.errors
30
- });
31
- }
32
- }
33
- };
34
- const validatorMiddlewareAfter = async (request)=>{
35
- const validResponse = await responseSchema(request.response);
36
- if (!validResponse) {
37
- throw createError(500, 'Response object failed validation', {
38
- cause: responseSchema.errors
39
- });
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
40
64
  }
41
- };
42
- return {
43
- before: eventSchema ?? contextSchema ? validatorMiddlewareBefore : undefined,
44
- after: responseSchema ? validatorMiddlewareAfter : undefined
45
- };
46
- };
47
- export default validatorMiddleware;
65
+ })
66
+ }
67
+ }
68
+ return {
69
+ before:
70
+ eventSchema ?? contextSchema ? validatorMiddlewareBefore : undefined,
71
+ after: responseSchema ? validatorMiddlewareAfter : undefined
72
+ }
73
+ }
48
74
 
75
+ export default validatorMiddleware
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.1",
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.1",
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.1",
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,18 +1,73 @@
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
+ */
22
+
23
+ import Ajv from 'ajv/dist/2020.js'
24
+ import ajvFormats from 'ajv-formats'
25
+ import ajvFormatsDraft2019 from 'ajv-formats-draft2019'
26
+ import ajvKeywords from 'ajv-keywords'
27
+ import ajvErrors from 'ajv-errors'
28
+ import uriResolver from 'fast-uri'
29
+
30
+ import { transpile } from 'ajv-ftl-i18n'
31
+
32
+ // import transpileFTL from 'ajv-cmd/ftl'
33
+ export const transpileFTL = transpile
34
+
35
+ // *** Start `ajv-cmd/compile` *** //
36
+ // import compileSchema from 'ajv-cmd/compile'
37
+
38
+ const defaultOptions = {
39
+ uriResolver // faster than default
40
+ }
41
+
42
+ const instance = (options = {}) => {
43
+ options = { ...defaultOptions, ...options, keywords: [] }
44
+
45
+ const ajv = new Ajv(options)
46
+ ajvFormats(ajv)
47
+ ajvFormatsDraft2019(ajv)
48
+ ajvKeywords(ajv)
49
+ ajvErrors(ajv)
50
+ return ajv
51
+ }
52
+
53
+ const compileSchema = (schema, options = {}) => {
54
+ options = { ...defaultOptions, ...options, keywords: [] }
55
+ const ajv = instance(options)
56
+ return ajv.compile(schema)
57
+ }
58
+ // *** End `ajv-cmd/compile` *** //
59
+
3
60
  const ajvDefaults = {
4
- strict: true,
5
- coerceTypes: 'array',
6
- allErrors: true,
7
- useDefaults: 'empty',
8
- messages: true
9
- };
10
- export const transpileSchema = (schema, ajvOptions)=>{
11
- const options = {
12
- ...ajvDefaults,
13
- ...ajvOptions
14
- };
15
- return compileSchema(schema, options);
16
- };
17
- export const transpileLocale = transpileFTL;
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
+ }
18
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,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
-