@middy/validator 5.0.3 → 5.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.
Files changed (3) hide show
  1. package/index.js +72 -54
  2. package/package.json +4 -4
  3. package/transpile.js +70 -45
package/index.js CHANGED
@@ -1,57 +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.context.preferredLanguage] ?? languages[defaultLanguage];
19
- localize?.(eventSchema.errors);
20
- throw createError(400, 'Event object failed validation', {
21
- cause: {
22
- package: '@middy/validator',
23
- data: eventSchema.errors
24
- }
25
- });
26
- }
27
- }
28
- if (contextSchema) {
29
- const validContext = await contextSchema(request.context);
30
- if (!validContext) {
31
- throw createError(500, 'Context object failed validation', {
32
- cause: {
33
- package: '@middy/validator',
34
- data: contextSchema.errors
35
- }
36
- });
37
- }
38
- }
39
- };
40
- const validatorMiddlewareAfter = async (request)=>{
41
- const validResponse = await responseSchema(request.response);
42
- if (!validResponse) {
43
- throw createError(500, 'Response object failed validation', {
44
- cause: {
45
- package: '@middy/validator',
46
- data: responseSchema.errors
47
- }
48
- });
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
+ package: '@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
49
64
  }
50
- };
51
- return {
52
- before: eventSchema ?? contextSchema ? validatorMiddlewareBefore : undefined,
53
- after: responseSchema ? validatorMiddlewareAfter : undefined
54
- };
55
- };
56
- export default validatorMiddleware;
65
+ })
66
+ }
67
+ }
68
+ return {
69
+ before:
70
+ eventSchema ?? contextSchema ? validatorMiddlewareBefore : undefined,
71
+ after: responseSchema ? validatorMiddlewareAfter : undefined
72
+ }
73
+ }
57
74
 
75
+ export default validatorMiddleware
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@middy/validator",
3
- "version": "5.0.3",
3
+ "version": "5.2.0",
4
4
  "description": "Validator middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -67,7 +67,7 @@
67
67
  "url": "https://github.com/sponsors/willfarrell"
68
68
  },
69
69
  "dependencies": {
70
- "@middy/util": "5.0.3",
70
+ "@middy/util": "5.2.0",
71
71
  "ajv": "8.12.0",
72
72
  "ajv-errors": "3.0.0",
73
73
  "ajv-formats": "2.1.1",
@@ -77,10 +77,10 @@
77
77
  "fast-uri": "2.3.0"
78
78
  },
79
79
  "devDependencies": {
80
- "@middy/core": "5.0.3",
80
+ "@middy/core": "5.2.0",
81
81
  "@types/http-errors": "^2.0.0",
82
82
  "ajv-bsontype": "^1.0.7",
83
83
  "ajv-cmd": "0.3.4"
84
84
  },
85
- "gitHead": "87660575a7ac2b52e4153c407a4c63c9449dcd0d"
85
+ "gitHead": "2d9096a49cd8fb62359517be96d6c93609df41f0"
86
86
  }
package/transpile.js CHANGED
@@ -1,48 +1,73 @@
1
- import Ajv from 'ajv/dist/2020.js';
2
- import ajvFormats from 'ajv-formats';
3
- import ajvFormatsDraft2019 from 'ajv-formats-draft2019';
4
- import ajvKeywords from 'ajv-keywords';
5
- import ajvErrors from 'ajv-errors';
6
- import uriResolver from 'fast-uri';
7
- import { transpile } from 'ajv-ftl-i18n';
8
- export const transpileFTL = transpile;
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
+
9
38
  const defaultOptions = {
10
- uriResolver
11
- };
12
- const instance = (options = {})=>{
13
- options = {
14
- ...defaultOptions,
15
- ...options,
16
- keywords: []
17
- };
18
- const ajv = new Ajv(options);
19
- ajvFormats(ajv);
20
- ajvFormatsDraft2019(ajv);
21
- ajvKeywords(ajv);
22
- ajvErrors(ajv);
23
- return ajv;
24
- };
25
- const compileSchema = (schema, options = {})=>{
26
- options = {
27
- ...defaultOptions,
28
- ...options,
29
- keywords: []
30
- };
31
- const ajv = instance(options);
32
- return ajv.compile(schema);
33
- };
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
+
34
60
  const ajvDefaults = {
35
- strict: true,
36
- coerceTypes: 'array',
37
- allErrors: true,
38
- useDefaults: 'empty',
39
- messages: true
40
- };
41
- export const transpileSchema = (schema, ajvOptions)=>{
42
- const options = {
43
- ...ajvDefaults,
44
- ...ajvOptions
45
- };
46
- return compileSchema(schema, options);
47
- };
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
+ }
48
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
+ }