Created
March 6, 2021 15:19
-
-
Save Chester97/38edc3c613a3d0e1157b44e23b2833dd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { ContractorDto } from './contractor.dto'; | |
import { IncomeDto } from './income.dto'; | |
import { ExpensesDto } from './expenses.dto'; | |
import { | |
IdValidator, | |
PositionValidator, | |
DateOfEventValidator, | |
DescriptionValidator, | |
RegistryValidator, | |
ContractorValidator, | |
IncomeValidator, | |
ExpensesValidator, | |
} from '../helpers/invoiceValidation.decorator'; | |
export class InvoiceDto { | |
@IdValidator() id: string; | |
@PositionValidator() position: number; | |
@DateOfEventValidator() dateOfEvent: string; | |
@RegistryValidator() registry: string; | |
@DescriptionValidator() description: string; | |
@ContractorValidator() contractor: ContractorDto; | |
@IncomeValidator() income: IncomeDto; | |
@ExpensesValidator() expenses: ExpensesDto; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type XYZ = Omit<InvoiceDto, 'id'>; | |
@Post('/add') | |
addInvoice( | |
@Res() res: Response, | |
@Body( | |
new ValidationPipe({ | |
transform: true, | |
disableErrorMessages: false, | |
always: true, | |
stopAtFirstError: true, | |
validateCustomDecorators: true, | |
skipMissingProperties: false, | |
}), | |
) | |
body: XYZ, // here is a problem, when I'm using InvoiceDto - validation works, but when i try to use Omit it doesnt work | |
) { | |
// I dont neet to validate ID field because I will generate it on backend | |
const invoice = this.invoicesService.addInvoice(body); | |
return res.json(invoice); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function IdValidator() { | |
return applyDecorators( | |
IsDefined({ message: 'ID of an inovice is required!' }), | |
IsString({ message: 'ID must be a string!' }), | |
Length(21, 21, { message: 'ID is not valid!' }), | |
); | |
} | |
export function PositionValidator() { | |
return applyDecorators( | |
IsDefined({ message: 'Position of invoice is required!' }), | |
IsNotEmpty(), | |
IsInt({ message: 'Position of invoice must be a number' }), | |
Min(1, { message: 'Position of invoice must be greater than 0' }), | |
); | |
} | |
export function DateOfEventValidator() { | |
return applyDecorators( | |
IsDefined({ message: 'Date of invoice event is required!' }), | |
IsOnlyDate(), | |
); | |
} | |
// I've just add a couple of validators |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment