Skip to content

Feature: add optional and nullable rules #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Feb 7, 2023

Conversation

joshcummingsdesign
Copy link
Contributor

@joshcummingsdesign joshcummingsdesign commented Feb 4, 2023

This PR removes the Validator::validateRulesandValues method in favor of new rules. The validation method was deemed unnecessary, as values without rules are already omitted, and rules without values return a null value — the validator itself doesn't need to throw any exceptions as the end result is still safe data.

Example of data omitted without rules:

$request = [
  'firstname' => 'Jason',
  'lastname' => 'Adams',
  'city' => 'Carlsbad',
];

$validator = new Validator([
  'firstname' => ['required'],
  'lastname' => ['required'],
], $request);

$expected = [
  'firstname' => 'Jason',
  'lastname' => 'Adams',
];

When reviewing the following, note that if a field is omitted entirely from the values, then its considered null. So both optional and nullable would work for omitted values.

Optional

If the value is null or an empty string, then the remaining validation rules are skipped.

Example:

$request = [
  'firstname' => 'Jason',
  'lastname' => 'Adams',
  'age' => ''
];

$validator = new Validator([
  'firstname' => ['required'],
  'lastname' => ['required'],
  'age' => ['optional', 'integer']
], $request);

$expected = [
  'firstname' => 'Jason',
  'lastname' => 'Adams',
  'age' => ''
];

Nullable

If the value is null, then the remaining validation rules are skipped.

Example:

$request = [
  'firstname' => 'Jason',
  'lastname' => 'Adams',
  'city' => null
];

$validator = new Validator([
  'firstname' => ['required'],
  'lastname' => ['required'],
  'city' => ['nullable', 'in:san diego'],
], $request);

$expected = [
  'firstname' => 'Jason',
  'lastname' => 'Adams',
  'city' => null,
];

@JasonTheAdams JasonTheAdams changed the base branch from master to develop February 4, 2023 01:07
@JasonTheAdams JasonTheAdams changed the title Remove Validator::validateRulesAndValues Feature: add optional and nullable rules Feb 4, 2023
@JasonTheAdams JasonTheAdams requested review from borkweb and removed request for JasonTheAdams February 6, 2023 19:57
@JasonTheAdams
Copy link
Member

@borkweb Since I ended up taking over this PR, I'd appreciate if if you'd give this a code review. Thanks!

Copy link
Member

@JasonTheAdams JasonTheAdams left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@joshcummingsdesign needs this for his work, so we went back through this and reviewed it together. It's passing all tests and works, so I'm going to mark this approved, merge, and release it!

@JasonTheAdams JasonTheAdams merged commit 756a105 into develop Feb 7, 2023
@JasonTheAdams JasonTheAdams deleted the feature/validate-rules-and-values branch February 7, 2023 19:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants