Skip to content

Commit

Permalink
th-302: Make Proper Driver License Number Validation (BinaryStudioAca…
Browse files Browse the repository at this point in the history
…demy#306)

* th-302: * make proper driver license number validation

* th-302: * make more sophisticated length testing

* th-302: * remove redundant line, fix typo

* th-302: * fix incompatibility issue
  • Loading branch information
Some14u committed Sep 28, 2023
1 parent 75efea8 commit 21d8b10
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const DriverValidationMessage = {
DRIVER_LICENSE_NUMBER_REQUIRED: 'Driver license number is required',
DRIVER_LICENSE_NUMBER_REQUIRED: 'This field is mandatory',
DRIVER_LICENSE_NUMBER_INVALID:
'This driver license number does not seem valid',
ID_MUST_BE_NUMBER: 'Driver id must be number',
USER_ID_MUST_BE_NUMBER: 'Driver user id must be number',
BUSINESS_ID_MUST_BE_NUMBER: 'Driver user id must be number',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const DriverValidationRule = {
LICENSE_MIN_LENGTH: 5,
LICENSE_MAX_LENGTH: 30,
LICENSE_NUMBER: /^[\dA-Z]+(?:[ -][\dA-Z]+)*$/,
LICENSE_SPACERS: /[\s-]/g,
} as const;

export { DriverValidationRule };
1 change: 1 addition & 0 deletions shared/src/packages/drivers/libs/enums/enums.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { DriverApiPath } from './driver-api-path.enum.js';
export { DriverValidationMessage } from './driver-validation-message.enum.js';
export { DriverValidationRule } from './driver-validation-rule.enum.js';
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,32 @@ import {
type MultipartParsedFile,
} from '~/packages/files/libs/types/types.js';
import { commonSignUpRules } from '~/packages/users/libs/validation-schemas/common-rules/common-rules.js';
import { UserValidationRule } from '~/packages/users/libs/validation-schemas/enums/enums.js';

import { DriverValidationMessage } from '../enums/enums.js';
import {
DriverValidationMessage as Message,
DriverValidationRule as Rule,
} from '../enums/enums.js';
import { type DriverUpdateRequestDto } from '../types/types.js';
import { checkMinMaxValidator } from './libs/helpers/heplers.js';

const driverLicenseNumber = joi
.string()
.trim()
.regex(UserValidationRule.DRIVER_LICENSE_NUMBER)
// To check min/max length we must consider only meaningful characters,
// thus we cannot use joi's min/max here
.custom(
checkMinMaxValidator(
Rule.LICENSE_MIN_LENGTH,
Rule.LICENSE_MAX_LENGTH,
Rule.LICENSE_SPACERS,
),
)
.regex(Rule.LICENSE_NUMBER)
.required()
.messages({
'string.empty': DriverValidationMessage.DRIVER_LICENSE_NUMBER_REQUIRED,
'string.empty': Message.DRIVER_LICENSE_NUMBER_REQUIRED,
'string.pattern.base': Message.DRIVER_LICENSE_NUMBER_INVALID,
'string.custom': Message.DRIVER_LICENSE_NUMBER_INVALID,
});

const driverUpdateRequestBody = joi.object<
Expand All @@ -27,7 +41,7 @@ const driverUpdateRequestBody = joi.object<
driverLicenseNumber,
truckIds: joi.array().items(joi.number()),
files: joi.array().items(joi.object()).min(1).required().messages({
'array.min': DriverValidationMessage.FILES_MIN_LENGTH,
'array.min': Message.FILES_MIN_LENGTH,
}),
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { type CustomValidator } from 'joi';

const checkMinMaxValidator: (
min: number,
max: number,
spacers: RegExp,
) => CustomValidator<string> = (min, max, spacers) => (value, helpers) => {
const characters = value.replace(spacers, '');

if (characters.length >= min && characters.length <= max) {
return value;
}

return helpers.error('string.custom');
};

export { checkMinMaxValidator };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { checkMinMaxValidator } from './check-min-max-validator.helper.js';
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const UserValidationRule = {
COMPANY_NAME: /^[\s\w!#&'*+,.;?@~-]{1,40}$/,
EMAIL_MIN_LENGTH: 5,
EMAIL_MAX_LENGTH: 254,
DRIVER_LICENSE_NUMBER: /^[A-Z]{3} \d{6}$/,
} as const;

export { UserValidationRule };

0 comments on commit 21d8b10

Please sign in to comment.