@middy/validator 3.0.0-alpha.6 → 3.0.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/README.md CHANGED
@@ -1,32 +1,42 @@
1
- # Middy validator middleware
2
-
3
- <div align="center">
4
- <img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.png"/>
5
- </div>
6
-
7
1
  <div align="center">
2
+ <h1>Middy validator middleware</h1>
3
+ <img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
8
4
  <p><strong>Validator middleware for the middy framework, the stylish Node.js middleware engine for AWS Lambda</strong></p>
9
- </div>
10
-
11
- <div align="center">
12
5
  <p>
13
- <a href="http://badge.fury.io/js/%40middy%2Fvalidator">
6
+ <a href="https://www.npmjs.com/package/@middy/validator?activeTab=versions">
14
7
  <img src="https://badge.fury.io/js/%40middy%2Fvalidator.svg" alt="npm version" style="max-width:100%;">
15
8
  </a>
9
+ <a href="https://packagephobia.com/result?p=@middy/validator">
10
+ <img src="https://packagephobia.com/badge?p=@middy/validator" alt="npm install size" style="max-width:100%;">
11
+ </a>
12
+ <a href="https://github.com/middyjs/middy/actions">
13
+ <img src="https://github.com/middyjs/middy/workflows/Tests/badge.svg" alt="GitHub Actions test status badge" style="max-width:100%;">
14
+ </a>
15
+ <br/>
16
+ <a href="https://standardjs.com/">
17
+ <img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
18
+ </a>
16
19
  <a href="https://snyk.io/test/github/middyjs/middy">
17
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%;">
18
21
  </a>
19
- <a href="https://standardjs.com/">
20
- <img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
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%;">
21
24
  </a>
25
+ <a href="https://bestpractices.coreinfrastructure.org/projects/5280">
26
+ <img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
27
+ </a>
28
+ <br/>
22
29
  <a href="https://gitter.im/middyjs/Lobby">
23
- <img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
30
+ <img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
31
+ </a>
32
+ <a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
33
+ <img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
24
34
  </a>
25
35
  </p>
26
36
  </div>
27
37
 
28
38
  This middleware automatically validates incoming events and outgoing responses against custom
