Skip to content

Commit 1600a2d

Browse files
committed
Split the DTOs to provide easier maintainability
1 parent d229f01 commit 1600a2d

3 files changed

Lines changed: 44 additions & 14 deletions

File tree

lib/domain/dtos/common/BeamTypeDto.js

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,22 @@ const { CustomJoi } = require('../CustomJoi.js');
1717
/**
1818
* Used by Bookkeeping-LHC Plugin to send data as per LHC
1919
*/
20-
const DASH_SEPARATED_BEAM_TYPE_PATTERN = /^[A-Za-z0-9]+ ?- ?[A-Za-z0-9]+$/;
20+
const DASH_SEPARATED_BEAM_TYPE_PATTERN = /^[A-Za-z0-9]{1,6} ?- ?[A-Za-z0-9]{1,6}$/;
2121

2222
/**
2323
* User Input format declared by user at deployment of environment
2424
*/
2525
const COMPACT_BEAM_TYPE_PATTERN = /^[A-Za-z]{2,15}$/;
2626

27+
const BEAM_TYPE_MIN_LENGTH = 5;
28+
const BEAM_TYPE_MAX_LENGTH = 15;
29+
const PDP_BEAM_TYPE_MIN_LENGTH = 2;
30+
const PDP_BEAM_TYPE_MAX_LENGTH = 10;
31+
2732
/**
2833
* @typedef {string[]} BeamTypesDto
2934
* @description An array of beam types, each represented as a string.
30-
* Each beam type must be a string with a minimum length of 3 characters and a maximum length of 15 characters.
35+
* Each beam type must be a string with a minimum length of 2 characters and a maximum length of 15 characters.
3136
* The string must match patterns such as "PROTON - PROTON", "NE10 - NE10", where the two parts are separated by a hyphen and optional spaces.
3237
*
3338
* RUN3 has the following beam types:
@@ -36,20 +41,45 @@ const COMPACT_BEAM_TYPE_PATTERN = /^[A-Za-z]{2,15}$/;
3641
* "O8 - O8"
3742
* "PB82 - PB82"
3843
* "PROTON - O8"
39-
* "PROTON - PROTON"
44+
*
45+
* RUN3 has the following compact beam types:
46+
* "pp"
47+
* "pPb"
48+
* "NeNe"
49+
* "OO"
50+
* "cosmic"
51+
* "technical"
4052
*
4153
* @example
4254
* const beamTypes = ["PROTON - PROTON", "NE10 - NE10"];
4355
*/
4456
exports.BeamTypesDto = CustomJoi.stringArray()
4557
.items(Joi.string()
4658
.trim()
47-
.min(2)
48-
.max(15)
49-
.pattern(new RegExp(`(?:${DASH_SEPARATED_BEAM_TYPE_PATTERN.source})|(?:${COMPACT_BEAM_TYPE_PATTERN.source})`))
59+
.min(BEAM_TYPE_MIN_LENGTH)
60+
.max(BEAM_TYPE_MAX_LENGTH)
61+
.pattern(DASH_SEPARATED_BEAM_TYPE_PATTERN)
5062
.messages({
5163
'string.base': 'Beam type must be a string',
52-
'string.min': 'Beam type must be at least 2 characters long',
53-
'string.max': 'Beam type must be at most 15 characters long',
54-
'string.pattern.base': 'Beam type must look like "PROTON - PROTON", "NE10 - NE10", "pp", "pPb", "NeNe", "OO", etc.',
64+
'string.min': `Beam type must be at least ${BEAM_TYPE_MIN_LENGTH} characters long`,
65+
'string.max': `Beam type must be at most ${BEAM_TYPE_MAX_LENGTH} characters long`,
66+
'string.pattern.base': 'Beam type must look like "PROTON - PROTON", "NE10 - NE10", etc.',
67+
}));
68+
69+
/**
70+
* @typedef {string[]} PdpBeamTypesDto
71+
* @description An array of compact PDP beam types represented as strings.
72+
* Values can include examples like "pp", "pPb", "NeNe", "OO", "cosmic", and "technical".
73+
*/
74+
exports.PdpBeamTypesDto = CustomJoi.stringArray()
75+
.items(Joi.string()
76+
.trim()
77+
.min(PDP_BEAM_TYPE_MIN_LENGTH)
78+
.max(PDP_BEAM_TYPE_MAX_LENGTH)
79+
.pattern(COMPACT_BEAM_TYPE_PATTERN)
80+
.messages({
81+
'string.base': 'PDP beam type must be a string',
82+
'string.min': `PDP beam type must be at least ${PDP_BEAM_TYPE_MIN_LENGTH} characters long`,
83+
'string.max': `PDP beam type must be at most ${PDP_BEAM_TYPE_MAX_LENGTH} characters long`,
84+
'string.pattern.base': 'PDP beam type must look like "pp", "pPb", "NeNe", "OO", "cosmic", "technical", etc.',
5585
}));

lib/domain/dtos/filters/RunFilterDto.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
const Joi = require('joi');
1414
const { CustomJoi } = require('../CustomJoi.js');
15-
const { BeamTypesDto } = require('../common/BeamTypeDto.js');
15+
const { BeamTypesDto, PdpBeamTypesDto } = require('../common/BeamTypeDto.js');
1616
const { FromToFilterDto } = require('./FromToFilterDto.js');
1717
const { RUN_QUALITIES } = require('../../enums/RunQualities.js');
1818
const { IntegerComparisonDto, FloatComparisonDto } = require('./NumericalComparisonDto.js');
@@ -41,7 +41,7 @@ exports.RunFilterDto = Joi.object({
4141
'Beam modes "{{#value}}" must contain only uppercase letters and single spaces between words.',
4242
})),
4343
beamTypes: BeamTypesDto,
44-
pdpBeamTypes: BeamTypesDto,
44+
pdpBeamTypes: PdpBeamTypesDto,
4545
runNumbers: Joi.string().trim().custom(validateRange).messages({
4646
[RANGE_INVALID]: '{{#message}}',
4747
'string.base': 'Run numbers must be comma-separated numbers or ranges (e.g. 12,15-18)',

test/api/runs.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,17 +229,17 @@ module.exports = () => {
229229
let { errors: [error] } = response.body;
230230

231231
expect(error.title).to.equal('Invalid Attribute');
232-
expect(error.detail).to.equal(`"query.filter.pdpBeamTypes[0]" length must be at least 2 characters long`);
232+
expect(error.detail).to.equal(`PDP beam type must be at least 2 characters long`);
233233

234-
const pdpBeamTypesLong = 'This is definitely not a pdp beam type'; // Too short
234+
const pdpBeamTypesLong = 'This is definitely not a pdp beam type'; // Too long
235235
const responseLong = await request(server).get(`/api/runs?filter[pdpBeamTypes]=${pdpBeamTypesLong}`);
236236

237237
expect(responseLong.status).to.equal(400);
238238

239239
({ errors: [error] } = responseLong.body);
240240

241241
expect(error.title).to.equal('Invalid Attribute');
242-
expect(error.detail).to.equal(`"query.filter.pdpBeamTypes[0]" length must be less than or equal to 20 characters long`);
242+
expect(error.detail).to.equal(`PDP beam type must be at most 10 characters long`);
243243
});
244244

245245
it('should return 400 if beamModes filter has the incorrect format', async () => {

0 commit comments

Comments
 (0)