2 ms·
The idea is that you have a clear, strongly-typed separation between unvalidated data and validated data. Unvalidated data (the DTO) is just the raw representa
by piaste 5y ago
The idea is that you have a clear, strongly-typed separation between unvalidated data and validated data.
Unvalidated data (the DTO) is just the raw representation of whatever was inputed, read from storage, received from external services, etc. Any possible input should be acceptable and faithfully representable as a DTO.
Then, by passing the DTO to a validation function, you return either a validated object (a model) which is in fact constrained to only contain a legal data state; or a set of validation errors which can be acted upon.
Your business logic should operate only on validated objects, so that you can actually rely on your basic assumptions, and actual workflow rules (eg. "you can't checkout an empty cart") can be expressed and separated from trivial validation (eg. "quantity must be greater than 0").