<commit_before># Validating tokens

!!! Note
    The examples here fetch the configuration object from a hypothetical dependency injection container.
    You can create it in the same script or require it from a different file. It basically depends on how your system is bootstrapped.

To validate a token you must create a new validator (easier when using the [configuration object](configuration.md)) and assert or validate a token.

## Using `Lcobucci\JWT\Validator#assert()`

!!! Warning
    You **MUST** provide at least one constraint, otherwise `\Lcobucci\JWT\Validation\NoConstraintsGiven` exception will be thrown.

This method goes through every single constraint in the set, groups all the violations, and throws an exception with the grouped violations:

```php
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Token\Plain;
use Lcobucci\JWT\Validation\RequiredConstraintsViolated;

$config = $container->get(Configuration::class);
assert($config instanceof Configuration);

$token = $config->parser()->parse('...');
assert($token instanceof Plain);

$constraints = $config->validationConstraints();

try {
    $config->validator()->assert($token, ...$constraints);
} catch (RequiredConstraintsViolated $e) {
    // list of constraints violation exceptions:
    var_dump($e->violations());
}
```

## Using `Lcobucci\JWT\Validator#validate()`

!!! Warning
    You **MUST** provide at least one constraint, otherwise `\Lcobucci\JWT\Validation\NoConstraintsGiven` exception will be thrown.

The difference here is that we'll always a get a `boolean` result and stop in the very first violation:

```php
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Token\Plain;

$token = $container->get(Configuration::class);
assert($config instanceof Configuration);

$token = $config->parser()->parse('...');
assert($token instanceof Plain);

$constraints = $config->validationConstraints();

if (! $config->validator()->validate($token, ...$constraints)) {
   throw new RuntimeException('No way!');
}<commit_msg>Fix validation doc<commit_after># Validating tokens

!!! Note
    The examples here fetch the configuration object from a hypothetical dependency injection container.
    You can create it in the same script or require it from a different file. It basically depends on how your system is bootstrapped.

To validate a token you must create a new validator (easier when using the [configuration object](configuration.md)) and assert or validate a token.

## Using `Lcobucci\JWT\Validator#assert()`

!!! Warning
    You **MUST** provide at least one constraint, otherwise `\Lcobucci\JWT\Validation\NoConstraintsGiven` exception will be thrown.

This method goes through every single constraint in the set, groups all the violations, and throws an exception with the grouped violations:

```php
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Token\Plain;
use Lcobucci\JWT\Validation\RequiredConstraintsViolated;

$config = $container->get(Configuration::class);
assert($config instanceof Configuration);

$token = $config->parser()->parse('...');
assert($token instanceof Plain);

$constraints = $config->validationConstraints();

try {
    $config->validator()->assert($token, ...$constraints);
} catch (RequiredConstraintsViolated $e) {
    // list of constraints violation exceptions:
    var_dump($e->violations());
}
```

## Using `Lcobucci\JWT\Validator#validate()`

!!! Warning
    You **MUST** provide at least one constraint, otherwise `\Lcobucci\JWT\Validation\NoConstraintsGiven` exception will be thrown.

The difference here is that we'll always a get a `boolean` result and stop in the very first violation:

```php
use Lcobucci\JWT\Configuration;
use Lcobucci\JWT\Token\Plain;

$config = $container->get(Configuration::class);
assert($config instanceof Configuration);

$token = $config->parser()->parse('...');
assert($token instanceof Plain);

$constraints = $config->validationConstraints();

if (! $config->validator()->validate($token, ...$constraints)) {
   throw new RuntimeException('No way!');
}
{'fraction_non_alphanumeric': 0.1221449851042701, 'fraction_numerical': 0.0, 'mean_word_length': 5.305164319248826, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4088504', 'n_tokens_mistral': 1135, 'n_tokens_neox': 1079, 'n_words': 384}