AssertableJson class

A powerful JSON testing utility that provides fluent assertions for JSON structures.

The AssertableJson class combines multiple mixins to provide a rich set of testing capabilities:

  • TappableMixin for chaining operations
  • ConditionableMixin for conditional assertions
  • ConditionMixin for numeric validations
  • MatchingMixin for pattern matching
  • HasMixin for property verification

Core Features:

  • Property existence checking
  • Type validation
  • Numeric comparisons
  • Pattern matching
  • Nested object navigation
  • Array validation

Basic Usage:

final json = AssertableJson({
  'name': 'John',
  'age': 30,
  'scores': [85, 90, 95]
});

json
  .has('name')
  .whereType<String>('name')
  .where('name', 'John')
  .has('age')
  .isGreaterThan('age', 25)
  .count('scores', 3);

Nested Objects:

final json = AssertableJson({
  'user': {
    'profile': {
      'email': 'john@example.com'
    }
  }
});

json.hasNested('user.profile.email');

Conditional Testing:

json.when(isAdmin, (json) {
  json.has('adminPrivileges');
});

Array Validation:

json.has('items', 3, (items) {
  items.each((item) {
    item.has('id').has('name');
  });
});

Numeric Assertions:

json
  .isGreaterThan('age', 18)
  .isLessThan('score', 100)
  .isBetween('rating', 1, 5);

Pattern Matching:

json
  .whereType<String>('email')
  .whereContains('email', '@')
  .whereIn('status', ['active', 'pending']);

Schema Validation:

json.matchesSchema({
  'id': int,
  'name': String,
  'active': bool
});

Property Interaction Tracking: The class automatically tracks which properties have been accessed during testing. Use verifyInteracted to ensure all properties have been checked:

json
  .has('name')
  .has('age')
  .verifyInteracted(); // Fails if any properties weren't checked

See also:

  • AssertableJsonBase for core functionality
  • HasMixin for property verification methods
  • ConditionMixin for numeric assertions
  • MatchingMixin for pattern matching capabilities
Inheritance

Constructors

AssertableJson(dynamic jsonData)
Constructs an AssertableJson instance with the provided jsonData.

Properties

hashCode int
The hash code for this object.
no setterinherited
json ↔ dynamic
getter/setter pair
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

