@@ -172,7 +172,13 @@ $signer = new HS256('12345678901234567890123456789012');
172172
173173// Extend the DefaultValidator
174174$validator = new DefaultValidator();
175- $validator->addRule('is-admin', new EqualsTo(true));
175+
176+ // The presence of the 'is-admin' claim is mandatory for successful validation. If absent, the validation will fail.
177+ // Additionally, if the rule does not match the corresponding value, the validation will also fail.
178+ $validator->addRequiredRule('is-admin', new EqualsTo(true));
179+
180+ // The 'exp' claim is optional, and the rule will be applicable only if it is present.
181+ $validator->addOptionalRule('exp', new NewerThan(time()), false);
176182
177183// Parse the token
178184$parser = new Parser($signer, $validator);
@@ -211,26 +217,6 @@ You can access the built-in Rules within the `MiladRahimi\Jwt\Validator\Rules` n
211217
212218Descriptions for each Rule can be found within their respective class doc blocks.
213219
214- #### Required and Optional Rules
215-
216- You can assign a rule as required or optional within a validator.
217- When a Rule is marked as required, the validation will fail if the associated claim is missing from the JWT claims.
218-
219- Here's an example illustrating how to designate rules as required or optional:
220-
221- ``` php
222- $validator = new DefaultValidator();
223-
224- // Add a rule as required
225- $validator->addRule('exp', new NewerThan(time()));
226-
227- // Add a rule as required again!
228- $validator->addRule('exp', new NewerThan(time()), true);
229-
230- // Add a rule as optional
231- $validator->addRule('exp', new NewerThan(time()), false);
232- ```
233-
234220#### Custom Rules
235221
236222If the provided built-in Rules don't fulfill your requirements, you can create custom Rules.
0 commit comments