@middy/validator 5.0.0-alpha.1 → 5.0.0-rc.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 +57 -72
  2. package/package.json +5 -5
  3. package/transpile.js +46 -45
package/index.js CHANGED
@@ -1,75 +1,60 @@
1
- import { createError } from '@middy/util'
2
-
1
+ import { createError } from '@middy/util';
3
2
  const defaults = {
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
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
+ // Bad Request
21
+ throw createError(400, 'Event object failed validation', {
22
+ cause: {
23
+ pacakge: '@middy/validator',
24
+ data: eventSchema.errors
25
+ }
26
+ });
27
+ }
28
+ }
29
+ if (contextSchema) {
30
+ const validContext = await contextSchema(request.context);
31
+ if (!validContext) {
32
+ // Internal Server Error
33
+ throw createError(500, 'Context object failed validation', {
34
+ cause: {
35
+ package: '@middy/validator',
36
+ data: contextSchema.errors
37
+ }
38
+ });
39
+ }
40
+ }
41
+ };
42
+ const validatorMiddlewareAfter = async (request)=>{
43
+ const validResponse = await responseSchema(request.response);
44
+ if (!validResponse) {
45
+ // Internal Server Error
46
+ throw createError(500, 'Response object failed validation', {
47
+ cause: {
48
+ package: '@middy/validator',
49
+ data: responseSchema.errors
50
+ }
51
+ });
64
52
  }
65
- })
66
- }
67
- }
68
- return {
69
- before:
70
- eventSchema ?? contextSchema ? validatorMiddlewareBefore : undefined,
71
- after: responseSchema ? validatorMiddlewareAfter : undefined
72
- }
73
- }
53
+ };
54
+ return {
55
+ before: eventSchema ?? contextSchema ? validatorMiddlewareBefore : undefined,
56
+ after: responseSchema ? validatorMiddlewareAfter : undefined
57
+ };
58
+ };
59
+ export default validatorMiddleware;
74
60
 
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.1",
3
+ "version": "5.0.0-rc.0",
4
4
  "description": "Validator middleware for the middy framework",
5
5
  "type": "module",
6
6
  "engines": {
@@ -67,20 +67,20 @@
67
67
  "url": "https://github.com/sponsors/willfarrell"
68
68
  },
69
69
  "dependencies": {
70
- "@middy/util": "5.0.0-alpha.1",
70
+ "@middy/util": "5.0.0-rc.0",
71
71
  "ajv": "8.12.0",
72
72
  "ajv-errors": "3.0.0",
73
73
  "ajv-formats": "2.1.1",
74
74
  "ajv-formats-draft2019": "1.6.1",
75
75
  "ajv-ftl-i18n": "0.1.1",
76
76
  "ajv-keywords": "5.1.0",
77
- "fast-uri": "2.2.0"
77
+ "fast-uri": "2.3.0"
78
78
  },
79
79
  "devDependencies": {
80
- "@middy/core": "5.0.0-alpha.1",
80
+ "@middy/core": "5.0.0-rc.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": "ebce8d5df8783077fa49ba62ee9be20e8486a7f1"
85
+ "gitHead": "403c54ba9f05e038d1ee7541f9e4a7a6d46e9916"
86
86
  }
package/transpile.js CHANGED
@@ -18,56 +18,57 @@ export const transpileSchema = (schema, ajvOptions) => {
18
18
  }
19
19
 
20
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
-
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';
32
28
  // import transpileFTL from 'ajv-cmd/ftl'
33
- export const transpileFTL = transpile
34
-
29
+ export const transpileFTL = transpile;
35
30
  // *** Start `ajv-cmd/compile` *** //
36
31
  // import compileSchema from 'ajv-cmd/compile'
37
-
38
32
  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
- }
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
+ };
58
57
  // *** End `ajv-cmd/compile` *** //
59
-
60
58
  const ajvDefaults = {
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
- }
67
-
59
+ strict: true,
60
+ coerceTypes: 'array',
61
+ allErrors: true,
62
+ useDefaults: 'empty',
63
+ messages: true // needs to be true to allow multi-locale errorMessage to work
64
+ };
68
65
  // This is pulled out due to it's performance cost (50-100ms on cold start)
69
66
  // 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
- }
67
+ export const transpileSchema = (schema, ajvOptions)=>{
68
+ const options = {
69
+ ...ajvDefaults,
70
+ ...ajvOptions
71
+ };
72
+ return compileSchema(schema, options);
73
+ };
74
+