Skip to content

Commit 94c2abb

Browse files
authored
Merge pull request #4144 from flexion/devex-1187-batch
Devex 1187: Batch
2 parents 13c670e + e2ca857 commit 94c2abb

File tree

5 files changed

+48
-38
lines changed

5 files changed

+48
-38
lines changed

shared/src/business/entities/Batch.test.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import { Batch } from './Batch';
22
import { applicationContext } from '../test/createTestApplicationContext';
33

4-
const { VALIDATION_ERROR_MESSAGES } = Batch;
5-
64
describe('Batch entity', () => {
75
it('adds a page', () => {
86
const batch = new Batch({
@@ -27,8 +25,8 @@ describe('Batch entity', () => {
2725
it('validates minimum number of pages', () => {
2826
const batch = new Batch({ applicationContext, rawBatch: {} });
2927

30-
expect(batch.getFormattedValidationErrors()).toMatchObject({
31-
pages: VALIDATION_ERROR_MESSAGES.pages,
28+
expect(batch.getValidationErrors()).toMatchObject({
29+
pages: 'At least one page is required',
3230
});
3331
});
3432

@@ -41,8 +39,8 @@ describe('Batch entity', () => {
4139
},
4240
});
4341

44-
expect(batch.getFormattedValidationErrors()).toMatchObject({
45-
batchIndex: VALIDATION_ERROR_MESSAGES.batchIndex,
42+
expect(batch.getValidationErrors()).toMatchObject({
43+
batchIndex: 'Invalid batch index',
4644
});
4745
});
4846
});

shared/src/business/entities/Batch.ts

+21-30
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { JoiValidationConstants } from './JoiValidationConstants';
2-
import { JoiValidationEntity } from './JoiValidationEntity';
2+
import { JoiValidationEntity_New } from '@shared/business/entities/joiValidationEntity/JoiValidationEntity_New';
33
import { createISODateString } from '@shared/business/utilities/DateHandler';
44
import joi from 'joi';
55

6-
export class Batch extends JoiValidationEntity {
6+
export class Batch extends JoiValidationEntity_New {
77
public batchId: string;
88
public batchIndex: string;
99
public createdAt: string;
@@ -18,42 +18,33 @@ export class Batch extends JoiValidationEntity {
1818
this.pages = rawBatch.pages || [];
1919
}
2020

21-
/**
22-
* adds a page to current Batch
23-
*
24-
* @param {object} page the page to add
25-
* @returns {Batch} the batch entity after the page is added
26-
*/
27-
addPage(page) {
21+
static VALIDATION_RULES = joi.object().keys({
22+
batchId: JoiValidationConstants.UUID.required(),
23+
batchIndex: joi
24+
.number()
25+
.integer()
26+
.min(0)
27+
.required()
28+
.messages({ '*': 'Invalid batch index' }),
29+
createdAt: JoiValidationConstants.ISO_DATE.required(),
30+
pages: joi
31+
.array()
32+
.min(1)
33+
.required()
34+
.messages({ '*': 'At least one page is required' }),
35+
});
36+
37+
addPage(page): Batch {
2838
this.pages.push(page);
2939
return this;
3040
}
3141

32-
/**
33-
* clears all pages within this Batch
34-
*
35-
* @returns {Batch} the batch entity after the pages are cleared
36-
*/
37-
clear() {
42+
clear(): Batch {
3843
this.pages = [];
3944
return this;
4045
}
4146

42-
static VALIDATION_ERROR_MESSAGES = {
43-
batchIndex: 'Invalid batch index',
44-
pages: 'At least one page is required',
45-
};
46-
4747
getValidationRules() {
48-
return joi.object().keys({
49-
batchId: JoiValidationConstants.UUID.required(),
50-
batchIndex: joi.number().integer().min(0).required(),
51-
createdAt: JoiValidationConstants.ISO_DATE.required(),
52-
pages: joi.array().min(1).required(),
53-
});
54-
}
55-
56-
getErrorToMessageMap() {
57-
return Batch.VALIDATION_ERROR_MESSAGES;
48+
return Batch.VALIDATION_RULES;
5849
}
5950
}

shared/src/business/entities/joiValidationEntity/JoiValidationEntity_New.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,16 @@ describe('Joi Entity', () => {
127127
});
128128
});
129129

130+
it('should ONlY recursively validate items in an array when the list contains items which are instances of JoiValidationEntity', () => {
131+
const testCaseEntity = new TestCaseEntity({
132+
otherList: [{ aProperty: 'abc' }], // Other list is a plain list of objects
133+
});
134+
135+
const errors = testCaseEntity.getValidationErrors();
136+
137+
expect(errors).toEqual(null);
138+
});
139+
130140
it('should return an object of errors when there is an error in a nested entity', () => {
131141
const testCaseEntity = new TestCaseEntity({
132142
nestedCase: { contactType: 'INVALID_1' },

shared/src/business/entities/joiValidationEntity/TestCaseEntity.ts

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export const TEST_VALIDATION_RULES = joi.object().keys({
1212
export class TestCaseEntity extends JoiValidationEntity_New {
1313
public contactType: string;
1414
public caseList: TestCaseEntity[];
15+
public otherList: { aProperty: string }[];
1516
public unhelpfulErrorMessage: string;
1617
public nestedCase: TestCaseEntity | undefined;
1718

@@ -29,6 +30,13 @@ export class TestCaseEntity extends JoiValidationEntity_New {
2930
.messages({
3031
'any.only': 'invalid contact type',
3132
}),
33+
otherList: joi
34+
.array()
35+
.items({ aProperty: joi.string().required() })
36+
.optional()
37+
.messages({
38+
'any.only': 'invalid contact type',
39+
}),
3240
unhelpfulErrorMessage: JoiValidationConstants.STRING.valid(
3341
'VALID_1',
3442
'VALID_2',
@@ -48,6 +56,7 @@ export class TestCaseEntity extends JoiValidationEntity_New {
4856
this.caseList = (rawTestCase.caseList || []).map(
4957
d => new TestCaseEntity(d),
5058
);
59+
this.otherList = rawTestCase.otherList;
5160
this.unhelpfulErrorMessage = rawTestCase.unhelpfulErrorMessage;
5261
this.nestedCase = rawTestCase.nestedCase
5362
? new TestCaseEntity(rawTestCase.nestedCase)

shared/src/business/entities/joiValidationEntity/helper.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ export function appendNestedEntitiesErrors(
2424
} else if (Array.isArray(entityPropertyValue)) {
2525
const errorsForNestedEntitiesInArray = entityPropertyValue
2626
.map((item, index) => {
27-
const itemErrors = item.getValidationErrors();
28-
return itemErrors ? { ...itemErrors, index } : null;
27+
if (item.getValidationErrors) {
28+
const itemErrors = item.getValidationErrors();
29+
return itemErrors ? { ...itemErrors, index } : null;
30+
}
2931
})
3032
.filter(itemErrors => !!itemErrors) as (TValidationError & {
3133
index: number;

0 commit comments

Comments
 (0)