assertable_json 0.1.0 copy "assertable_json: ^0.1.0" to clipboard
assertable_json: ^0.1.0 copied to clipboard

A package that provides a way to assert JSON objects.

AssertableJson #

A powerful, fluent JSON testing utility for Dart that makes it easy to write expressive and readable assertions for JSON data structures.

Features #

  • 🔍 Property Validation: Assert presence/absence of keys, nested properties, and values
  • 📐 Type Checking: Verify types with strong type safety
  • 🔢 Numeric Assertions: Compare numbers, check ranges, validate mathematical properties
  • 🎯 Pattern Matching: Test values against patterns and custom conditions
  • 📦 Schema Validation: Validate JSON structure against expected schemas
  • 🎭 Conditional Testing: Execute assertions based on conditions
  • 🔄 Chain Operations: Fluent API for chaining multiple assertions
  • 🎨 JSON String Testing: Special support for testing JSON string responses

Installation #

Add to your pubspec.yaml:

dependencies:
  assertable_json: ^0.1.0
copied to clipboard

Basic Usage #

import 'package:assertable_json/assertable_json.dart';

void main() {
  test('verify user data', () {
    final json = AssertableJson({
      'user': {
        'id': 123,
        'name': 'John Doe',
        'email': 'john@example.com',
        'age': 30,
        'roles': ['admin', 'user']
      }
    });

    json
      .has('user', (user) => user
        .has('id')
        .whereType<int>('id')
        .has('name')
        .whereType<String>('name')
        .has('email')
        .whereContains('email', '@')
        .has('age')
        .isGreaterThan('age', 18)
        .has('roles')
        .count('roles', 2));
  });
}
copied to clipboard

Key Features in Detail #

Property Assertions #

json
  .has('name')               // Verify key exists
  .hasNested('user.email')   // Check nested properties
  .hasAll(['id', 'name'])    // Check multiple properties
  .missing('deletedAt');     // Verify key doesn't exist
copied to clipboard

Numeric Validations #

json
  .isGreaterThan('age', 18)
  .isLessThan('price', 100)
  .isBetween('rating', 1, 5)
  .isDivisibleBy('count', 5)
  .isPositive('score');
copied to clipboard

Type & Pattern Matching #

json
  .whereType<String>('email')
  .whereContains('email', '@')
  .whereIn('status', ['active', 'pending'])
  .where('age', (value) => value >= 18);
copied to clipboard

Schema Validation #

json.matchesSchema({
  'id': int,
  'name': String,
  'email': String,
  'age': int,
  'optional?': String  // Optional field
});
copied to clipboard

Conditional Testing #

json
  .when(isAdmin, (json) {
    json.has('adminPrivileges');
  })
  .unless(isGuest, (json) {
    json.has('personalInfo');
  });
copied to clipboard

JSON String Testing #

When working with JSON string responses:

final response = AssertableJsonString('{"status": "success", "data": {"id": 1}}');

response
  .assertCount(2)
  .assertFragment({'status': 'success'})
  .assertStructure({
    'status': String,
    'data': {
      'id': int
    }
  });
copied to clipboard

Array Testing #

json
  .has('items', 3, (items) {
    items.each((item) {
      item
        .has('id')
        .has('name')
        .whereType<bool>('active');
    });
  });
copied to clipboard

Additional Features #

  • Property Tracking: Automatically tracks which properties have been tested
  • Debugging: Built-in debugging tools with dd() and printR()
  • Custom Assertions: Extend with your own assertion methods
  • Type Safety: Full type safety with Dart's static typing
  • Fluent API: Chain multiple assertions for cleaner code
  • Detailed Errors: Clear error messages for failed assertions

Contributing #

Contributions are welcome! Please feel free to submit a Pull Request.

License #

This project is licensed under the MIT License - see the LICENSE file for details.

3
likes
0
points
330
downloads

Publisher

verified publisherglenfordwilliams.com

Weekly Downloads

2024.09.15 - 2025.03.30

A package that provides a way to assert JSON objects.

Repository (GitHub)
View/report issues

Topics

#json #testing #assertions

Funding

Consider supporting this project:

www.buymeacoffee.com

License

unknown (license)

Dependencies

test

More

Packages that depend on assertable_json