@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.
- package/index.js +57 -72
- package/package.json +5 -5
- 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
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
const
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
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
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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-
|
|
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-
|
|
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.
|
|
77
|
+
"fast-uri": "2.3.0"
|
|
78
78
|
},
|
|
79
79
|
"devDependencies": {
|
|
80
|
-
"@middy/core": "5.0.0-
|
|
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": "
|
|
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
|
|
24
|
-
import
|
|
25
|
-
import
|
|
26
|
-
import
|
|
27
|
-
import
|
|
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
|
-
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
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
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
-
|
|
72
|
-
|
|
73
|
-
|
|
67
|
+
export const transpileSchema = (schema, ajvOptions)=>{
|
|
68
|
+
const options = {
|
|
69
|
+
...ajvDefaults,
|
|
70
|
+
...ajvOptions
|
|
71
|
+
};
|
|
72
|
+
return compileSchema(schema, options);
|
|
73
|
+
};
|
|
74
|
+
|