Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Json Guard Laravel Package

league/json-guard

Unmaintained JSON Schema validator (draft 4) for PHP. Passes the full Draft 4 test suite, supports custom rule sets, and returns helpful errors with JSON Pointers. Consider opis/json-schema or swaggest/php-json-schema as alternatives.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Pros:

    • JSON Schema Draft 4 Support: Aligns with Laravel’s ecosystem (e.g., API validation, form requests) and existing JSON Schema tools like opis/json-schema or swaggest/php-json-schema.
    • Custom Rule Sets: Enables extensibility for domain-specific validation logic (e.g., business rules, legacy formats).
    • Error Granularity: JSON Pointers and structured error objects integrate well with Laravel’s validation error handling (e.g., FormRequest::validate()).
    • Dereferencing: Supports $ref for modular schema design, useful in large Laravel applications with shared schemas (e.g., microservices, API contracts).
    • MIT License: No legal barriers to adoption.
  • Cons:

    • Unmaintained: No active development or security patches. Risk of compatibility issues with PHP 8.x+ or Laravel 10+.
    • Draft 4 Only: Lacks support for newer JSON Schema drafts (Draft 7/2019/2020), which may limit future-proofing.
    • No Laravel-Specific Integrations: Requires manual setup (e.g., no built-in FormRequest facade or validation rule helpers).

Integration Feasibility

  • Laravel Compatibility:
    • Works with Laravel’s json_decode($data, false) (object mode) requirement, but conflicts with Laravel’s json_decode($data, true) (array mode) convention.
    • Can be wrapped in a custom validation rule (e.g., JsonGuardValidator) to bridge Laravel’s Validator facade.
  • Dependency Conflicts:
    • Minimal dependencies (symfony/polyfill-mbstring, symfony/translation for error localization). Low risk of version conflicts.
  • Performance:
    • Recursive validation may impact performance for deeply nested or circular JSON (mitigated by maxDepth setting).

Technical Risk

  • Migration Risk:
    • High: Unmaintained package may break with PHP/Laravel updates. Requires fork or replacement (e.g., opis/json-schema).
    • Workaround: Use as a short-term solution with fallback to json_schema extension or swaggest/php-json-schema.
  • Security Risk:
    • No recent security audits. Potential for CVEs in dependencies (e.g., symfony/polyfill-mbstring).
  • Testing Overhead:
    • Requires manual testing for edge cases (e.g., circular references, custom formats).

Key Questions

  1. Why not use opis/json-schema or swaggest/php-json-schema?
    • Evaluate trade-offs: opis/json-schema is actively maintained but may have stricter draft requirements; swaggest is lightweight but less documented.
  2. How will we handle schema updates?
    • Plan for periodic validation of schemas against the meta-schema (e.g., CI checks).
  3. What’s the fallback if this package breaks?
    • Define a migration path to a maintained alternative (e.g., json_schema PHP extension).
  4. How will errors be surfaced to users?
    • Customize error messages using symfony/translation or Laravel’s Validator::extend().
  5. Performance impact:
    • Benchmark with production-like JSON payloads to validate maxDepth settings.

Integration Approach

Stack Fit

  • Laravel Ecosystem:
    • Form Requests: Integrate via Validator::extend() or a custom rule (e.g., JsonGuard).
      use League\JsonGuard\Validator as JsonGuardValidator;
      
      Validator::extend('json_guard', function ($attribute, $value, $parameters, $validator) {
          $schema = json_decode($parameters[0]);
          $guard = new JsonGuardValidator(json_decode($value, false), $schema);
          if ($guard->fails()) {
              $validator->errors()->add($attribute, implode(', ', array_column($guard->errors(), 'message')));
              return false;
          }
          return true;
      });
      
    • API Resources: Use for request/response validation (e.g., JsonResource contracts).
    • Event Payloads: Validate serialized event data (e.g., Illuminate\Queue\SerializesModels).
  • Microservices:
    • Shared schemas via json-reference for contract validation (e.g., OpenAPI/Swagger specs).

Migration Path

  1. Short-Term:
    • Add league/json-guard as a dev dependency.
    • Create a wrapper class (e.g., App\Services\JsonGuardValidator) to abstract Laravel integration.
  2. Long-Term:
    • Replace with opis/json-schema or swaggest/php-json-schema in a phased approach:
      • Start with non-critical paths.
      • Update schemas to Draft 7/2020 for compatibility.
  3. Schema Migration:
    • Use a tool like jsonschema-to-json-schema to convert schemas between drafts.

Compatibility

  • PHP Versions: Tested up to PHP 7.4 (may require polyfills for PHP 8.x).
  • Laravel Versions: No official support, but basic usage should work in Laravel 7+.
  • Dependencies:
    • symfony/polyfill-mbstring: May conflict with Laravel’s symfony/translation (use explicit versions).
    • league/json-reference: Required for $ref support (add as a dependency).

Sequencing

  1. Phase 1: Pilot in a non-production service (e.g., admin panel API).
  2. Phase 2: Integrate with FormRequest validation.
  3. Phase 3: Extend for event/queue payload validation.
  4. Phase 4: Monitor performance and plan migration to a maintained package.

Operational Impact

Maintenance

  • Effort:
    • High: Requires manual updates, bug fixes, and schema validation.
    • Workarounds: Fork the repo or pin to a specific commit.
  • Dependencies:
    • Monitor symfony/polyfill-mbstring and league/json-reference for updates.
  • Documentation:
    • Maintain internal docs for custom rule sets and error handling.

Support

  • Issues:
    • No official support; rely on community or forked versions.
    • Track GitHub issues for league/json-guard and league/json-reference.
  • Error Handling:
    • Customize error messages for end-users (e.g., translate JSON Pointer paths).
    • Log validation failures with context (e.g., schema path, data snippet).

Scaling

  • Performance:
    • Bottlenecks: Deeply nested or circular JSON may cause timeouts (adjust maxDepth).
    • Optimizations:
      • Cache dereferenced schemas (e.g., League\JsonReference\Dereferencer with caching).
      • Use async validation for non-critical paths (e.g., Laravel queues).
  • Concurrency:
    • Stateless validator instances are thread-safe; no shared state risks.

Failure Modes

  • Schema Errors:
    • Invalid schemas may cause silent failures or cryptic errors. Validate schemas against the meta-schema in CI.
  • Circular References:
    • Unbounded recursion can crash the app (set maxDepth conservatively).
  • Dependency Failures:
    • league/json-reference or symfony/polyfill-mbstring breaking changes may halt validation.

Ramp-Up

  • Team Onboarding:
    • 1-2 Days: Learn JSON Schema basics and league/json-guard API.
    • 1 Week: Implement custom rule sets and error handling.
  • Training:
    • Document use cases (e.g., API validation, event payloads) and examples.
    • Pair programming for complex schemas (e.g., nested $ref).
  • Tooling:
    • Integrate with IDE (e.g., PHPStorm JSON Schema validation) for developer experience.
    • Add schema linting to CI (e.g., ajv-cli for Draft 7 compatibility checks).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky