Skip to content

Null and Empty Assertions

Assertions for validating null values, empty values, and blank strings.

Assert that a value is null.

use Cline\Assert\Assertions\Assertion;
Assertion::null($value);
Assertion::null($optionalField, 'Field must be null');

Assert that a value is not null.

Assertion::notNull($value);
Assertion::notNull($user, 'User is required');

Assert that a value is not empty (using empty() check).

Assertion::notEmpty($value);
Assertion::notEmpty($name, 'Name is required');

Assert that a value is empty (using empty() check).

Assertion::noContent($value);
Assertion::noContent($deletedField, 'Field must be empty');

Assert that a value is not blank (not empty string or whitespace-only).

Assertion::notBlank($value);
Assertion::notBlank($description, 'Description cannot be blank');
Assertion::noContent(''); // empty string
Assertion::noContent(0); // integer zero
Assertion::noContent(null); // null
Assertion::noContent(false); // boolean false
Assertion::noContent([]); // empty array
Assertion::null(null); // Pass
Assertion::null(''); // Fail - empty string, not null
Assertion::noContent(null); // Pass
Assertion::noContent(''); // Pass
Assertion::noContent(0); // Pass
Assertion::notBlank('hello'); // Pass
Assertion::notBlank(' '); // Fail - whitespace only
Assertion::notBlank(''); // Fail - empty string
use Cline\Assert\Assert;
Assert::that($username)
->notNull('Username is required')
->notEmpty('Username cannot be empty')
->string();
Assert::that($description)
->string()
->notBlank('Description cannot be blank');
Assert::that($email)
->notNull('Email is required')
->notEmpty('Email cannot be empty')
->notBlank('Email cannot be blank')
->email('Invalid email format');
Assert::thatNullOr($phoneNumber)
->string()
->e164('Invalid phone format');
Assert::lazy()
->that($form['name'] ?? null, 'name')->notNull()->notBlank()
->that($form['email'] ?? null, 'email')->notNull()->notBlank()->email()
->verifyNow();
// Allow null OR validate if not null
Assert::thatNullOr($middleName)
->string()
->minLength(2);
// notEmpty allows whitespace
Assertion::notEmpty(' '); // Pass
// notBlank rejects whitespace
Assertion::notBlank(' '); // Fail
Assert::that($user)
->notNull('User not found')
->isObject();
Assert::that($comment)
->notBlank('Comment cannot be empty');
Assert::that($name)
->notNull()
->string()
->notBlank();