29
- schemas defined with the [JSON schema syntax](http://json-schema.org/).
39
+ schemas defined with the [JSON schema syntax](https://json-schema.org/).
30
40
 
31
41
  If an incoming event fails validation a `BadRequest` error is raised.
32
42
  If an outgoing response fails validation a `InternalServerError` error is
@@ -36,7 +46,7 @@ This middleware can be used in combination with
36
46
  [`httpErrorHandler`](#httperrorhandler) to automatically return the right
37
47
  response to the user.
38
48
 
39
- It can also be used in combination with [`httpcontentnegotiation`](#httpContentNegotiation) to load localised translations for the error messages (based on the currently requested language). This feature uses internally [`ajv-i18n`](http://npm.im/ajv-i18n) module, so reference to this module for options and more advanced use cases. By default the language used will be English (`en`), but you can redefine the default language by passing it in the `ajvOptions` options with the key `defaultLanguage` and specifying as value one of the [supported locales](https://www.npmjs.com/package/ajv-i18n#supported-locales).
49
+ It can also be used in combination with [`httpcontentnegotiation`](#httpContentNegotiation) to load localised translations for the error messages (based on the currently requested language). This feature uses internally [`ajv-i18n`](https://www.npmjs.com/package/ajv-i18n) module, so reference to this module for options and more advanced use cases. By default the language used will be English (`en`), but you can redefine the default language by passing it in the `ajvOptions` options with the key `defaultLanguage` and specifying as value one of the [supported locales](https://www.npmjs.com/package/ajv-i18n#supported-locales).
40
50
 
41
51
  Also, this middleware accepts an object with plugins to be applied to customize the internal `ajv` instance. Out-of-the-box `ajv-i18n` and `ajv-formats` are being used.
42
52
 
@@ -51,13 +61,16 @@ npm install --save @middy/validator
51
61
 
52
62
  ## Options
53
63
 
54
- - `inputSchema` (object) (optional): The JSON schema object or compiled ajv validator that will be used
55
- to validate the input (`request.event`) of the Lambda handler.
56
- - `outputSchema` (object) (optional): The JSON schema object or compiled ajv validator that will be used
57
- to validate the output (`request.response`) of the Lambda handler.
58
- - `ajvOptions` (object) (optional): Options to pass to [ajv](https://ajv.js.org/docs/api.html#options)
64
+ - `eventSchema` (object|function) (default `undefined`): The JSON schema object or compiled ajv validator that will be used
65
+ to validate the input (`request.event`) of the Lambda handler. Supports alias `inputSchema`
66
+ - `contextSchema` (object|function) (default `undefined`): The JSON schema object or compiled ajv validator that will be used
67
+ to validate the input (`request.context`) of the Lambda handler. Has additional support for `typeof` keyword to allow validation of `"typeof":"function"`.
68
+ - `responseSchema` (object|function) (default `undefined`): The JSON schema object or compiled ajv validator that will be used
69
+ to validate the output (`request.response`) of the Lambda handler. Supports alias `inputSchema`
70
+ - `ajvOptions` (object) (default `undefined`): Options to pass to [ajv](https://ajv.js.org/docs/api.html#options)
59
71
  class constructor. Defaults are `{ strict: true, coerceTypes: 'array', allErrors: true, useDefaults: 'empty', messages: false, defaultLanguage: 'en' }`.
60
- - `i18nEnabled` (boolean) (optional): Option to disable i18n default package. Defaults to `true`
72
+ - `i18nEnabled` (boolean) (default `true`): Option to disable i18n default package.
73
+
61
74
  NOTES:
62
75
  - At least one of `inputSchema` or `outputSchema` is required.
63
76
  - **Important** Compiling schemas on the fly will cause a 50-100ms performance hit during cold start for simple JSON Schemas. Precompiling is highly recommended.
package/index.cjs ADDED
@@ -0,0 +1,3 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _util=require("@middy/util");var _2020Js=_interopRequireDefault(require("ajv/dist/2020.js"));var _ajvI18N=_interopRequireDefault(require("ajv-i18n"));var _ajvFormats=_interopRequireDefault(require("ajv-formats"));var _ajvFormatsDraft2019=_interopRequireDefault(require("ajv-formats-draft2019"));var _typeofJs=_interopRequireDefault(require("ajv-keywords/dist/definitions/typeof.js"));function _interopRequireDefault(obj){return obj&&obj.__esModule?obj:{default:obj}}const Ajv=_2020Js.default.default;let ajv;const ajvDefaults={strict:true,coerceTypes:"array",allErrors:true,useDefaults:"empty",messages:false,keywords:[(0,_typeofJs).default()]};const defaults={eventSchema:undefined,contextSchema:undefined,responseSchema:undefined,ajvOptions:{},ajvInstance:undefined,defaultLanguage:"en",i18nEnabled:true};const validatorMiddleware=(opts={})=>{let{inputSchema,outputSchema,eventSchema,contextSchema,responseSchema,ajvOptions,ajvInstance,defaultLanguage,i18nEnabled}={...defaults,...opts};eventSchema=compile(eventSchema??inputSchema,ajvOptions,ajvInstance);contextSchema=compile(contextSchema,ajvOptions,ajvInstance);responseSchema=compile(responseSchema??outputSchema,ajvOptions,ajvInstance);const validatorMiddlewareBefore=async request=>{if(eventSchema){const validEvent=await eventSchema(request.event);if(!validEvent){if(i18nEnabled){const language=chooseLanguage(request.event,defaultLanguage);_ajvI18N.default[language](eventSchema.errors)}const error=(0,_util).createError(400,"Event object failed validation");error.cause=eventSchema.errors;throw error}}if(contextSchema){const validContext=await contextSchema(request.context);if(!validContext){const error=(0,_util).createError(500,"Context object failed validation");error.cause=contextSchema.errors;throw error}}};const validatorMiddlewareAfter=async request=>{const valid=await responseSchema(request.response);if(!valid){const error=(0,_util).createError(500,"Response object failed validation");error.cause=responseSchema.errors;throw error}};return{before:(eventSchema??inputSchema)??contextSchema?validatorMiddlewareBefore:undefined,after:responseSchema??outputSchema?validatorMiddlewareAfter:undefined}};const compile=(schema,ajvOptions,ajvInstance=null)=>{if(typeof schema==="function"||!schema)return schema;const options={...ajvDefaults,...ajvOptions};if(!ajv){ajv=ajvInstance??new Ajv(options);(0,_ajvFormats).default(ajv);(0,_ajvFormatsDraft2019).default(ajv)}else if(!ajvInstance){ajv.opts={...ajv.opts,...options}}return ajv.compile(schema)};const languageNormalizationMap={pt:"pt-BR","pt-br":"pt-BR",pt_BR:"pt-BR",pt_br:"pt-BR","zh-tw":"zh-TW",zh_TW:"zh-TW",zh_tw:"zh-TW"};const normalizePreferredLanguage=lang=>languageNormalizationMap[lang]??lang;const availableLanguages=Object.keys(_ajvI18N.default);const chooseLanguage=({preferredLanguage},defaultLanguage)=>{if(preferredLanguage){const lang=normalizePreferredLanguage(preferredLanguage);if(availableLanguages.includes(lang)){return lang}}return defaultLanguage};var _default=validatorMiddleware;exports.default=_default
2
+
3
+ //# sourceMappingURL=index.cjs.map
package/index.d.ts CHANGED
@@ -3,8 +3,11 @@ import middy from '@middy/core'
3
3
  import Ajv, { Options as AjvOptions } from 'ajv'
4
4
 
5
5
  interface Options {
6
- inputSchema?: object | any
7
- outputSchema?: object | any
6
+ inputSchema?: object | any // Deprecate v4
7
+ outputSchema?: object | any // Deprecate v4
8
+ eventSchema?: object | any
9
+ contextSchema?: object | any
10
+ responseSchema?: object | any
8
11
  ajvOptions?: Partial<AjvOptions>
9
12
  ajvInstance?: Ajv
10
13
  defaultLanguage?: string
package/index.js CHANGED
@@ -1,116 +1,3 @@
1
- import { createError } from '@middy/util';
2
- import _ajv from 'ajv/dist/2019.js';
3
- import localize from 'ajv-i18n';
4
- import formats from 'ajv-formats';
5
- import formatsDraft2019 from 'ajv-formats-draft2019';
6
- const Ajv = _ajv.default;
7
- let ajv;
8
- const ajvDefaults = {
9
- strict: true,
10
- coerceTypes: 'array',
11
- allErrors: true,
12
- useDefaults: 'empty',
13
- messages: false
14
- };
15
- const defaults = {
16
- inputSchema: undefined,
17
- outputSchema: undefined,
18
- ajvOptions: {},
19
- ajvInstance: undefined,
20
- defaultLanguage: 'en',
21
- i18nEnabled: true
22
- };
1
+ import{createError}from"@middy/util";import _ajv from"ajv/dist/2020.js";import localize from"ajv-i18n";import formats from"ajv-formats";import formatsDraft2019 from"ajv-formats-draft2019";import typeofKeyword from"ajv-keywords/dist/definitions/typeof.js";const Ajv=_ajv.default;let ajv;const ajvDefaults={strict:true,coerceTypes:"array",allErrors:true,useDefaults:"empty",messages:false,keywords:[typeofKeyword()]};const defaults={eventSchema:undefined,contextSchema:undefined,responseSchema:undefined,ajvOptions:{},ajvInstance:undefined,defaultLanguage:"en",i18nEnabled:true};const validatorMiddleware=(opts={})=>{let{inputSchema,outputSchema,eventSchema,contextSchema,responseSchema,ajvOptions,ajvInstance,defaultLanguage,i18nEnabled}={...defaults,...opts};eventSchema=compile(eventSchema??inputSchema,ajvOptions,ajvInstance);contextSchema=compile(contextSchema,ajvOptions,ajvInstance);responseSchema=compile(responseSchema??outputSchema,ajvOptions,ajvInstance);const validatorMiddlewareBefore=async request=>{if(eventSchema){const validEvent=await eventSchema(request.event);if(!validEvent){if(i18nEnabled){const language=chooseLanguage(request.event,defaultLanguage);localize[language](eventSchema.errors)}const error=createError(400,"Event object failed validation");error.cause=eventSchema.errors;throw error}}if(contextSchema){const validContext=await contextSchema(request.context);if(!validContext){const error=createError(500,"Context object failed validation");error.cause=contextSchema.errors;throw error}}};const validatorMiddlewareAfter=async request=>{const valid=await responseSchema(request.response);if(!valid){const error=createError(500,"Response object failed validation");error.cause=responseSchema.errors;throw error}};return{before:(eventSchema??inputSchema)??contextSchema?validatorMiddlewareBefore:undefined,after:responseSchema??outputSchema?validatorMiddlewareAfter:undefined}};const compile=(schema,ajvOptions,ajvInstance=null)=>{if(typeof schema==="function"||!schema)return schema;const options={...ajvDefaults,...ajvOptions};if(!ajv){ajv=ajvInstance??new Ajv(options);formats(ajv);formatsDraft2019(ajv)}else if(!ajvInstance){ajv.opts={...ajv.opts,...options}}return ajv.compile(schema)};const languageNormalizationMap={pt:"pt-BR","pt-br":"pt-BR",pt_BR:"pt-BR",pt_br:"pt-BR","zh-tw":"zh-TW",zh_TW:"zh-TW",zh_tw:"zh-TW"};const normalizePreferredLanguage=lang=>languageNormalizationMap[lang]??lang;const availableLanguages=Object.keys(localize);const chooseLanguage=({preferredLanguage},defaultLanguage)=>{if(preferredLanguage){const lang=normalizePreferredLanguage(preferredLanguage);if(availableLanguages.includes(lang)){return lang}}return defaultLanguage};export default validatorMiddleware
23
2
 
24
- const validatorMiddleware = (opts = {}) => {
25
- let {
26
- inputSchema,
27
- outputSchema,
28
- ajvOptions,
29
- ajvInstance,
30
- defaultLanguage,
31
- i18nEnabled
32
- } = { ...defaults,
33
- ...opts
34
- };
35
- inputSchema = compile(inputSchema, ajvOptions, ajvInstance);
36
- outputSchema = compile(outputSchema, ajvOptions, ajvInstance);
37
-
38
- const validatorMiddlewareBefore = async request => {
39
- const valid = inputSchema(request.event);
40
-
41
- if (!valid) {
42
- if (i18nEnabled) {
43
- const language = chooseLanguage(request.event, defaultLanguage);
44
- localize[language](inputSchema.errors);
45
- }
46
-
47
- const error = createError(400, 'Event object failed validation');
48
- error.cause = inputSchema.errors;
49
- throw error;
50
- }
51
- };
52
-
53
- const validatorMiddlewareAfter = async request => {
54
- const valid = outputSchema(request.response);
55
-
56
- if (!valid) {
57
- const error = createError(500, 'Response object failed validation');
58
- error.cause = outputSchema.errors;
59
- throw error;
60
- }
61
- };
62
-
63
- return {
64
- before: inputSchema ? validatorMiddlewareBefore : undefined,
65
- after: outputSchema ? validatorMiddlewareAfter : undefined
66
- };
67
- };
68
-
69
- const compile = (schema, ajvOptions, ajvInstance = null) => {
70
- if (typeof schema === 'function' || !schema) return schema;
71
- const options = { ...ajvDefaults,
72
- ...ajvOptions
73
- };
74
-
75
- if (!ajv) {
76
- ajv = ajvInstance ?? new Ajv(options);
77
- formats(ajv);
78
- formatsDraft2019(ajv);
79
- } else if (!ajvInstance) {
80
- ajv.opts = { ...ajv.opts,
81
- ...options
82
- };
83
- }
84
-
85
- return ajv.compile(schema);
86
- };
87
-
88
- const languageNormalizationMap = {
89
- pt: 'pt-BR',
90
- 'pt-br': 'pt-BR',
91
- pt_BR: 'pt-BR',
92
- pt_br: 'pt-BR',
93
- 'zh-tw': 'zh-TW',
94
- zh_TW: 'zh-TW',
95
- zh_tw: 'zh-TW'
96
- };
97
-
98
- const normalizePreferredLanguage = lang => languageNormalizationMap[lang] ?? lang;
99
-
100
- const availableLanguages = Object.keys(localize);
101
-
102
- const chooseLanguage = ({
103
- preferredLanguage
104
- }, defaultLanguage) => {
105
- if (preferredLanguage) {
106
- const lang = normalizePreferredLanguage(preferredLanguage);
107
-
108
- if (availableLanguages.includes(lang)) {
109
- return lang;
110
- }
111
- }
112
-
113
- return defaultLanguage;
114
- };
115
-
116
- export default validatorMiddleware;
3
+ //# sourceMappingURL=index.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/validator",
3
- "version": "3.0.0-alpha.6",
3
+ "version": "3.0.0",
4
4
  "description": "Validator middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -10,10 +10,17 @@
10
10
  "publishConfig": {
11
11
  "access": "public"
12
12
  },
13
- "exports": "./index.js",
13
+ "exports": {
14
+ ".": {
15
+ "import": "./index.js",
16
+ "require": "./index.cjs",
17
+ "types": "./index.d.ts"
18
+ }
19
+ },
14
20
  "types": "index.d.ts",
15
21
  "files": [
16
22
  "index.js",
23
+ "index.cjs",
17
24
  "index.d.ts"
18
25
  ],
19
26
  "scripts": {
@@ -45,18 +52,19 @@
45
52
  "bugs": {
46
53
  "url": "https://github.com/middyjs/middy/issues"
47
54
  },
48
- "homepage": "https://github.com/middyjs/middy#readme",
55
+ "homepage": "https://middy.js.org",
49
56
  "dependencies": {
50
- "@middy/util": "^3.0.0-alpha.6",
57
+ "@middy/util": "^3.0.0",
51
58
  "ajv": "8.9.0",
52
59
  "ajv-formats": "2.1.1",
53
60
  "ajv-formats-draft2019": "1.6.1",
54
- "ajv-i18n": "4.2.0"
61
+ "ajv-i18n": "4.2.0",
62
+ "ajv-keywords": "5.1.0"
55
63
  },
56
64
  "devDependencies": {
57
- "@middy/core": "^3.0.0-alpha.6",
65
+ "@middy/core": "^3.0.0",
58
66
  "@types/http-errors": "^1.8.1",
59
67
  "ajv-bsontype": "^1.0.7"
60
68
  },
61
- "gitHead": "176660ed3e0716d6bfb635c77251b301e0e24720"
69
+ "gitHead": "01520fa8628a36c2f89e126cad656a16547ea0d6"
62
70
  }