validateConstraints method

bool validateConstraints(
  1. HttpRequest request
)

Validates route constraints against a request.

Implementation

bool validateConstraints(HttpRequest request) {
  // Get route params extracted at runtime
  final routeParams = extractParameters(request.uri.path);

  return constraints.entries.every((entry) {
    final key = entry.key;
    final constraint = entry.value;

    if (key == 'openapi' || key == 'components') {
      return true;
    }

    // Special-case: domain constraint
    if (key == 'domain' && constraint is String) {
      return RegExp(constraint).hasMatch(request.headers.host ?? '');
    }

    // Special-case: function constraint
    if (constraint is bool Function(HttpRequest)) {
      return constraint(request);
    }

    // Otherwise, treat constraint as a regex pattern for the same-named path param
    final paramValue = routeParams[key];
    if (paramValue == null) {
      // If the param doesn't exist, decide how to handle
      return false;
    }

    // If our constraint is a string, interpret it as a regex
    if (constraint is String) {
      return RegExp(constraint).hasMatch(paramValue.toString());
    }

    // If not recognized, assume it's OK or handle it however you like
    return true;
  });
}