validate<T> method

Future<T> validate<T>({
  1. Source? source,
})

Check the request with the DTO T and give the typed object. Throws the 422 ValidationException when the input is not valid. source wins over the source of the DTO.

CreateUser data = await req.validate<CreateUser>();

Implementation

Future<T> validate<T>({Source? source}) async {
  DtoSchema<Object>? schema = Dox().dtos.ofType(T);
  if (schema == null) {
    throw StateError(
      '$T is not a known DTO. Add @Dto() to it, run '
      '`dart run build_runner build` and set `dtos: dtos` '
      '(lib/dtos.g.dart) in AppConfig.',
    );
  }
  return await Dox().validator.validateDto(this, schema, source: source) as T;
}