Skip to content

Commit 5e9a7cd

Browse files
committed
refactor: add idx property to ValidationErrorData; now ValidationError is used also for *Many documents validation.
All validator records are now combined into one array like was suggested in #248 (comment) One array better: `errors: [ { idx: 3, message: '', path: ''} ]` Than nested array with nulls: `errors: [ null, null, null, { message: '', errors: [{ message: '', path: ''}] } ]`
1 parent e697829 commit 5e9a7cd

File tree

4 files changed

+26
-107
lines changed

4 files changed

+26
-107
lines changed

src/__tests__/github_issues/248-test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ describe("issue #248 - payloads' errors", () => {
141141
message
142142
path
143143
value
144+
idx
144145
}
145146
}
146147
}
@@ -155,13 +156,13 @@ describe("issue #248 - payloads' errors", () => {
155156
records: null,
156157
error: {
157158
__typename: 'ValidationError',
158-
message: 'User validation failed: someStrangeField: this is a validate message',
159+
message: 'Nothing has been saved. Some documents contain validation errors',
159160
errors: [
160161
{
161162
message: 'this is a validate message',
162-
path: '1.someStrangeField',
163-
// ^^ - we add idx of broken record
163+
path: 'someStrangeField',
164164
value: 'Test',
165+
idx: 1,
165166
},
166167
],
167168
},

src/errors/ManyValidationError.ts

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/errors/index.ts

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
import type { InterfaceTypeComposer, SchemaComposer } from 'graphql-compose';
22
import { MongoError, getMongoErrorOTC } from './MongoError';
33
import { ValidationError, getValidationErrorOTC } from './ValidationError';
4-
import {
5-
ManyValidationError,
6-
getManyValidationErrorOTC,
7-
ManyValidationsByIdx,
8-
} from './ManyValidationError';
94
import { RuntimeError, getRuntimeErrorOTC } from './RuntimeError';
105

11-
export { MongoError, ValidationError, ManyValidationError, ManyValidationsByIdx, RuntimeError };
6+
export { MongoError, ValidationError, RuntimeError };
127

138
export function getErrorInterface(schemaComposer: SchemaComposer<any>): InterfaceTypeComposer {
149
const ErrorInterface = schemaComposer.getOrCreateIFTC('ErrorInterface', (iftc) => {
@@ -49,43 +44,3 @@ export function getErrorInterface(schemaComposer: SchemaComposer<any>): Interfac
4944

5045
return ErrorInterface;
5146
}
52-
53-
export function getManyErrorInterface(schemaComposer: SchemaComposer<any>): InterfaceTypeComposer {
54-
const ErrorInterface = schemaComposer.getOrCreateIFTC('ManyErrorInterface', (iftc) => {
55-
iftc.addFields({
56-
message: {
57-
description: 'Generic error message',
58-
type: 'String',
59-
},
60-
});
61-
62-
const ManyValidationErrorOTC = getManyValidationErrorOTC(schemaComposer);
63-
const MongoErrorOTC = getMongoErrorOTC(schemaComposer);
64-
const RuntimeErrorOTC = getRuntimeErrorOTC(schemaComposer);
65-
66-
ManyValidationErrorOTC.addInterface(iftc);
67-
MongoErrorOTC.addInterface(iftc);
68-
RuntimeErrorOTC.addInterface(iftc);
69-
70-
schemaComposer.addSchemaMustHaveType(ManyValidationErrorOTC);
71-
schemaComposer.addSchemaMustHaveType(MongoErrorOTC);
72-
schemaComposer.addSchemaMustHaveType(RuntimeErrorOTC);
73-
74-
const ManyValidationErrorType = ManyValidationErrorOTC.getType();
75-
const MongoErrorType = MongoErrorOTC.getType();
76-
const RuntimeErrorType = RuntimeErrorOTC.getType();
77-
78-
iftc.setResolveType((value) => {
79-
switch (value?.name) {
80-
case 'ManyValidationError':
81-
return ManyValidationErrorType;
82-
case 'MongoError':
83-
return MongoErrorType;
84-
default:
85-
return RuntimeErrorType;
86-
}
87-
});
88-
});
89-
90-
return ErrorInterface;
91-
}

src/resolvers/helpers/validate.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
import type { Error as MongooseError } from 'mongoose';
22
import type { Document } from 'mongoose';
3-
import { ValidationError, ManyValidationError, ManyValidationsByIdx } from '../../errors';
3+
import { ValidationError } from '../../errors';
44

55
export type ValidationErrorData = {
66
path: string;
77
message: string;
88
value: any;
9+
/**
10+
* This `idx` property is used only for *Many operations.
11+
* It stores idx from received array of records which occurs Validation Error.
12+
*/
13+
idx?: number;
914
};
1015

1116
export type ValidationsWithMessage = {
@@ -48,29 +53,32 @@ export async function validateAndThrow(doc: Document): Promise<void> {
4853
/**
4954
* Make async validation for array of mongoose documents.
5055
* And if they have validation errors then throw one Error with embedding
51-
* all validation errors for every document separately.
52-
* If document does not have error then in embedded errors' array will
53-
* be `null` at the same idx position.
56+
* all validator errors into one array with addition of `idx` property.
57+
* `idx` represent record index in array received from user.
5458
*/
5559
export async function validateManyAndThrow(docs: Document[]): Promise<void> {
56-
const manyValidations: ManyValidationsByIdx = [];
60+
const combinedValidators: Array<ValidationErrorData> = [];
5761
let hasValidationError = false;
5862

59-
for (const doc of docs) {
60-
const validations: ValidationsWithMessage | null = await validateDoc(doc);
63+
for (let idx = 0; idx < docs.length; idx++) {
64+
const validations: ValidationsWithMessage | null = await validateDoc(docs[idx]);
6165

6266
if (validations) {
63-
manyValidations.push(validations);
67+
validations.errors.forEach((validatorError) => {
68+
combinedValidators.push({
69+
...validatorError,
70+
idx,
71+
});
72+
});
73+
6474
hasValidationError = true;
65-
} else {
66-
manyValidations.push(null);
6775
}
6876
}
6977

7078
if (hasValidationError) {
71-
throw new ManyValidationError({
72-
message: 'Some documents contain validation errors',
73-
errors: manyValidations,
79+
throw new ValidationError({
80+
message: 'Nothing has been saved. Some documents contain validation errors',
81+
errors: combinedValidators,
7482
});
7583
}
7684
}

0 commit comments

Comments
 (0)