Skip to content

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

License

Notifications You must be signed in to change notification settings

thecodexhub/request-validator

Repository files navigation

Request Validator

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

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


🧭 Overview

This library aims to provide functionalities to simplify request objects validation in Dart Frog applications. It allows the definition of custom validation rules for different fields within the request objects, 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.3.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 and query params
  @override
  List<ValidationRule> validationRules() => [
        // Validate if the request body contains `name`, and that should be a string
        ValidationRule.body('name', (value) => value is String),
        // Validate if the request query has `code` field, and
        // it's value should be greater than 0.
        ValidationRule.query('code', (value) => int.parse(value) > 0),
        // Validate the request has `Content-Type` header set as `application/json`
        ValidationRule.headers(
          HttpHeaders.contentTypeHeader,
          (value) => value == 'application/json',
        ),
      ];
}

📍 Other Properties of ValidationRule

  • optional: Specifies whether the field being validated is optional within the request body. If true, the library first checks if the field exists in the request body. If it's missing, the validation for that field is skipped.
  • message: Defines a custom error message to be used when the validation for this field fails. If null (the default), a generic error message will be provided during validation failure.

More complete examples with ValidationRule

🟠 Request Body Validation
static final _emailRegExp = RegExp(
  r'^[a-zA-Z\d.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z\d-]+(?:\.[a-zA-Z\d-]+)*$',
);

ValidationRule.body(
  'email',
  (value) => value is String && _emailRegExp.hasMatch(value),
  optional: false,
  message: 'Either the email field is empty or invalid!',
),
🟣 Request Query Validation
ValidationRule.query(
  'filter',
  (value) => ['name', 'age', 'email'].contains(value),
  optional: true,
  message: 'Valid filters are - name, age, and email.',
),
🟢 Request Headers Validation
ValidationRule.headers(
  HttpHeaders.contentTypeHeader,
  (value) => value == 'application/json',
  optional: false,
  message: 'The request must have application/json as content type',
),

📦 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());
}

🧩 Example

See the example Dart Frog app.

✨ Maintainers