assertEmpty([String? path]) → void
Asserts that the value at the specified path is empty.
inherited
count(dynamic key, [int? length]) AssertableJson
Verifies the count of elements at a specific key or at the root level.
inherited
countBetween(dynamic key, dynamic min, dynamic max) AssertableJson
Verifies that the count of elements at key is between min and max, inclusive.
inherited
dd() → void
Prints the JSON data in a human-readable format and stops execution.
inherited
each(dynamic callback(AssertableJson)) AssertableJson
Iterates over the JSON data, either an array or an object, and calls the provided callback function for each element.
inherited
ensureSorted(Map<String, dynamic> value) → void
Ensures all nested maps within the given value have their entries sorted by key.
inherited
equals(String key, num value) AssertableJson
Asserts that the numeric value at key equals value.
inherited
etc() AssertableJson
Marks all properties in the JSON object as accessed.
inherited
exists(String path) bool
Checks if the specified path exists within the JSON data.
inherited
first(dynamic callback(AssertableJson)) AssertableJson
Scopes the current AssertableJson instance to the first element of the JSON data.
inherited
get<T>(String path) → T?
Gets the value at the specified path and casts it to the specified type T.
inherited
getList<T>(String path) List<T>
Gets a list of items at the specified path and casts them to the specified type T.
inherited
getMap<T>(String path) Map<String, T>
inherited
getRequired<T>(String path) → T
Gets the value at the specified path and casts it to the specified type T.
inherited
has(dynamic key, [dynamic arg1, AssertableJsonCallback? arg2]) AssertableJson
Verifies the existence of a key and optionally its content.
inherited
hasAll(dynamic keys) AssertableJson
Verifies the existence of all specified keys.
inherited
hasAny(dynamic keys) AssertableJson
Verifies that at least one of the specified keys exists.
inherited
hasNested(String path) AssertableJson
Verifies the existence of a nested property at path.
inherited
hasValues(List values) AssertableJson
Verifies that all specified values exist in the JSON object.
inherited
interactsWith(String key) → void
Records an interaction with the specified JSON property key.
inherited
isBetween(String key, num min, num max) AssertableJson
Asserts that the numeric value at key is between min and max inclusive.
inherited
isDivisibleBy(String key, num divisor) AssertableJson
Asserts that the numeric value at key is divisible by divisor.
inherited
isGreaterOrEqual(String key, num value) AssertableJson
Asserts that the numeric value at key is greater than or equal to value.
inherited
isGreaterThan(String key, num value) AssertableJson
Asserts that the numeric value at key is greater than value.
inherited
isLessOrEqual(String key, num value) AssertableJson
Asserts that the numeric value at key is less than or equal to value.
inherited
isLessThan(String key, num value) AssertableJson
Asserts that the numeric value at key is less than value.
inherited
isMultipleOf(String key, num factor) AssertableJson
Asserts that the numeric value at key is a multiple of factor.
inherited
isNegative(String key) AssertableJson
Asserts that the numeric value at key is negative (less than zero).
inherited
isPositive(String key) AssertableJson
Asserts that the numeric value at key is positive (greater than zero).
inherited
keys([String? path]) List<String>
Gets a list of all keys at the specified path in the JSON data.
inherited
length([String? path]) int
Gets the length of the JSON data at the specified path.
inherited
matchesSchema(Map<String, Type> schema) AssertableJson
Validates that the JSON structure matches a given schema.
inherited
missing(String key) AssertableJson
Verifies that the specified key is missing from the JSON.
inherited
missingAll(dynamic keys) AssertableJson
Verifies that all specified keys are missing from the JSON.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
notEquals(String key, num value) AssertableJson
Asserts that the numeric value at key does not equal value.
inherited
printR({int indent = 2}) → void
Prints the JSON data in a human-readable format.
inherited
scope(String key, dynamic callback(AssertableJson)) AssertableJson
Scopes the current AssertableJson instance to the specified key and calls the provided callback function with the scoped instance.
inherited
tap(dynamic callback(AssertableJson)) AssertableJson
Executes the provided callback function on the current AssertableJson instance and returns the instance to enable method chaining.
inherited
toString() String
A string representation of this object.
inherited
unless(bool condition, dynamic callback(AssertableJson)) AssertableJson
Executes the callback on this AssertableJson instance when the condition is false.
inherited
values<T>([String? path]) List<T>
Gets a list of all the values at the specified path within the JSON data.
inherited
verifyInteracted() → void
Verifies that all properties in the JSON object have been accessed.
inherited
when(bool condition, dynamic callback(AssertableJson)) AssertableJson
Executes the callback on this AssertableJson instance when the condition is true.
inherited
where(String key, dynamic expected) AssertableJson
Asserts that a property matches an expected value or satisfies a condition.
inherited
whereAll(Map<String, dynamic> bindings) AssertableJson
Asserts that multiple properties match their expected values.
inherited
whereAllType<T>(List<String> keys) AssertableJson
Asserts that multiple properties are all of type T.
inherited
whereContains(String key, dynamic expected) AssertableJson
Asserts that a property contains an expected value.
inherited
whereIn(String key, List values) AssertableJson
Asserts that a property's value is one of the provided values.
inherited
whereNot(String key, dynamic unexpected) AssertableJson
Asserts that a property does not match an unexpected value.
inherited
whereNotIn(String key, List values) AssertableJson
Asserts that a property's value is not one of the provided values.
inherited
whereType<T>([String? key]) AssertableJson
Asserts that a property is of a specific type T.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited