@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 CHANGED
@@ -1,13 +1,13 @@
1
- import middy from '@middy/core'
1
+ import type middy from "@middy/core";
2
2
 
3
3
  interface Options {
4
- eventSchema?: Function | any
5
- contextSchema?: Function | any
6
- responseSchema?: Function | any
7
- defaultLanguage?: string
8
- languages?: object | any
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 (options?: Options): middy.MiddlewareObj
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 '@middy/util'
1
+ import { createError } from "@middy/util";
2
2
 
3
3
  const defaults = {
4
- eventSchema: undefined,
5
- contextSchema: undefined,
6
- responseSchema: undefined,
7
- defaultLanguage: 'en',
8
- languages: {}
9
- }
4
+ eventSchema: undefined,
5
+ contextSchema: undefined,
6
+ responseSchema: undefined,
7
+ defaultLanguage: "en",
8
+ languages: {},
9
+ };
10
10
 
11
11
  const validatorMiddleware = (opts = {}) => {
12
- const {
13
- eventSchema,
14
- contextSchema,
15
- responseSchema,
16
- defaultLanguage,
17
- languages
18
- } = { ...defaults, ...opts }
12
+ const {
13
+ eventSchema,
14
+ contextSchema,
15
+ responseSchema,
16
+ defaultLanguage,
17
+ languages,
18
+ } = { ...defaults, ...opts };
19
19
 
20
- const validatorMiddlewareBefore = async (request) => {
21
- if (eventSchema) {
22
- const validEvent = await eventSchema(request.event)
20
+ const validatorMiddlewareBefore = async (request) => {
21
+ if (eventSchema) {
22
+ const validEvent = await eventSchema(request.event);
23
23
 
24
- if (!validEvent) {
25
- const localize =
26
- languages[request.context.preferredLanguage] ??
27
- languages[defaultLanguage]
28
- localize?.(eventSchema.errors)
24
+ if (!validEvent) {
25
+ const localize =
26
+ languages[request.context.preferredLanguage] ??
27
+ languages[defaultLanguage];
28
+ localize?.(eventSchema.errors);
29
29
 
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
- }
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
- if (contextSchema) {
41
- const validContext = await contextSchema(request.context)
40
+ if (contextSchema) {
41
+ const validContext = await contextSchema(request.context);
42
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
- }
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
- const validatorMiddlewareAfter = async (request) => {
56
- const validResponse = await responseSchema(request.response)
55
+ const validatorMiddlewareAfter = async (request) => {
56
+ const validResponse = await responseSchema(request.response);
57
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
64
- }
65
- })
66
- }
67
- }
68
- return {
69
- before:
70
- eventSchema ?? contextSchema ? validatorMiddlewareBefore : undefined,
71
- after: responseSchema ? validatorMiddlewareAfter : undefined
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
- "name": "@middy/validator",
3
- "version": "6.1.5",
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": [
36
- "index.js",
37
- "index.d.ts",
38
- "transpile.js",
39
- "transpile.d.ts"
40
- ],
41
- "scripts": {
42
- "test": "npm run test:unit && npm run test:fuzz",
43
- "test:unit": "node --test __tests__/index.js",
44
- "test:fuzz": "node --test __tests__/fuzz.js",
45
- "test:benchmark": "node __benchmarks__/index.js"
46
- },
47
- "license": "MIT",
48
- "keywords": [
49
- "Lambda",
50
- "Middleware",
51
- "Serverless",
52
- "Framework",
53
- "AWS",
54
- "AWS Lambda",
55
- "Middy",
56
- "Validator",
57
- "JSON Schema"
58
- ],
59
- "author": {
60
- "name": "Middy contributors",
61
- "url": "https://github.com/middyjs/middy/graphs/contributors"
62
- },
63
- "repository": {
64
- "type": "git",
65
- "url": "git+https://github.com/middyjs/middy.git",
66
- "directory": "packages/validator"
67
- },
68
- "bugs": {
69
- "url": "https://github.com/middyjs/middy/issues"
70
- },
71
- "homepage": "https://middy.js.org",
72
- "funding": {
73
- "type": "github",
74
- "url": "https://github.com/sponsors/willfarrell"
75
- },
76
- "dependencies": {
77
- "@middy/util": "6.1.5",
78
- "ajv": "8.17.1",
79
- "ajv-errors": "3.0.0",
80
- "ajv-formats": "3.0.1",
81
- "ajv-formats-draft2019": "1.6.1",
82
- "ajv-ftl-i18n": "0.1.1",
83
- "ajv-keywords": "5.1.0"
84
- },
85
- "devDependencies": {
86
- "@middy/core": "6.1.5",
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, { Options as AjvOptions } from 'ajv'
1
+ import type Ajv from "ajv";
2
+ import type { Options as AjvOptions } from "ajv";
2
3
 
3
- export function transpileSchema (
4
- schema: object,
5
- ajvOptions?: Partial<AjvOptions>
6
- ): Ajv
4
+ export function transpileSchema(
5
+ schema: object,
6
+ ajvOptions?: Partial<AjvOptions>,
7
+ ): Ajv;
7
8
 
8
- export function transpileLocale (src: string, options?: object | any): Function
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 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'
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 'ajv-ftl-i18n'
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
- options = { ...options, keywords: [] }
38
+ Object.assign(options, { keywords: [] });
39
39
 
40
- const ajv = new Ajv(options)
41
- ajvFormats(ajv)
42
- ajvFormatsDraft2019(ajv)
43
- ajvKeywords(ajv)
44
- ajvErrors(ajv)
45
- return ajv
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
- options = { ...options, keywords: [] }
50
- const ajv = instance(options)
51
- return ajv.compile(schema)
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
- 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
- }
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
- const options = { ...ajvDefaults, ...ajvOptions }
67
- return compileSchema(schema, options)
68
- }
66
+ const options = { ...ajvDefaults, ...ajvOptions };
67
+ return compileSchema(schema, options);
68
+ };