request_validator 0.1.0 copy "request_validator: ^0.1.0" to clipboard
request_validator: ^0.1.0 copied to clipboard

A middleware to validate request body before route handler, focused with Dart Frog.

Request Validator #

A middleware to validate request body before route handler, currently focused with Dart Frog.

Build Status style: very good analysis Code Coverage Powered by Mason License: MIT


🧭 Overview #

The goal of this library is to provide functionalities to simplify request body validation in Dart Frog applications. It allows to define custom validation rules for different fields within the request body, ensuring data integrity and preventing invalid processing.

🚧 Installation #

Install the following dependency to the pubspec.yaml file of your Dart Frog application:

dependencies:
  request_validator: ^0.1.0

💻 Usage #

🛠️ Create a RequestValidator #

import 'package:request_validator/request_validator.dart';

// Extend the [RequestValidator] and provide the list of validation rules
// and configure the Response on validation failure.
class PersonValidator extends RequestValidator {
  // Validation rules will work only for POST requests
  PersonValidator() : super(allowedMethods: [HttpMethod.post]);

  // Override onError to configure Response object when validation fails
  @override
  FutureOr<Response> onError(List<ValidationError> errors) {
    return Response.json(
      statusCode: HttpStatus.badRequest,
      body: errors.toMapArray()
    );
  }

  // Override validator rules to handle validating request body object
  @override
  List<ValidationRule> validationRules() => [
        ValidationRule.body('name', (value) => value is String),
        ValidationRule.body('age', (value) => value is int && value > 0),
      ];
}

📦 Use PersonValidator as Middleware #

Handler middleware(Handler handler) {
  final validator = PersonValidator();
  // The serveAsMiddleware extension on the validator converts it into 
  // a middleware function
  return handler.use(validator.serveAsMiddleware());
}

✨ Maintainers #

2
likes
0
pub points
0%
popularity

Publisher

verified publisherthecodexhub.dev

A middleware to validate request body before route handler, focused with Dart Frog.

Repository (GitHub)
View/report issues

Documentation

Documentation

License

unknown (license)

Dependencies

collection, dart_frog, meta

More

Packages that depend on request_validator