@bedrock/validation 6.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/.eslintrc.cjs ADDED
@@ -0,0 +1,12 @@
1
+ module.exports = {
2
+ root: true,
3
+ parserOptions: {
4
+ // this is required for dynamic import()
5
+ ecmaVersion: 2020
6
+ },
7
+ env: {
8
+ node: true
9
+ },
10
+ extends: ['digitalbazaar', 'digitalbazaar/jsdoc'],
11
+ ignorePatterns: ['node_modules/']
12
+ };
@@ -0,0 +1,67 @@
1
+ name: Bedrock Node.js CI
2
+
3
+ on: [push]
4
+
5
+ jobs:
6
+ lint:
7
+ runs-on: ubuntu-latest
8
+ timeout-minutes: 10
9
+ strategy:
10
+ matrix:
11
+ node-version: [14.x]
12
+ steps:
13
+ - uses: actions/checkout@v2
14
+ - name: Use Node.js ${{ matrix.node-version }}
15
+ uses: actions/setup-node@v1
16
+ with:
17
+ node-version: ${{ matrix.node-version }}
18
+ - run: npm install
19
+ - name: Run eslint
20
+ run: npm run lint
21
+ test-node:
22
+ needs: [lint]
23
+ runs-on: ubuntu-latest
24
+ timeout-minutes: 10
25
+ strategy:
26
+ matrix:
27
+ node-version: [14.x]
28
+ steps:
29
+ - uses: actions/checkout@v2
30
+ - name: Use Node.js ${{ matrix.node-version }}
31
+ uses: actions/setup-node@v1
32
+ with:
33
+ node-version: ${{ matrix.node-version }}
34
+ - run: |
35
+ npm install
36
+ cd test
37
+ npm install
38
+ - name: Run test with Node.js ${{ matrix.node-version }}
39
+ run: |
40
+ cd test
41
+ npm test
42
+ coverage:
43
+ needs: [test-node]
44
+ runs-on: ubuntu-latest
45
+ timeout-minutes: 10
46
+ strategy:
47
+ matrix:
48
+ node-version: [14.x]
49
+ steps:
50
+ - uses: actions/checkout@v2
51
+ - name: Use Node.js ${{ matrix.node-version }}
52
+ uses: actions/setup-node@v1
53
+ with:
54
+ node-version: ${{ matrix.node-version }}
55
+ - run: |
56
+ npm install
57
+ cd test
58
+ npm install
59
+ - name: Generate coverage report
60
+ run: |
61
+ cd test
62
+ npm run coverage-ci
63
+ - name: Upload coverage to Codecov
64
+ uses: codecov/codecov-action@v2
65
+ with:
66
+ file: ./test/coverage/lcov.info
67
+ fail_ci_if_error: true
package/CHANGELOG.md ADDED
@@ -0,0 +1,244 @@
1
+ # bedrock-validation ChangeLog
2
+
3
+ ## 6.0.0 - 2022-04-01
4
+
5
+ ### Changed
6
+ - **BREAKING**: Rename package to `@bedrock/server`.
7
+ - **BREAKING**: Convert to module (ESM).
8
+ - **BREAKING**: Remove default export.
9
+ - **BREAKING**: Require node 14.x.
10
+ - **BREAKING**: All APIs now use options-style/named parameters.
11
+ - **BREAKING**: When no schema can be found to match a given schema name,
12
+ the error returned from `validateInstance` is now of type `NotFoundError`
13
+ instead of `UnknownSchema`.
14
+
15
+ ### Removed
16
+ - **BREAKING**: Remove deprecated `validate` function. Use `validateInstance`
17
+ or `createValidateMiddleware`.
18
+ - **BREAKING**: All callback-style APIs/parameters have been removed. Functions
19
+ that validate instances are synchronous.
20
+ - **BREAKING**: Removed obsolete `graphSignature` schema.
21
+
22
+ ## 5.6.3 - 2022-03-24
23
+
24
+ ### Fixed
25
+ - Fix slow loading of schemas in same dir (load in parallel).
26
+
27
+ ## 5.6.2 - 2022-03-24
28
+
29
+ ### Fixed
30
+ - Handle case that import schema has no default export.
31
+
32
+ ## 5.6.1 - 2022-03-24
33
+
34
+ ### Fixed
35
+ - Update schemas to use esm style and `esm.js` to transpile
36
+ to CommonJS.
37
+
38
+ ## 5.6.0 - 2022-03-23
39
+
40
+ ### Changed
41
+ - Update peer deps:
42
+ - `bedrock@4.5`.
43
+ - Update internals to use esm style and use `esm.js` to
44
+ transpile to CommonJS.
45
+
46
+ ## 5.5.0 - 2022-02-17
47
+
48
+ ### Added
49
+ - Add `createValidateMiddleware` to create a more modern validate express-style
50
+ middleware. This function has more clear name, simpler parameters, and
51
+ ensures that the schemas used are compiled once. It requires schemas to
52
+ be passed directly, a string identifying a schema may not be used.
53
+
54
+ ## 5.4.1 - 2022-02-10
55
+
56
+ ### Fixed
57
+ - Ensure `title` is set on compiled function.
58
+
59
+ ## 5.4.0 - 2022-02-10
60
+
61
+ ### Added
62
+ - Add `compile()` function to pre-compile a schema and return a
63
+ function that outputs the same validation results as calling
64
+ `validateInstance`.
65
+
66
+ ## 5.3.0 - 2022-02-10
67
+
68
+ ### Changed
69
+ - Pre-compile schemas that are used in middlewares to prevent
70
+ compiling on every request.
71
+
72
+ ## 5.2.2 - 2021-12-15
73
+
74
+ ### Fixed
75
+ - Fix bug in `graphSignature` schema causing it to be invalid.
76
+
77
+ ## 5.2.1 - 2021-12-14
78
+
79
+ ### Fixed
80
+ - Fix `api.validate` to throw error if schema does not exist instead of
81
+ returning middleware that raises the error.
82
+
83
+ ## 5.2.0 - 2021-07-23
84
+
85
+ ### Changed
86
+ - Update peer dependencies; use bedrock@4.
87
+
88
+ ## 5.1.0 - 2021-06-30
89
+
90
+ ### Added
91
+ - Support `linkedDataSignature2020` schema generator.
92
+
93
+ ## 5.0.0 - 2021-03-19
94
+
95
+ ### Changed
96
+ - **BREAKING**: Email pattern no longer supports uppercase chars.
97
+
98
+ ## 4.5.0 - 2020-11-19
99
+
100
+ ### Added
101
+ - Implement a new ES6 Map based cache for AJV schemas. This provides a
102
+ significant peformance improvement over the default AJV caching mechanism.
103
+
104
+ ## 4.4.0 - 2020-04-27
105
+
106
+ ### Added
107
+ - Allow context in `DID Login` validator to be array with single context string.
108
+
109
+ ## 4.3.0 - 2020-03-30
110
+
111
+ ### Changed
112
+ - Increase character length of email domain labels per RFC 1034.
113
+
114
+ ## 4.2.0 - 2019-11-08
115
+
116
+ ### Changed
117
+ - Update dependencies.
118
+
119
+ ## 4.1.0 - 2019-01-23
120
+
121
+ ### Added
122
+ - Support verificationMethod in linkedDataSignature2018.
123
+
124
+ ## 4.0.1 - 2018-12-17
125
+
126
+ ### Fixed
127
+ - Add missing RsaSignature2018 type in LD signature schema.
128
+
129
+ ## 4.0.0 - 2018-11-28
130
+
131
+ ### Changed
132
+ - Remove `async` module dependency.
133
+ - Return value is {valid: <bool>, error: <error>}
134
+
135
+ ## 3.1.2 - 2018-09-17
136
+
137
+ ### Fixed
138
+ - Invalid `jsonldType` schema.
139
+
140
+ ## 3.1.1 - 2018-09-13
141
+
142
+ ### Fixed
143
+ - Improper use of arrow function in `validate` API.
144
+
145
+ ## 3.1.0 - 2018-09-13
146
+
147
+ ### Added
148
+ - Schema `linkedDataSignature2018`.
149
+
150
+ ### Changed
151
+ - Errors are now 2.x compatible.
152
+
153
+ ### Fixed
154
+ - Fixed test suite.
155
+
156
+ ## 3.0.0 - 2018-02-24
157
+ - Switch core validator to `ajv`.
158
+
159
+ ## 2.4.1 - 2018-06-28
160
+
161
+ ### Changed
162
+ - Use child logger.
163
+
164
+ ## 2.4.0 - 2018-05-10
165
+
166
+ ### Added
167
+ - Add `jsonPatch` and `sequencedPatch` validators.
168
+
169
+ ## 2.3.0 - 2017-09-06
170
+
171
+ ### Added
172
+ - Support multiple signatures in `linkedDataSignature` schema.
173
+
174
+ ## 2.2.1 - 2016-09-22
175
+
176
+ ### Changed
177
+ - Restructure test framework for CI.
178
+
179
+ ## 2.2.0 - 2016-07-22
180
+
181
+ ### Added
182
+ - Add `lowerCaseOnly` option to email validator.
183
+
184
+ ## 2.1.1 - 2016-07-20
185
+
186
+ ### Changed
187
+ - Replace deprecated `GLOBAL` with `global`.
188
+
189
+ ## 2.1.0 - 2016-07-06
190
+
191
+ ### Added
192
+ - Add `linkedDataSignature` schema.
193
+
194
+ ## 2.0.3 - 2016-06-13
195
+
196
+ ### Fixed
197
+ - Accept dates before the year 2000 (1000+). A future fix should
198
+ also accept dates 0-1000.
199
+
200
+ ## 2.0.2 - 2016-03-15
201
+
202
+ ### Changed
203
+ - Update bedrock dependencies.
204
+
205
+ ### Changed
206
+ - Use jsonldContext schema to validate Credential @context.
207
+
208
+ ## 2.0.1 - 2016-03-09
209
+
210
+ ### Changed
211
+ - Update publicKeyPem schema to accept RSA PUBLIC KEY.
212
+
213
+ ## 2.0.0 - 2016-03-02
214
+
215
+ ### Changed
216
+ - Update package dependencies for npm v3 compatibility.
217
+
218
+ ## 1.0.3 - 2016-01-06
219
+
220
+ ### Fixed
221
+ - Fix synchronous error emission when invalid schema is specified.
222
+
223
+ ## 1.0.2 - 2015-10-13
224
+
225
+ ### Fixed
226
+ - Fix credential schema bugs/style.
227
+
228
+ ## 1.0.1 - 2015-05-07
229
+
230
+ ### Added
231
+ - Support validating JSON-LD context arrays.
232
+
233
+ ### Changed
234
+ - Accept and ignore null as jsonldContext context param.
235
+
236
+ ## 1.0.0 - 2015-04-08
237
+
238
+ ### Changed
239
+ - Labels/titles: Allow double quotes.
240
+ - Lables: Allow length of 200 (up from 32).
241
+
242
+ ## 0.1.0 (up to early 2015)
243
+
244
+ - See git history for changes.
package/LICENSE.md ADDED
@@ -0,0 +1,115 @@
1
+ Bedrock Non-Commercial License v1.0
2
+ ===================================
3
+
4
+ Copyright (c) 2011-2020 Digital Bazaar, Inc.
5
+ All rights reserved.
6
+
7
+ Summary
8
+ =======
9
+
10
+ This license allows the licensee to use Bedrock and its software modules
11
+ for non-commercial purposes such as self-study, research, personal
12
+ projects, or for evaluation purposes. If the licensee uses Bedrock
13
+ directly or indirectly to generate revenue, or to provide products or
14
+ services to more than 500 people (users), the licensee must immediately
15
+ obtain a non-profit or commercial license.
16
+
17
+ Examples
18
+ ========
19
+
20
+ These are examples of cases that are allowed by this license:
21
+
22
+ * The licensee is an individual that creates Bedrock-dependent software for
23
+ personal use only.
24
+ * The licensee is an individual or group of students/researchers that uses
25
+ Bedrock to experiment with an idea for a non-commercial project.
26
+ * The licensee is a startup company that prototypes a Bedrock-dependent
27
+ product before they have cash flow and will be testing the prototype
28
+ software with less than 500 users. The service will not generate revenue
29
+ of any kind.
30
+ * The licensee is a for-profit organization that creates a product or
31
+ service that is used by less than 500 users and is built with or
32
+ integrates with Bedrock. The service must be exclusively provided for free
33
+ and no parent, subsidiary, agent, or affiliate organization may profit
34
+ from its use.
35
+
36
+ These cases require a non-profit or commercial license:
37
+
38
+ * The licensee is a non-profit that receives funding to create and/or run a
39
+ Bedrock-dependent service.
40
+ * The licensee is a startup company with Bedrock-dependent software that is
41
+ funded by another organization.
42
+ * The licensee is a startup company that is going into production with
43
+ Bedrock-dependent software.
44
+ * The licensee has more than 500 users using a Bedrock-dependent service
45
+ either directly or indirectly.
46
+ * The licensee is a medium to large organization that builds or integrates a
47
+ commercial product or service with Bedrock.
48
+
49
+ THE LICENSE
50
+ ===========
51
+
52
+ This section and all subsequent sections of this document constitute the
53
+ agreement between the licensee and Digital Bazaar, Inc.
54
+
55
+ DEFINITIONS
56
+ ===========
57
+
58
+ * Product - The Bedrock software and any modules associated with Bedrock
59
+ where Digital Bazaar, Inc. owns the copyright.
60
+
61
+ CONDITIONS
62
+ ==========
63
+
64
+ Redistribution and use in source and binary forms, with or without
65
+ modification, are permitted for NON-COMMERCIAL PURPOSES as long as the
66
+ following conditions are met:
67
+
68
+ 1. Any use of the Product must not generate revenue for the licensee or
69
+ any parent, subsidiary, agent, or affiliate of the licensee. Use of
70
+ Product includes, but is not limited to, interacting with any of the
71
+ licensee's Product-dependent products or services over a network.
72
+
73
+ 2. The aggregate number of individual people (users) of the licensee's
74
+ products or services that use Product must be less than 500.
75
+
76
+ 3. Redistributions of source code must retain the above copyright notice
77
+ intact, this list of conditions and the following disclaimer.
78
+
79
+ 4. Redistributions in binary form must reproduce the above copyright
80
+ notice, this license and the following disclaimer in the documentation and
81
+ on a web page available via interactive use and/or other materials
82
+ provided with the distribution.
83
+
84
+ 5. Neither the name of the copyright holder, the names of its
85
+ contributors, nor any trademarks held by the copyright holder may be used
86
+ to endorse or promote products or services built using the Product without
87
+ specific prior written permission.
88
+
89
+ 6. Any modifications are clearly outlined in release documentation and are
90
+ specifically mentioned as not being a part of an official Product release.
91
+ No additional restrictions to this license may be made when distributing
92
+ modifications.
93
+
94
+ 7. For the avoidance of doubt, this license prohibits sublicensing of the
95
+ Product.
96
+
97
+ 8. Any breach of this license by licensee must be resolved within 30 days.
98
+ Failure to do so results in the termination of this license.
99
+
100
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
101
+ IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
102
+ THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
103
+ PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
104
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
105
+ EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
106
+ PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
107
+ PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
108
+ LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
109
+ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
110
+ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
111
+
112
+ To obtain a non-profit or commercial license for Product, please contact
113
+ Digital Bazaar, Inc. at the following email address:
114
+
115
+ Digital Bazaar <support@digitalbazaar.com>
package/README.md ADDED
@@ -0,0 +1,92 @@
1
+ # bedrock-validation
2
+
3
+ [![Build Status](http://ci.digitalbazaar.com/buildStatus/icon?job=bedrock-validation)](http://ci.digitalbazaar.com/job/bedrock-validation)
4
+
5
+ A [bedrock][] module that provides a express middleware and an API for
6
+ validating data structures and their contents. **bedrock-validation** uses
7
+ schemas based on [JSON schema][].
8
+
9
+ ## Requirements
10
+
11
+ - npm v6+
12
+
13
+ ## Quick Examples
14
+
15
+ ```
16
+ npm install @bedrock/validation
17
+ ```
18
+
19
+ ```js
20
+ import * as bedrock from '@bedrock/core';
21
+ import {validate} from '@bedrock/validation';
22
+
23
+ // load schemas from '/foo'
24
+ bedrock.config.validation.schema.paths.push('/foo');
25
+
26
+ // add an express route with validation middleware
27
+ bedrock.events.on('bedrock-express.configure.routes', function(app) {
28
+ app.post('/bar',
29
+ // validate the query using the 'postBarQueryValidator'
30
+ // validate the response body using the 'postBarValidator'
31
+ validate({query: 'postBarQueryValidator', body: 'postBarValidator'}),
32
+ function(req, res) {
33
+ // do something
34
+ });
35
+ });
36
+
37
+ bedrock.start();
38
+ ```
39
+
40
+ ## Configuration
41
+
42
+ **bedrock-validation** will, on initialization (via the `bedrock.init` event),
43
+ read any schemas found in the list of paths specified in
44
+ `bedrock.config.validation.schema.paths`. Individual schemas can be skipped
45
+ via the `bedrock.config.validation.schema.skip` configuration option. If any
46
+ schema name matches a previously loaded schema, it will override that
47
+ schema.
48
+
49
+ For more documentation on configuration, see [config.js](./lib/config.js).
50
+
51
+ ## API
52
+
53
+ ### validate(name, [data])
54
+
55
+ This method may be called with either one or two parameters.
56
+
57
+ If only one parameter is given:
58
+
59
+ * The method returns express middleware that will be used to validate a request
60
+ using the schema associated with the given name.
61
+ * If a string is provided for the first parameter, then it will be used as the
62
+ schema name for validating the request body.
63
+ * If an object is provided for the first parameter, then the object can contain
64
+ `body` and `query` schema names as properties of the object.
65
+
66
+ If two parameters are given:
67
+
68
+ * The first parameter must be a string and the second parameter must be the
69
+ data to validate. The return value will contain the result of the validation.
70
+
71
+ ### getSchema(name)
72
+
73
+ Retrieves a validation schema given a `name` for the schema. If no such
74
+ schema exists, `null` is returned.
75
+
76
+ ### validateInstance(instance, schema)
77
+
78
+ Validates an `instance` (data) against a `schema`. This method may be used
79
+ to validate data using a schema that wasn't necessarily registered via
80
+ the configuration system. The `schema` must be a [JSON schema][] instance. The
81
+ return value will contain the result of the validation. This function returns
82
+ a promise that resolves to:
83
+ ```js
84
+ {
85
+ valid: <boolean>,
86
+ error: <error> // only present when valid === false
87
+ }
88
+ ```
89
+
90
+
91
+ [bedrock]: https://github.com/digitalbazaar/bedrock
92
+ [JSON schema]: http://json-schema.org/
package/lib/Cache.js ADDED
@@ -0,0 +1,24 @@
1
+ /*!
2
+ * Copyright (c) 2012-2022 Digital Bazaar, Inc. All rights reserved.
3
+ */
4
+ export class Cache {
5
+ constructor() {
6
+ this._cache = new Map();
7
+ }
8
+
9
+ put(key, value) {
10
+ this._cache.set(key, value);
11
+ }
12
+
13
+ get(key) {
14
+ return this._cache.get(key);
15
+ }
16
+
17
+ del(key) {
18
+ this._cache.delete(key);
19
+ }
20
+
21
+ clear() {
22
+ this._cache.clear();
23
+ }
24
+ }
package/lib/config.js ADDED
@@ -0,0 +1,18 @@
1
+ /*!
2
+ * Copyright (c) 2012-2022 Digital Bazaar, Inc. All rights reserved.
3
+ */
4
+ import {config} from '@bedrock/core';
5
+ import {fileURLToPath} from 'url';
6
+ import path from 'path';
7
+
8
+ const __dirname = path.dirname(fileURLToPath(import.meta.url));
9
+
10
+ config.validation = {};
11
+ config.validation.schema = {};
12
+ config.validation.schema.paths = [];
13
+ config.validation.schema.skip = [];
14
+
15
+ // common validation schemas
16
+ config.validation.schema.paths.push(
17
+ path.join(__dirname, '..', 'schemas')
18
+ );