lcobucci/jwt · error · Lcobucci\JWT\Validation\NoConstraintsGiven

No constraint given.

Error message

No constraint given.

What it means

Lcobucci\JWT\Validation\Validator::assert() requires at least one Constraint. When called with an empty variadic constraint list it immediately throws NoConstraintsGiven('No constraint given.') instead of silently succeeding (which would falsely mean 'token is valid'). This is a guard against meaningless validation calls.

Solutions

  1. Check the constraint list with count($constraints) > 0 (or !== []) before calling assert() and skip validation or fail fast in your own code.
  2. Fix the constraint-assembly logic so at least the constraints your policy requires (e.g. SignedWith, IdentifiedBy) are always added.
  3. If empty means 'nothing to validate', restructure to call assert() only when constraints exist.

Example fix

// before
$validator->assert($token, ...$this->constraintsFromConfig());
// after
$constraints = $this->constraintsFromConfig();
if ($constraints === []) {
    throw new \LogicException('No validation constraints configured');
}
$validator->assert($token, ...$constraints);
Defensive patterns

Strategy: validation

Validate before calling

if ($constraints === []) {
    throw new \LogicException('Refusing to validate token: no constraints configured');
}

Type guard

function hasConstraints(Lcobucci\JWT\Validation\Constraint ...$constraints): bool
{
    return $constraints !== [];
}

Try / catch

try {
    $validator->assert($token, ...$constraints);
} catch (Lcobucci\JWT\Validation\NoConstraintsGiven $e) {
    // configuration bug: log and reject the request
    throw new \RuntimeException('JWT validation misconfigured: no constraints', 0, $e);
}

Prevention

When it happens

Trigger: Calling $validator->assert($token) with no Constraint arguments — e.g. building the constraint list in a loop that produced zero constraints, or forwarding an empty array via ...$constraints.

Common situations: Dynamically assembling constraints from configuration where all constraints were disabled or the config section was missing/empty; refactoring that dropped the constraints argument; conditional code paths that skip adding any constraint.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of lcobucci/jwt@375813049c (2026-09-14). Data as JSON: /api/errors/d5ab76a30ec51c10. Report an issue: GitHub.

Appendix: source

Thrown at src/Validation/Validator.php:13

<?php
declare(strict_types=1);

namespace Lcobucci\JWT\Validation;

use Lcobucci\JWT\Token;

final readonly class Validator implements \Lcobucci\JWT\Validator
{
    public function assert(Token $token, Constraint ...$constraints): void
    {
        if ($constraints === []) {
            throw new NoConstraintsGiven('No constraint given.');
        }

        $violations = [];

        foreach ($constraints as $constraint) {
            $this->checkConstraint($constraint, $token, $violations);
        }

        if ($violations !== []) {
            throw RequiredConstraintsViolated::fromViolations(...$violations);
        }
    }

    /** @param ConstraintViolation[] $violations */
    private function checkConstraint(
        Constraint $constraint,
        Token $token,
        array &$violations,

View on GitHub (pinned to 375813049c)