@contrail/documents 1.6.0-alpha.validation.4 → 1.6.0-alpha.validation.5
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.
|
@@ -2,8 +2,8 @@ import { WriteTransactionRequest } from './types/document-transaction';
|
|
|
2
2
|
export type WriteTransactionValidationCode = 'OPERATIONS_REQUIRED' | 'UNKNOWN_OPERATION_ACTION' | 'OPERATION_MISSING_ELEMENT' | 'OPERATION_MISSING_ELEMENT_ID' | 'UNKNOWN_ELEMENT_FIELD' | 'INVALID_ELEMENT_FIELD_TYPE' | 'INVALID_CLIENT_TRANSACTION_ID' | 'ELEMENT_DOCUMENT_ID_MISMATCH';
|
|
3
3
|
export interface WriteTransactionValidationIssue {
|
|
4
4
|
code: WriteTransactionValidationCode;
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
operationIndex?: number;
|
|
6
|
+
operationAction?: string;
|
|
7
7
|
field?: string;
|
|
8
8
|
fields?: string[];
|
|
9
9
|
expected?: string;
|
|
@@ -118,39 +118,45 @@ const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3
|
|
|
118
118
|
function validateOperation(operation, index, options) {
|
|
119
119
|
const action = operation === null || operation === void 0 ? void 0 : operation.action;
|
|
120
120
|
if (typeof action !== 'string' || !KNOWN_OPERATION_ACTIONS.includes(action)) {
|
|
121
|
-
return [
|
|
121
|
+
return [
|
|
122
|
+
{
|
|
123
|
+
code: 'UNKNOWN_OPERATION_ACTION',
|
|
124
|
+
operationIndex: index,
|
|
125
|
+
operationAction: typeof action === 'string' ? action : undefined,
|
|
126
|
+
},
|
|
127
|
+
];
|
|
122
128
|
}
|
|
123
129
|
if (action === 'deleteElement') {
|
|
124
130
|
const elementId = operation.elementId;
|
|
125
131
|
if (typeof elementId !== 'string' || elementId.length === 0) {
|
|
126
|
-
return [{ code: 'OPERATION_MISSING_ELEMENT_ID', index, action }];
|
|
132
|
+
return [{ code: 'OPERATION_MISSING_ELEMENT_ID', operationIndex: index, operationAction: action }];
|
|
127
133
|
}
|
|
128
134
|
return [];
|
|
129
135
|
}
|
|
130
136
|
if (action === 'updateElement') {
|
|
131
137
|
const elementId = operation.elementId;
|
|
132
138
|
if (typeof elementId !== 'string' || elementId.length === 0) {
|
|
133
|
-
return [{ code: 'OPERATION_MISSING_ELEMENT_ID', index, action }];
|
|
139
|
+
return [{ code: 'OPERATION_MISSING_ELEMENT_ID', operationIndex: index, operationAction: action }];
|
|
134
140
|
}
|
|
135
141
|
const changes = operation.changes;
|
|
136
142
|
if (!isPlainObject(changes)) {
|
|
137
|
-
return [{ code: 'OPERATION_MISSING_ELEMENT', index, action }];
|
|
143
|
+
return [{ code: 'OPERATION_MISSING_ELEMENT', operationIndex: index, operationAction: action }];
|
|
138
144
|
}
|
|
139
|
-
return collectElementFieldIssues(changes, []).map((issue) => (Object.assign(Object.assign({}, issue), { index, action })));
|
|
145
|
+
return collectElementFieldIssues(changes, []).map((issue) => (Object.assign(Object.assign({}, issue), { operationIndex: index, operationAction: action })));
|
|
140
146
|
}
|
|
141
147
|
const element = operation.element;
|
|
142
148
|
if (!isPlainObject(element) || typeof element.id !== 'string' || element.id.length === 0) {
|
|
143
|
-
return [{ code: 'OPERATION_MISSING_ELEMENT', index, action }];
|
|
149
|
+
return [{ code: 'OPERATION_MISSING_ELEMENT', operationIndex: index, operationAction: action }];
|
|
144
150
|
}
|
|
145
|
-
const issues = collectElementFieldIssues(element, ['id', 'documentId']).map((issue) => (Object.assign(Object.assign({}, issue), { index, action })));
|
|
151
|
+
const issues = collectElementFieldIssues(element, ['id', 'documentId']).map((issue) => (Object.assign(Object.assign({}, issue), { operationIndex: index, operationAction: action })));
|
|
146
152
|
const elementDocumentId = element.documentId;
|
|
147
153
|
if (options.documentId !== undefined &&
|
|
148
154
|
typeof elementDocumentId === 'string' &&
|
|
149
155
|
elementDocumentId !== options.documentId) {
|
|
150
156
|
issues.push({
|
|
151
157
|
code: 'ELEMENT_DOCUMENT_ID_MISMATCH',
|
|
152
|
-
index,
|
|
153
|
-
action,
|
|
158
|
+
operationIndex: index,
|
|
159
|
+
operationAction: action,
|
|
154
160
|
elementDocumentId,
|
|
155
161
|
documentId: options.documentId,
|
|
156
162
|
});
|
|
@@ -171,5 +177,5 @@ function validateWriteTransaction(request, options = {}) {
|
|
|
171
177
|
(typeof clientTransactionId !== 'string' || !UUID_PATTERN.test(clientTransactionId))) {
|
|
172
178
|
issues.push({ code: 'INVALID_CLIENT_TRANSACTION_ID' });
|
|
173
179
|
}
|
|
174
|
-
return issues.sort((a, b) => { var _a, _b; return CODE_RANK[a.code] - CODE_RANK[b.code] || ((_a = a.
|
|
180
|
+
return issues.sort((a, b) => { var _a, _b; return CODE_RANK[a.code] - CODE_RANK[b.code] || ((_a = a.operationIndex) !== null && _a !== void 0 ? _a : -1) - ((_b = b.operationIndex) !== null && _b !== void 0 ? _b : -1); });
|
|
175
181
|
}
|