refactoring seperate decorators from validating functions
This commit is contained in:
parent
1b5ed98ee5
commit
8dcf456f7e
|
@ -104,6 +104,7 @@
|
|||
"**/*.(t|j)s"
|
||||
],
|
||||
"coveragePathIgnorePatterns": [
|
||||
".validator.ts",
|
||||
".controller.ts",
|
||||
".module.ts",
|
||||
".request.ts",
|
||||
|
|
|
@ -15,10 +15,10 @@ import { mappingKeyToFrequency } from './validators/frequency.mapping';
|
|||
import { MarginDTO } from './create.margin.dto';
|
||||
import { ScheduleDTO } from './create.schedule.dto';
|
||||
import { AddressRequestDTO } from './create.address.request';
|
||||
import { HasProperPassengerSeats } from './validators/has-passenger-seats.validator';
|
||||
import { HasProperDriverSeats } from './validators/has-driver-seats.validator';
|
||||
import { HasProperPositionIndexes } from './validators/address-position.validator';
|
||||
import { IsPunctualOrRecurrent } from './validators/is-punctual-or-recurrent.validator';
|
||||
import { IsPunctualOrRecurrent } from './validators/decorators/is-punctual-or-recurrent.validator';
|
||||
import { HasProperDriverSeats } from './validators/decorators/has-driver-seats.validator';
|
||||
import { HasProperPassengerSeats } from './validators/decorators/has-passenger-seats.validator';
|
||||
import { HasProperPositionIndexes } from './validators/decorators/address-position.validator';
|
||||
|
||||
export class CreateAdRequest {
|
||||
@IsOptional()
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
import { AddressRequestDTO } from '../create.address.request';
|
||||
|
||||
export function hasProperPositionIndexes(value: AddressRequestDTO[]) {
|
||||
if (value.every((address) => address.position === undefined)) return true;
|
||||
else if (value.every((address) => typeof address.position === 'number')) {
|
||||
value.sort((a, b) => a.position - b.position);
|
||||
for (let i = 1; i < value.length; i++) {
|
||||
if (value[i - 1].position >= value[i].position) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
|
@ -1,19 +1,7 @@
|
|||
import { ValidateBy, ValidationOptions, buildMessage } from 'class-validator';
|
||||
import { AddressRequestDTO } from '../create.address.request';
|
||||
import { AddressRequestDTO } from '../../create.address.request';
|
||||
import { hasProperPositionIndexes } from '../address-position';
|
||||
|
||||
export function hasProperPositionIndexes(value: AddressRequestDTO[]) {
|
||||
if (value.every((address) => address.position === undefined)) return true;
|
||||
else if (value.every((address) => typeof address.position === 'number')) {
|
||||
value.sort((a, b) => a.position - b.position);
|
||||
for (let i = 1; i < value.length; i++) {
|
||||
if (value[i - 1].position >= value[i].position) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
export function HasProperPositionIndexes(
|
||||
validationOptions?: ValidationOptions,
|
||||
): PropertyDecorator {
|
|
@ -4,24 +4,7 @@ import {
|
|||
ValidationOptions,
|
||||
buildMessage,
|
||||
} from 'class-validator';
|
||||
|
||||
export function hasProperDriverSeats(args: ValidationArguments) {
|
||||
if (
|
||||
args.object['driver'] === true &&
|
||||
typeof args.object['seatsDriver'] === 'number'
|
||||
)
|
||||
return args.object['seatsDriver'] > 0;
|
||||
if (
|
||||
(args.object['driver'] === false ||
|
||||
args.object['driver'] === null ||
|
||||
args.object['driver'] === undefined) &&
|
||||
(args.object['seatsDriver'] === 0 ||
|
||||
args.object['seatsDriver'] === null ||
|
||||
args.object['seatsDriver'] === undefined)
|
||||
)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
import { hasProperDriverSeats } from '../has-driver-seats';
|
||||
|
||||
export function HasProperDriverSeats(
|
||||
validationOptions?: ValidationOptions,
|
|
@ -4,24 +4,7 @@ import {
|
|||
ValidationOptions,
|
||||
buildMessage,
|
||||
} from 'class-validator';
|
||||
|
||||
export function hasProperPassengerSeats(args: ValidationArguments) {
|
||||
if (
|
||||
args.object['passenger'] === true &&
|
||||
typeof args.object['seatsPassenger'] === 'number'
|
||||
)
|
||||
return args.object['seatsPassenger'] > 0;
|
||||
else if (
|
||||
(args.object['passenger'] === false ||
|
||||
args.object['passenger'] === null ||
|
||||
args.object['passenger'] === undefined) &&
|
||||
(args.object['seatsPassenger'] === 0 ||
|
||||
args.object['seatsPassenger'] === null ||
|
||||
args.object['seatsPassenger'] === undefined)
|
||||
)
|
||||
return true;
|
||||
else return false;
|
||||
}
|
||||
import { hasProperPassengerSeats } from '../has-passenger-seats';
|
||||
|
||||
export function HasProperPassengerSeats(
|
||||
validationOptions?: ValidationOptions,
|
|
@ -0,0 +1,28 @@
|
|||
import {
|
||||
ValidateBy,
|
||||
ValidationArguments,
|
||||
ValidationOptions,
|
||||
buildMessage,
|
||||
} from 'class-validator';
|
||||
import { isPunctualOrRecurrent } from '../is-punctual-or-recurrent';
|
||||
|
||||
export function IsPunctualOrRecurrent(
|
||||
validationOptions?: ValidationOptions,
|
||||
): PropertyDecorator {
|
||||
return ValidateBy(
|
||||
{
|
||||
name: '',
|
||||
constraints: [],
|
||||
validator: {
|
||||
validate: (value, args: ValidationArguments): boolean =>
|
||||
isPunctualOrRecurrent(args),
|
||||
defaultMessage: buildMessage(
|
||||
() =>
|
||||
`the departure, from date, to date and schedule must be properly set on reccurent or punctual ad `,
|
||||
validationOptions,
|
||||
),
|
||||
},
|
||||
},
|
||||
validationOptions,
|
||||
);
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
import { ValidationArguments } from 'class-validator';
|
||||
|
||||
export function hasProperDriverSeats(args: ValidationArguments) {
|
||||
if (
|
||||
args.object['driver'] === true &&
|
||||
typeof args.object['seatsDriver'] === 'number'
|
||||
)
|
||||
return args.object['seatsDriver'] > 0;
|
||||
if (
|
||||
(args.object['driver'] === false ||
|
||||
args.object['driver'] === null ||
|
||||
args.object['driver'] === undefined) &&
|
||||
(args.object['seatsDriver'] === 0 ||
|
||||
args.object['seatsDriver'] === null ||
|
||||
args.object['seatsDriver'] === undefined)
|
||||
)
|
||||
return true;
|
||||
return false;
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
import { ValidationArguments } from 'class-validator';
|
||||
|
||||
export function hasProperPassengerSeats(args: ValidationArguments) {
|
||||
if (
|
||||
args.object['passenger'] === true &&
|
||||
typeof args.object['seatsPassenger'] === 'number'
|
||||
)
|
||||
return args.object['seatsPassenger'] > 0;
|
||||
else if (
|
||||
(args.object['passenger'] === false ||
|
||||
args.object['passenger'] === null ||
|
||||
args.object['passenger'] === undefined) &&
|
||||
(args.object['seatsPassenger'] === 0 ||
|
||||
args.object['seatsPassenger'] === null ||
|
||||
args.object['seatsPassenger'] === undefined)
|
||||
)
|
||||
return true;
|
||||
else return false;
|
||||
}
|
|
@ -1,9 +1,4 @@
|
|||
import {
|
||||
ValidateBy,
|
||||
ValidationArguments,
|
||||
ValidationOptions,
|
||||
buildMessage,
|
||||
} from 'class-validator';
|
||||
import { ValidationArguments } from 'class-validator';
|
||||
import { Frequency } from '../../types/frequency.enum';
|
||||
|
||||
function isPunctual(args: ValidationArguments): boolean {
|
||||
|
@ -30,24 +25,3 @@ function isRecurrent(args: ValidationArguments): boolean {
|
|||
export const isPunctualOrRecurrent = (args: ValidationArguments): boolean => {
|
||||
return isPunctual(args) || isRecurrent(args);
|
||||
};
|
||||
|
||||
export function IsPunctualOrRecurrent(
|
||||
validationOptions?: ValidationOptions,
|
||||
): PropertyDecorator {
|
||||
return ValidateBy(
|
||||
{
|
||||
name: '',
|
||||
constraints: [],
|
||||
validator: {
|
||||
validate: (value, args: ValidationArguments): boolean =>
|
||||
isPunctualOrRecurrent(args),
|
||||
defaultMessage: buildMessage(
|
||||
() =>
|
||||
`the departure, from date, to date and schedule must be properly set on reccurent or punctual ad `,
|
||||
validationOptions,
|
||||
),
|
||||
},
|
||||
},
|
||||
validationOptions,
|
||||
);
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
import { hasProperDriverSeats } from '../../../domain/dtos/validators/has-driver-seats.validator';
|
||||
import { hasProperDriverSeats } from '../../../domain/dtos/validators/has-driver-seats';
|
||||
|
||||
describe('driver and/or driver seats validator', () => {
|
||||
it('should validate if driver and drivers seats is not provided ', () => {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { hasProperPassengerSeats } from '../../../domain/dtos/validators/has-passenger-seats.validator';
|
||||
import { hasProperPassengerSeats } from '../../../domain/dtos/validators/has-passenger-seats';
|
||||
|
||||
describe('driver and/or passenger seats validator', () => {
|
||||
it('should validate if passenger and passengers seats is not provided ', () => {
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { AddressRequestDTO } from '../../../domain/dtos/create.address.request';
|
||||
import { hasProperPositionIndexes } from '../../../domain/dtos/validators/address-position.validator';
|
||||
import { hasProperPositionIndexes } from '../../../domain/dtos/validators/address-position';
|
||||
describe('addresses position validators', () => {
|
||||
const mockAddress1: AddressRequestDTO = {
|
||||
lon: 48.68944505415954,
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { isPunctualOrRecurrent } from '../../../domain/dtos/validators/is-punctual-or-recurrent.validator';
|
||||
import { isPunctualOrRecurrent } from '../../../domain/dtos/validators/is-punctual-or-recurrent';
|
||||
import { Frequency } from '../../../domain/types/frequency.enum';
|
||||
|
||||
describe('punctual or reccurent validators', () => {
|
||||
|
|
Loading…
Reference in New Issue