electrodb 1.5.0 → 1.6.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/CHANGELOG.md +18 -6
- package/README.md +87 -46
- package/index.d.ts +39 -0
- package/package.json +1 -1
- package/src/errors.js +92 -8
- package/src/schema.js +101 -50
package/CHANGELOG.md
CHANGED
|
@@ -98,28 +98,40 @@ All notable changes to this project will be documented in this file. Breaking ch
|
|
|
98
98
|
### Patched
|
|
99
99
|
- Updates did not include composite attributes involved in primary index. Though these values cannot be changed, they should be `set` on update method calls in case the update results in an item insert. [[read more]](./README.md#updates-to-composite-attributes)
|
|
100
100
|
|
|
101
|
-
## [1.4.5]
|
|
101
|
+
## [1.4.5] - 2021-10-17
|
|
102
102
|
### Fixed
|
|
103
103
|
- Improved .npmignore to remove playground oriented files, and created official directory to keep playground in sync with library changes.
|
|
104
104
|
|
|
105
|
-
## [1.4.6]
|
|
105
|
+
## [1.4.6] - 2021-10-20
|
|
106
106
|
### Added, Fixed
|
|
107
107
|
- Adding Entity identifiers to all update operations. When primary index composite attributes were added in 1.4.4, entities were written properly but did not include the identifiers. This resulted in entities being written but not being readable without the query option `ignoreOwnership` being used.
|
|
108
108
|
|
|
109
|
-
## [1.4.7]
|
|
109
|
+
## [1.4.7] - 2021-10-20
|
|
110
110
|
### Changed
|
|
111
111
|
- Using `add()` update mutation now resolves to `ADD #prop :prop` update expression instead of a `SET #prop = #prop + :prop`
|
|
112
112
|
|
|
113
113
|
### Fixed
|
|
114
114
|
- Fixed param naming conflict during updates, when map attribute shares a name with another (separate) attribute.
|
|
115
115
|
|
|
116
|
-
## [1.4.8]
|
|
116
|
+
## [1.4.8] - 2021-11-01
|
|
117
117
|
### Fixed
|
|
118
118
|
- Addressed issue#90 to flip batchGet's response tuple type definition.
|
|
119
119
|
|
|
120
|
-
## [1.5.0]
|
|
120
|
+
## [1.5.0] - 2021-11-07
|
|
121
121
|
### Changed
|
|
122
122
|
- Queries will now fully paginate all responses. Prior to this change, ElectroDB would only return items from a single ElectroDB query result. Now ElectroDB will paginate through all query results. This will impact both uses of entity queries and service collections. [[read more](./README.md#query-method)]
|
|
123
123
|
- The query option `limit` has an extended meaning with the change to automatically paginate records on query. The option `limit` now represents a target for the number of items to return from DynamoDB. If this option is passed, Queries on entities and through collections will paginate DynamoDB until this limit is reached or all items for that query have been returned. [[read more](./README.md#query-options)]
|
|
124
|
+
|
|
125
|
+
### Added
|
|
126
|
+
- A new query option `pages` has been added to coincide with the change to automatically paginate all records when queried. The `pages` option sets a max number of pagination iterations ElectroDB will perform on a query. When this option is paired with `limit`, ElectroDB will respect the first condition reached. [[read more](./README.md#query-options)]
|
|
127
|
+
|
|
128
|
+
## [1.6.0] - 2021-11-21
|
|
124
129
|
### Added
|
|
125
|
-
-
|
|
130
|
+
- Exporting TypeScript interfaces for `ElectroError` and `ElectroValidationError`
|
|
131
|
+
- Errors thrown within an attribute's validate callback are now wrapped and accessible after being thrown. Prior to this change, only the `message` of the error thrown by a validation function was persisted back through to the user, now the error itself is also accessible. Reference the exported interface typedef for `ElectroValidationError` [here](./index.d.ts) to see the new properties available on a thrown validation error.
|
|
132
|
+
|
|
133
|
+
### Changed
|
|
134
|
+
- As a byproduct of enhancing validation errors, the format of message text on a validation error has changed. This could be breaking if your app had a hardcoded dependency on the exact text of a thrown validation error.
|
|
135
|
+
|
|
136
|
+
### Fixed
|
|
137
|
+
- For Set attributes, the callback functions `get`, `set`, and `validate` are now consistently given an Array of values. These functions would sometimes (incorrectly) be called with a DynamoDB DocClient Set.
|
package/README.md
CHANGED
|
@@ -107,11 +107,11 @@ tasks
|
|
|
107
107
|
* [TypeScript Support](#typescript-support)
|
|
108
108
|
+ [TypeScript Services](#typescript-services)
|
|
109
109
|
* [Join](#join)
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
110
|
+
- [Independent Models](#independent-models)
|
|
111
|
+
- [Joining Entity instances to a Service](#joining-entity-instances-to-a-service)
|
|
112
|
+
- [Joining models to a Service](#joining-models-to-a-service)
|
|
113
|
+
- [Joining Entities or Models with an alias](#joining-entities-or-models-with-an-alias)
|
|
114
|
+
- [Joining Entities at Service construction for TypeScript](#joining-entities-at-service-construction-for-typescript)
|
|
115
115
|
* [Model](#model)
|
|
116
116
|
+ [Model Properties](#model-properties)
|
|
117
117
|
+ [Service Options](#service-options)
|
|
@@ -125,7 +125,7 @@ tasks
|
|
|
125
125
|
- [Set Attributes](#set-attributes)
|
|
126
126
|
- [Attribute Getters and Setters](#attribute-getters-and-setters)
|
|
127
127
|
- [Attribute Watching](#attribute-watching)
|
|
128
|
-
* [Attribute Watching: Watch All](#attribute-watching
|
|
128
|
+
* [Attribute Watching: Watch All](#attribute-watching--watch-all)
|
|
129
129
|
* [Attribute Watching Examples](#attribute-watching-examples)
|
|
130
130
|
- [Calculated Attributes](#calculated-attributes)
|
|
131
131
|
- [Virtual Attributes](#virtual-attributes)
|
|
@@ -147,7 +147,7 @@ tasks
|
|
|
147
147
|
+ [Collection Queries vs Entity Queries](#collection-queries-vs-entity-queries)
|
|
148
148
|
+ [Collection Response Structure](#collection-response-structure)
|
|
149
149
|
* [Sub-Collections](#sub-collections)
|
|
150
|
-
|
|
150
|
+
- [Sub-Collection Entities](#sub-collection-entities)
|
|
151
151
|
* [Index and Collection Naming Conventions](#index-and-collection-naming-conventions)
|
|
152
152
|
+ [Index Naming Conventions](#index-naming-conventions)
|
|
153
153
|
* [Collection Naming Conventions](#collection-naming-conventions)
|
|
@@ -163,11 +163,11 @@ tasks
|
|
|
163
163
|
+ [Multiple Where Clauses](#multiple-where-clauses)
|
|
164
164
|
* [Parse](#parse)
|
|
165
165
|
- [Building Queries](#building-queries)
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
166
|
+
+ [Using composite attributes to make hierarchical keys](#using-composite-attributes-to-make-hierarchical-keys)
|
|
167
|
+
- [Shopping Mall Stores](#shopping-mall-stores)
|
|
168
|
+
+ [Query App Records](#query-app-records)
|
|
169
|
+
- [Partition Key Composite Attributes](#partition-key-composite-attributes)
|
|
170
|
+
+ [Sort Key Operations](#sort-key-operations)
|
|
171
171
|
* [Query Chains](#query-chains)
|
|
172
172
|
+ [Query Method](#query-method)
|
|
173
173
|
+ [Get Method](#get-method)
|
|
@@ -178,14 +178,14 @@ tasks
|
|
|
178
178
|
+ [Batch Write Put Records](#batch-write-put-records)
|
|
179
179
|
+ [Update Record](#update-record)
|
|
180
180
|
- [Updates to Composite Attributes](#updates-to-composite-attributes)
|
|
181
|
-
- [Update Method: Set](#update-method
|
|
182
|
-
- [Update Method: Remove](#update-method
|
|
183
|
-
- [Update Method: Add](#update-method
|
|
184
|
-
- [Update Method: Subtract](#update-method
|
|
185
|
-
- [Update Method: Append](#update-method
|
|
186
|
-
- [Update Method: Delete](#update-method
|
|
187
|
-
- [Update Method: Data](#update-method
|
|
188
|
-
+ [Update Method: Complex Data Types](#update-method
|
|
181
|
+
- [Update Method: Set](#update-method--set)
|
|
182
|
+
- [Update Method: Remove](#update-method--remove)
|
|
183
|
+
- [Update Method: Add](#update-method--add)
|
|
184
|
+
- [Update Method: Subtract](#update-method--subtract)
|
|
185
|
+
- [Update Method: Append](#update-method--append)
|
|
186
|
+
- [Update Method: Delete](#update-method--delete)
|
|
187
|
+
- [Update Method: Data](#update-method--data)
|
|
188
|
+
+ [Update Method: Complex Data Types](#update-method--complex-data-types)
|
|
189
189
|
+ [Scan Records](#scan-records)
|
|
190
190
|
+ [Remove Method](#remove-method)
|
|
191
191
|
+ [Patch Record](#patch-record)
|
|
@@ -205,30 +205,33 @@ tasks
|
|
|
205
205
|
* [Pagination Example](#pagination-example)
|
|
206
206
|
* [Query Examples](#query-examples)
|
|
207
207
|
* [Query Options](#query-options)
|
|
208
|
-
- [Errors:](#errors)
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
208
|
+
- [Errors:](#errors-)
|
|
209
|
+
+ [No Client Defined On Model](#no-client-defined-on-model)
|
|
210
|
+
+ [Invalid Identifier](#invalid-identifier)
|
|
211
|
+
+ [Invalid Key Composite Attribute Template](#invalid-key-composite-attribute-template)
|
|
212
|
+
+ [Duplicate Indexes](#duplicate-indexes)
|
|
213
|
+
+ [Collection Without An SK](#collection-without-an-sk)
|
|
214
|
+
+ [Duplicate Collections](#duplicate-collections)
|
|
215
|
+
+ [Missing Primary Index](#missing-primary-index)
|
|
216
|
+
+ [Invalid Attribute Definition](#invalid-attribute-definition)
|
|
217
|
+
+ [Invalid Model](#invalid-model)
|
|
218
|
+
+ [Invalid Options](#invalid-options)
|
|
219
|
+
+ [Duplicate Index Fields](#duplicate-index-fields)
|
|
220
|
+
+ [Duplicate Index Composite Attributes](#duplicate-index-composite-attributes)
|
|
221
|
+
+ [Incompatible Key Composite Attribute Template](#incompatible-key-composite-attribute-template)
|
|
222
|
+
+ [Invalid Index With Attribute Name](#invalid-index-with-attribute-name)
|
|
223
|
+
+ [Invalid Collection on Index With Attribute Field Names](#invalid-collection-on-index-with-attribute-field-names)
|
|
224
|
+
+ [Missing Composite Attributes](#missing-composite-attributes)
|
|
225
|
+
+ [Missing Table](#missing-table)
|
|
226
|
+
+ [Invalid Concurrency Option](#invalid-concurrency-option)
|
|
227
|
+
+ [Invalid Pages Option](#invalid-pages-option)
|
|
228
|
+
+ [Invalid Limit Option](#invalid-limit-option)
|
|
229
|
+
+ [Invalid Attribute](#invalid-attribute)
|
|
230
|
+
+ [AWS Error](#aws-error)
|
|
231
|
+
+ [Unknown Errors](#unknown-errors)
|
|
232
|
+
+ [Invalid Last Evaluated Key](#invalid-last-evaluated-key)
|
|
233
|
+
+ [No Owner For Pager](#no-owner-for-pager)
|
|
234
|
+
+ [Pager Not Unique](#pager-not-unique)
|
|
232
235
|
- [Examples](#examples)
|
|
233
236
|
* [Employee App](#employee-app)
|
|
234
237
|
+ [Employee App Requirements](#employee-app-requirements)
|
|
@@ -4572,7 +4575,45 @@ When performing a query [Query](#building-queries) you can pass a [Query Options
|
|
|
4572
4575
|
*What to do about it:*
|
|
4573
4576
|
Expect this error only if you're providing a `limit` option. Double-check the value you are providing is the value you expect to be passing, and that the value passes the tests listed above.
|
|
4574
4577
|
|
|
4575
|
-
###
|
|
4578
|
+
### Invalid Attribute
|
|
4579
|
+
*Code: 3001*
|
|
4580
|
+
|
|
4581
|
+
*Why this occurred:*
|
|
4582
|
+
The value received for a validation either failed type expectations (e.g. a "number" instead of a "string"), or the user provided "validate" callback on an attribute rejected a value.
|
|
4583
|
+
|
|
4584
|
+
*What to do about it:*
|
|
4585
|
+
Examine the error itself for more precise detail on why the failure occurred. The error object itself should have a property called "fields" which contains an array of every attribute that failed validation, and a reason for each. If the failure originated from a "validate" callback, the originally thrown error will be accessible via the `cause` property the corresponding element within the fields array.1
|
|
4586
|
+
|
|
4587
|
+
Below is the type definition for an ElectroValidationError:
|
|
4588
|
+
|
|
4589
|
+
```typescript
|
|
4590
|
+
ElectroValidationError<T extends Error = Error> extends ElectroError {
|
|
4591
|
+
readonly name: "ElectroValidationError"
|
|
4592
|
+
readonly fields: ReadonlyArray<{
|
|
4593
|
+
/**
|
|
4594
|
+
* The json path to the attribute that had a validation error
|
|
4595
|
+
*/
|
|
4596
|
+
readonly field: string;
|
|
4597
|
+
|
|
4598
|
+
/**
|
|
4599
|
+
* A description of the validation error for that attribute
|
|
4600
|
+
*/
|
|
4601
|
+
readonly reason: string;
|
|
4602
|
+
|
|
4603
|
+
/**
|
|
4604
|
+
* Index of the value passed (present only in List attribute validation errors)
|
|
4605
|
+
*/
|
|
4606
|
+
readonly index: number | undefined;
|
|
4607
|
+
|
|
4608
|
+
/**
|
|
4609
|
+
* The error thrown from the attribute's validate callback (if applicable)
|
|
4610
|
+
*/
|
|
4611
|
+
readonly cause: T | undefined;
|
|
4612
|
+
}>
|
|
4613
|
+
}
|
|
4614
|
+
```
|
|
4615
|
+
|
|
4616
|
+
### AWS Error
|
|
4576
4617
|
*Code: 4001*
|
|
4577
4618
|
|
|
4578
4619
|
*Why this occurred:*
|
|
@@ -5497,4 +5538,4 @@ This change stems from the fact the `facets` is already a defined term in the Dy
|
|
|
5497
5538
|
1.0.0 brings back a `null` response from the `get()` method when a record could not be found. Prior to `1.0.0` ElectroDB returned an empty object.
|
|
5498
5539
|
|
|
5499
5540
|
# Coming Soon
|
|
5500
|
-
- Default query options defined on the `model` to give more general control of interactions with the Entity.
|
|
5541
|
+
- Default query options defined on the `model` to give more general control of interactions with the Entity.
|
package/index.d.ts
CHANGED
|
@@ -1,6 +1,45 @@
|
|
|
1
1
|
declare const WhereSymbol: unique symbol;
|
|
2
2
|
declare const UpdateDataSymbol: unique symbol;
|
|
3
3
|
|
|
4
|
+
export interface ElectroError extends Error {
|
|
5
|
+
readonly name: 'ElectroError';
|
|
6
|
+
readonly code: number;
|
|
7
|
+
readonly date: number;
|
|
8
|
+
readonly isElectroError: boolean;
|
|
9
|
+
ref: {
|
|
10
|
+
readonly code: number;
|
|
11
|
+
readonly section: string;
|
|
12
|
+
readonly name: string;
|
|
13
|
+
readonly sym: unique symbol;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface ElectroValidationErrorFieldReference<T extends Error = Error> {
|
|
18
|
+
/**
|
|
19
|
+
* The json path to the attribute that had a validation error
|
|
20
|
+
*/
|
|
21
|
+
readonly field: string;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* A description of the validation error for that attribute
|
|
25
|
+
*/
|
|
26
|
+
readonly reason: string;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Index of the value passed (present only in List attribute validation errors)
|
|
30
|
+
*/
|
|
31
|
+
readonly index: number | undefined;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The error thrown from the attribute's validate callback (if applicable)
|
|
35
|
+
*/
|
|
36
|
+
readonly cause: T | undefined;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ElectroValidationError<T extends Error = Error> extends ElectroError {
|
|
40
|
+
readonly fields: ReadonlyArray<ElectroValidationErrorFieldReference<T>>;
|
|
41
|
+
}
|
|
42
|
+
|
|
4
43
|
interface ReadOnlyAttribute {
|
|
5
44
|
readonly readOnly: true;
|
|
6
45
|
}
|
package/package.json
CHANGED
package/src/errors.js
CHANGED
|
@@ -207,28 +207,112 @@ const ErrorCodes = {
|
|
|
207
207
|
},
|
|
208
208
|
};
|
|
209
209
|
|
|
210
|
+
function makeMessage(message, section) {
|
|
211
|
+
return `${message} - For more detail on this error reference: ${getHelpLink(section)}`
|
|
212
|
+
}
|
|
213
|
+
|
|
210
214
|
class ElectroError extends Error {
|
|
211
|
-
constructor(
|
|
215
|
+
constructor(code, message) {
|
|
212
216
|
super(message);
|
|
213
217
|
let detail = ErrorCodes.UnknownError;
|
|
214
|
-
if (
|
|
215
|
-
detail =
|
|
218
|
+
if (code && code.sym === ErrorCode) {
|
|
219
|
+
detail = code
|
|
216
220
|
}
|
|
217
|
-
this.
|
|
218
|
-
|
|
221
|
+
this._message = message;
|
|
222
|
+
// this.message = `${message} - For more detail on this error reference: ${getHelpLink(detail.section)}`;
|
|
223
|
+
this.message = makeMessage(message, detail.section);
|
|
219
224
|
if (Error.captureStackTrace) {
|
|
220
225
|
Error.captureStackTrace(this, ElectroError);
|
|
221
226
|
}
|
|
222
227
|
|
|
223
228
|
this.name = 'ElectroError';
|
|
224
|
-
this.ref =
|
|
229
|
+
this.ref = code;
|
|
225
230
|
this.code = detail.code;
|
|
226
|
-
this.date =
|
|
231
|
+
this.date = Date.now();
|
|
227
232
|
this.isElectroError = true;
|
|
228
233
|
}
|
|
229
234
|
}
|
|
230
235
|
|
|
236
|
+
class ElectroValidationError extends ElectroError {
|
|
237
|
+
constructor(errors = []) {
|
|
238
|
+
const fields = [];
|
|
239
|
+
const messages = [];
|
|
240
|
+
for (let i = 0; i < errors.length; i++) {
|
|
241
|
+
const error = errors[i];
|
|
242
|
+
const message = error ? (error._message || error.message) : undefined;
|
|
243
|
+
messages.push(message);
|
|
244
|
+
if (error instanceof ElectroUserValidationError) {
|
|
245
|
+
fields.push({
|
|
246
|
+
field: error.field,
|
|
247
|
+
index: error.index,
|
|
248
|
+
reason: message,
|
|
249
|
+
cause: error.cause,
|
|
250
|
+
type: 'validation'
|
|
251
|
+
});
|
|
252
|
+
} else if (error instanceof ElectroAttributeValidationError) {
|
|
253
|
+
fields.push({
|
|
254
|
+
field: error.field,
|
|
255
|
+
index: error.index,
|
|
256
|
+
reason: message,
|
|
257
|
+
cause: error.cause || error, // error | undefined
|
|
258
|
+
type: 'validation'
|
|
259
|
+
});
|
|
260
|
+
} else if (message) {
|
|
261
|
+
fields.push({
|
|
262
|
+
field: '',
|
|
263
|
+
index: error.index,
|
|
264
|
+
reason: message,
|
|
265
|
+
cause: error !== undefined ? error.cause || error : undefined,
|
|
266
|
+
type: 'fatal'
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const message = messages
|
|
272
|
+
.filter(message => typeof message === "string" && message.length)
|
|
273
|
+
.join(', ') || `Invalid value(s) provided`;
|
|
274
|
+
|
|
275
|
+
super(ErrorCodes.InvalidAttribute, message);
|
|
276
|
+
this.fields = fields;
|
|
277
|
+
this.name = "ElectroValidationError";
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
class ElectroUserValidationError extends ElectroError {
|
|
282
|
+
constructor(field, cause) {
|
|
283
|
+
let message;
|
|
284
|
+
let hasCause = false;
|
|
285
|
+
if (typeof cause === "string") {
|
|
286
|
+
message = cause;
|
|
287
|
+
} else if (cause !== undefined && typeof cause._message === "string" && cause._message.length) {
|
|
288
|
+
message = cause._message;
|
|
289
|
+
hasCause = true;
|
|
290
|
+
} else if (cause !== undefined && typeof cause.message === "string" && cause.message.length) {
|
|
291
|
+
message = cause.message;
|
|
292
|
+
hasCause = true;
|
|
293
|
+
} else {
|
|
294
|
+
message = "Invalid value provided";
|
|
295
|
+
}
|
|
296
|
+
super(ErrorCodes.InvalidAttribute, message);
|
|
297
|
+
this.field = field;
|
|
298
|
+
this.name = "ElectroUserValidationError";
|
|
299
|
+
if (hasCause) {
|
|
300
|
+
this.cause = cause;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
class ElectroAttributeValidationError extends ElectroError {
|
|
306
|
+
constructor(field, reason) {
|
|
307
|
+
super(ErrorCodes.InvalidAttribute, reason);
|
|
308
|
+
this.field = field;
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
|
|
231
312
|
module.exports = {
|
|
313
|
+
ErrorCodes,
|
|
232
314
|
ElectroError,
|
|
233
|
-
|
|
315
|
+
ElectroValidationError,
|
|
316
|
+
ElectroUserValidationError,
|
|
317
|
+
ElectroAttributeValidationError
|
|
234
318
|
};
|
package/src/schema.js
CHANGED
|
@@ -289,7 +289,7 @@ class Attribute {
|
|
|
289
289
|
|
|
290
290
|
_makeCast(name, cast) {
|
|
291
291
|
if (cast !== undefined && !CastTypes.includes(cast)) {
|
|
292
|
-
throw new e.ElectroError(e.ErrorCodes.InvalidAttributeDefinition, `Invalid "cast" property for attribute: "${name}". Acceptable types include ${CastTypes.join(", "
|
|
292
|
+
throw new e.ElectroError(e.ErrorCodes.InvalidAttributeDefinition, `Invalid "cast" property for attribute: "${name}". Acceptable types include ${CastTypes.join(", ")}`,
|
|
293
293
|
);
|
|
294
294
|
} else if (cast === AttributeTypes.string) {
|
|
295
295
|
return (val) => {
|
|
@@ -327,20 +327,34 @@ class Attribute {
|
|
|
327
327
|
_makeValidate(definition) {
|
|
328
328
|
if (typeof definition === "function") {
|
|
329
329
|
return (val) => {
|
|
330
|
-
|
|
331
|
-
|
|
330
|
+
try {
|
|
331
|
+
let reason = definition(val);
|
|
332
|
+
const isValid = !reason;
|
|
333
|
+
if (isValid) {
|
|
334
|
+
return [isValid, []];
|
|
335
|
+
} else if (typeof reason === "boolean") {
|
|
336
|
+
return [isValid, [new e.ElectroUserValidationError(this.path, "Invalid value provided")]];
|
|
337
|
+
} else {
|
|
338
|
+
return [isValid, [new e.ElectroUserValidationError(this.path, reason)]];
|
|
339
|
+
}
|
|
340
|
+
} catch(err) {
|
|
341
|
+
return [false, [new e.ElectroUserValidationError(this.path, err)]];
|
|
342
|
+
}
|
|
332
343
|
};
|
|
333
344
|
} else if (definition instanceof RegExp) {
|
|
334
345
|
return (val) => {
|
|
335
346
|
if (val === undefined) {
|
|
336
|
-
return [true,
|
|
347
|
+
return [true, []];
|
|
337
348
|
}
|
|
338
349
|
let isValid = definition.test(val);
|
|
339
|
-
let reason =
|
|
350
|
+
let reason = [];
|
|
351
|
+
if (!isValid) {
|
|
352
|
+
reason.push(new e.ElectroUserValidationError(this.path, `Invalid value for attribute "${this.path}": Failed model defined regex`));
|
|
353
|
+
}
|
|
340
354
|
return [isValid, reason];
|
|
341
355
|
};
|
|
342
356
|
} else {
|
|
343
|
-
return (
|
|
357
|
+
return () => [true, []];
|
|
344
358
|
}
|
|
345
359
|
}
|
|
346
360
|
|
|
@@ -385,15 +399,19 @@ class Attribute {
|
|
|
385
399
|
|
|
386
400
|
_isType(value) {
|
|
387
401
|
if (value === undefined) {
|
|
388
|
-
|
|
402
|
+
let reason = [];
|
|
403
|
+
if (this.required) {
|
|
404
|
+
reason.push(new e.ElectroAttributeValidationError(this.path, `Invalid value type at entity path: "${this.path}". Value is required.`));
|
|
405
|
+
}
|
|
406
|
+
return [!this.required, reason];
|
|
389
407
|
}
|
|
390
408
|
let isTyped = false;
|
|
391
|
-
let reason =
|
|
409
|
+
let reason = [];
|
|
392
410
|
switch (this.type) {
|
|
393
411
|
case AttributeTypes.enum:
|
|
394
412
|
isTyped = this.enumArray.includes(value);
|
|
395
413
|
if (!isTyped) {
|
|
396
|
-
reason
|
|
414
|
+
reason.push(new e.ElectroAttributeValidationError(this.path, `Invalid value type at entity path: "${this.path}". Value not found in set of acceptable values: ${u.commaSeparatedString(this.enumArray)}`));
|
|
397
415
|
}
|
|
398
416
|
break;
|
|
399
417
|
case AttributeTypes.any:
|
|
@@ -405,7 +423,7 @@ class Attribute {
|
|
|
405
423
|
default:
|
|
406
424
|
isTyped = typeof value === this.type;
|
|
407
425
|
if (!isTyped) {
|
|
408
|
-
reason
|
|
426
|
+
reason.push(new e.ElectroAttributeValidationError(this.path, `Invalid value type at entity path: "${this.path}". Received value of type "${typeof value}", expected value of type "${this.type}"`));
|
|
409
427
|
}
|
|
410
428
|
break;
|
|
411
429
|
}
|
|
@@ -414,12 +432,12 @@ class Attribute {
|
|
|
414
432
|
|
|
415
433
|
isValid(value) {
|
|
416
434
|
try {
|
|
417
|
-
let [isTyped,
|
|
418
|
-
let [isValid, validationError] = this.validate(value);
|
|
419
|
-
let
|
|
420
|
-
return [isTyped && isValid,
|
|
435
|
+
let [isTyped, typeErrorReason] = this._isType(value);
|
|
436
|
+
let [isValid, validationError] = isTyped ? this.validate(value) : [false, []];
|
|
437
|
+
let errors = [...typeErrorReason, ...validationError].filter(value => value !== undefined);
|
|
438
|
+
return [isTyped && isValid, errors];
|
|
421
439
|
} catch (err) {
|
|
422
|
-
return [false, err
|
|
440
|
+
return [false, [err]];
|
|
423
441
|
}
|
|
424
442
|
}
|
|
425
443
|
|
|
@@ -433,10 +451,9 @@ class Attribute {
|
|
|
433
451
|
|
|
434
452
|
getValidate(value) {
|
|
435
453
|
value = this.val(value);
|
|
436
|
-
let [isValid,
|
|
454
|
+
let [isValid, validationErrors] = this.isValid(value);
|
|
437
455
|
if (!isValid) {
|
|
438
|
-
|
|
439
|
-
throw new Error(validationError);
|
|
456
|
+
throw new e.ElectroValidationError(validationErrors);
|
|
440
457
|
}
|
|
441
458
|
return value;
|
|
442
459
|
}
|
|
@@ -509,13 +526,17 @@ class MapAttribute extends Attribute {
|
|
|
509
526
|
|
|
510
527
|
_isType(value) {
|
|
511
528
|
if (value === undefined) {
|
|
512
|
-
|
|
529
|
+
let reason = [];
|
|
530
|
+
if (this.required) {
|
|
531
|
+
reason.push(new e.ElectroAttributeValidationError(this.path, `Invalid value type at entity path: "${this.path}". Value is required.`));
|
|
532
|
+
}
|
|
533
|
+
return [!this.required, reason];
|
|
513
534
|
}
|
|
514
535
|
const valueType = getValueType(value);
|
|
515
536
|
if (valueType !== ValueTypes.object) {
|
|
516
|
-
return [false, `Invalid value type at entity path "${this.path}. Received value of type "${valueType}", expected value of type "object"`];
|
|
537
|
+
return [false, [new e.ElectroAttributeValidationError(this.path, `Invalid value type at entity path "${this.path}. Received value of type "${valueType}", expected value of type "object"`)]];
|
|
517
538
|
}
|
|
518
|
-
let reason =
|
|
539
|
+
let reason = [];
|
|
519
540
|
const [childrenAreValid, childErrors] = this._validateChildren(value);
|
|
520
541
|
if (!childrenAreValid) {
|
|
521
542
|
reason = childErrors;
|
|
@@ -526,24 +547,24 @@ class MapAttribute extends Attribute {
|
|
|
526
547
|
_validateChildren(value) {
|
|
527
548
|
const valueType = getValueType(value);
|
|
528
549
|
const attributes = this.properties.attributes;
|
|
529
|
-
|
|
550
|
+
let errors = [];
|
|
530
551
|
if (valueType === ValueTypes.object) {
|
|
531
552
|
for (const child of Object.keys(attributes)) {
|
|
532
|
-
const [isValid,
|
|
553
|
+
const [isValid, errorValues] = attributes[child].isValid(value === undefined ? value : value[child])
|
|
533
554
|
if (!isValid) {
|
|
534
|
-
errors
|
|
555
|
+
errors = [...errors, ...errorValues]
|
|
535
556
|
}
|
|
536
557
|
}
|
|
537
558
|
} else if (valueType !== ValueTypes.object) {
|
|
538
559
|
errors.push(
|
|
539
|
-
`Invalid value type at entity path: "${this.path}". Expected value to be an object to fulfill attribute type "${this.type}"`
|
|
560
|
+
new e.ElectroAttributeValidationError(this.path, `Invalid value type at entity path: "${this.path}". Expected value to be an object to fulfill attribute type "${this.type}"`)
|
|
540
561
|
);
|
|
541
562
|
} else if (this.properties.hasRequiredAttributes) {
|
|
542
563
|
errors.push(
|
|
543
|
-
`Invalid value type at entity path: "${this.path}". Map attribute requires at least the properties ${u.commaSeparatedString(Object.keys(attributes))}`
|
|
564
|
+
new e.ElectroAttributeValidationError(this.path, `Invalid value type at entity path: "${this.path}". Map attribute requires at least the properties ${u.commaSeparatedString(Object.keys(attributes))}`)
|
|
544
565
|
);
|
|
545
566
|
}
|
|
546
|
-
return [errors.length === 0, errors
|
|
567
|
+
return [errors.length === 0, errors];
|
|
547
568
|
}
|
|
548
569
|
|
|
549
570
|
val(value) {
|
|
@@ -560,7 +581,7 @@ class MapAttribute extends Attribute {
|
|
|
560
581
|
} else if (value && valueType !== "object" && Object.keys(value).length === 0) {
|
|
561
582
|
return getValue(value);
|
|
562
583
|
} else if (valueType !== "object") {
|
|
563
|
-
throw new
|
|
584
|
+
throw new e.ElectroAttributeValidationError(this.path, `Invalid value type at entity path: "${this.path}". Expected value to be an object to fulfill attribute type "${this.type}"`);
|
|
564
585
|
}
|
|
565
586
|
|
|
566
587
|
const data = {};
|
|
@@ -643,24 +664,29 @@ class ListAttribute extends Attribute {
|
|
|
643
664
|
}
|
|
644
665
|
|
|
645
666
|
_validateArrayValue(value) {
|
|
667
|
+
const reason = [];
|
|
646
668
|
const valueType = getValueType(value);
|
|
647
669
|
if (value !== undefined && valueType !== ValueTypes.array) {
|
|
648
|
-
return [false, `Invalid value type at entity path "${this.path}. Received value of type "${valueType}", expected value of type "array"`];
|
|
670
|
+
return [false, [new e.ElectroAttributeValidationError(this.path, `Invalid value type at entity path "${this.path}. Received value of type "${valueType}", expected value of type "array"`)]];
|
|
649
671
|
} else {
|
|
650
|
-
return [true,
|
|
672
|
+
return [true, []];
|
|
651
673
|
}
|
|
652
674
|
}
|
|
653
675
|
|
|
654
676
|
_isType(value) {
|
|
655
677
|
if (value === undefined) {
|
|
656
|
-
|
|
678
|
+
let reason = [];
|
|
679
|
+
if (this.required) {
|
|
680
|
+
reason.push(new e.ElectroAttributeValidationError(this.path, `Invalid value type at entity path: "${this.path}". Value is required.`));
|
|
681
|
+
}
|
|
682
|
+
return [!this.required, reason];
|
|
657
683
|
}
|
|
658
684
|
|
|
659
685
|
const [isValidArray, errors] = this._validateArrayValue(value);
|
|
660
686
|
if (!isValidArray) {
|
|
661
687
|
return [isValidArray, errors];
|
|
662
688
|
}
|
|
663
|
-
let reason =
|
|
689
|
+
let reason = [];
|
|
664
690
|
const [childrenAreValid, childErrors] = this._validateChildren(value);
|
|
665
691
|
if (!childrenAreValid) {
|
|
666
692
|
reason = childErrors;
|
|
@@ -673,17 +699,22 @@ class ListAttribute extends Attribute {
|
|
|
673
699
|
const errors = [];
|
|
674
700
|
if (valueType === ValueTypes.array) {
|
|
675
701
|
for (const i in value) {
|
|
676
|
-
const [isValid,
|
|
702
|
+
const [isValid, errorValues] = this.items.isValid(value[i]);
|
|
677
703
|
if (!isValid) {
|
|
678
|
-
|
|
704
|
+
for (const err of errorValues) {
|
|
705
|
+
if (err instanceof e.ElectroAttributeValidationError || err instanceof e.ElectroUserValidationError) {
|
|
706
|
+
err.index = parseInt(i);
|
|
707
|
+
}
|
|
708
|
+
errors.push(err);
|
|
709
|
+
}
|
|
679
710
|
}
|
|
680
711
|
}
|
|
681
712
|
} else {
|
|
682
713
|
errors.push(
|
|
683
|
-
`Invalid value type at entity path: "${this.path}". Expected value to be an Array to fulfill attribute type "${this.type}"`
|
|
714
|
+
new e.ElectroAttributeValidationError(this.path, `Invalid value type at entity path: "${this.path}". Expected value to be an Array to fulfill attribute type "${this.type}"`)
|
|
684
715
|
);
|
|
685
716
|
}
|
|
686
|
-
return [errors.length === 0, errors
|
|
717
|
+
return [errors.length === 0, errors];
|
|
687
718
|
}
|
|
688
719
|
|
|
689
720
|
val(value) {
|
|
@@ -700,7 +731,7 @@ class ListAttribute extends Attribute {
|
|
|
700
731
|
} else if (Array.isArray(value) && value.length === 0) {
|
|
701
732
|
return value;
|
|
702
733
|
} else if (!Array.isArray(value)) {
|
|
703
|
-
throw new
|
|
734
|
+
throw new e.ElectroAttributeValidationError(this.path, `Invalid value type at entity path "${this.path}. Received value of type "${getValueType(value)}", expected value of type "array"`);
|
|
704
735
|
}
|
|
705
736
|
|
|
706
737
|
const data = [];
|
|
@@ -731,6 +762,20 @@ class SetAttribute extends Attribute {
|
|
|
731
762
|
this.items = items;
|
|
732
763
|
this.get = this._makeGet(definition.get, items);
|
|
733
764
|
this.set = this._makeSet(definition.set, items);
|
|
765
|
+
this.validate = this._makeSetValidate(definition);
|
|
766
|
+
}
|
|
767
|
+
|
|
768
|
+
_makeSetValidate(definition) {
|
|
769
|
+
const validate = this._makeValidate(definition.validate);
|
|
770
|
+
return (value) => {
|
|
771
|
+
if (Array.isArray(value)) {
|
|
772
|
+
return validate([...value]);
|
|
773
|
+
} else if (value && value.wrapperName === 'Set') {
|
|
774
|
+
return validate([...value.values])
|
|
775
|
+
} else {
|
|
776
|
+
return validate(value);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
734
779
|
}
|
|
735
780
|
|
|
736
781
|
fromDDBSet(value) {
|
|
@@ -768,7 +813,7 @@ class SetAttribute extends Attribute {
|
|
|
768
813
|
return this._createDDBSet(value);
|
|
769
814
|
}
|
|
770
815
|
default:
|
|
771
|
-
throw new
|
|
816
|
+
throw new e.ElectroAttributeValidationError(this.path, `Invalid attribute value supplied to "set" attribute "${this.path}". Received value of type "${valueType}". Set values must be supplied as either Arrays, native JavaScript Set objects, DocumentClient Set objects, strings, or numbers.`)
|
|
772
817
|
}
|
|
773
818
|
|
|
774
819
|
}
|
|
@@ -795,7 +840,9 @@ class SetAttribute extends Attribute {
|
|
|
795
840
|
this._checkGetSet(set, "set");
|
|
796
841
|
const setter = set || ((attr) => attr);
|
|
797
842
|
return (values, siblings) => {
|
|
798
|
-
const results =
|
|
843
|
+
const results = values && values.wrapperName === 'Set'
|
|
844
|
+
? setter(values.values, siblings)
|
|
845
|
+
: setter(values, siblings)
|
|
799
846
|
if (results !== undefined) {
|
|
800
847
|
return this.toDDBSet(results);
|
|
801
848
|
}
|
|
@@ -804,10 +851,14 @@ class SetAttribute extends Attribute {
|
|
|
804
851
|
|
|
805
852
|
_isType(value) {
|
|
806
853
|
if (value === undefined) {
|
|
807
|
-
|
|
854
|
+
const reason = [];
|
|
855
|
+
if (this.required) {
|
|
856
|
+
reason.push(new e.ElectroAttributeValidationError(this.path, `Invalid value type at entity path: "${this.path}". Value is required.`));
|
|
857
|
+
}
|
|
858
|
+
return [!this.required, reason];
|
|
808
859
|
}
|
|
809
860
|
|
|
810
|
-
let reason =
|
|
861
|
+
let reason = [];
|
|
811
862
|
const [childrenAreValid, childErrors] = this._validateChildren(value);
|
|
812
863
|
if (!childrenAreValid) {
|
|
813
864
|
reason = childErrors;
|
|
@@ -817,7 +868,7 @@ class SetAttribute extends Attribute {
|
|
|
817
868
|
|
|
818
869
|
_validateChildren(value) {
|
|
819
870
|
const valueType = getValueType(value);
|
|
820
|
-
|
|
871
|
+
let errors = [];
|
|
821
872
|
let arr = [];
|
|
822
873
|
if (valueType === ValueTypes.array) {
|
|
823
874
|
arr = value;
|
|
@@ -827,16 +878,16 @@ class SetAttribute extends Attribute {
|
|
|
827
878
|
arr = value.values;
|
|
828
879
|
} else {
|
|
829
880
|
errors.push(
|
|
830
|
-
`Invalid value type at attribute path: "${this.path}". Expected value to be an Expected value to be an Array, native JavaScript Set objects, or DocumentClient Set objects to fulfill attribute type "${this.type}"`
|
|
881
|
+
new e.ElectroAttributeValidationError(this.path, `Invalid value type at attribute path: "${this.path}". Expected value to be an Expected value to be an Array, native JavaScript Set objects, or DocumentClient Set objects to fulfill attribute type "${this.type}"`)
|
|
831
882
|
)
|
|
832
883
|
}
|
|
833
884
|
for (const item of arr) {
|
|
834
|
-
const [isValid,
|
|
885
|
+
const [isValid, errorValues] = this.items.isValid(item);
|
|
835
886
|
if (!isValid) {
|
|
836
|
-
errors
|
|
887
|
+
errors = [...errors, ...errorValues];
|
|
837
888
|
}
|
|
838
889
|
}
|
|
839
|
-
return [errors.length === 0, errors
|
|
890
|
+
return [errors.length === 0, errors];
|
|
840
891
|
}
|
|
841
892
|
|
|
842
893
|
val(value) {
|
|
@@ -1232,11 +1283,11 @@ class Schema {
|
|
|
1232
1283
|
for (const path of paths) {
|
|
1233
1284
|
const attribute = this.traverser.getPath(path);
|
|
1234
1285
|
if (!attribute) {
|
|
1235
|
-
throw new
|
|
1286
|
+
throw new e.ElectroAttributeValidationError(path, `Attribute "${path}" does not exist on model.`);
|
|
1236
1287
|
} else if (attribute.readOnly) {
|
|
1237
|
-
throw new
|
|
1288
|
+
throw new e.ElectroAttributeValidationError(attribute.path, `Attribute "${attribute.path}" is Read-Only and cannot be removed`);
|
|
1238
1289
|
} else if (attribute.required) {
|
|
1239
|
-
throw new
|
|
1290
|
+
throw new e.ElectroAttributeValidationError(attribute.path, `Attribute "${attribute.path}" is Required and cannot be removed`);
|
|
1240
1291
|
}
|
|
1241
1292
|
}
|
|
1242
1293
|
return paths;
|
|
@@ -1251,7 +1302,7 @@ class Schema {
|
|
|
1251
1302
|
}
|
|
1252
1303
|
if (attribute.readOnly) {
|
|
1253
1304
|
// todo: #electroerror
|
|
1254
|
-
throw new
|
|
1305
|
+
throw new e.ElectroAttributeValidationError(attribute.path, `Attribute "${attribute.path}" is Read-Only and cannot be updated`);
|
|
1255
1306
|
} else {
|
|
1256
1307
|
record[path] = attribute.getValidate(value);
|
|
1257
1308
|
}
|