ergebnis/json-pointer
RFC 6901 JSON Pointer abstraction for PHP. Create, parse, and encode reference tokens from plain strings, JSON strings, or URI fragment identifiers, with helpers to output JSON-safe and URI-safe forms. Install via Composer: ergebnis/json-pointer.
JsonPointer, ReferenceToken) aligns with Laravel’s growing emphasis on immutable collections and domain-driven design (DDD). This reduces side effects and simplifies state management.Specification class enables declarative validation of pointers, which is valuable for Laravel’s validation layer (e.g., API request validation, form data constraints).GET /posts/{id}/comments/{pointer}).composer require ergebnis/json-pointer ensures seamless adoption.#/pointer) may conflict with Laravel’s routing conventions (requires explicit namespace isolation).json_decode()/array_get() for nested data access, or is it for specialized scenarios (e.g., JSON Patch, HAL+JSON)?#/pointer) needed, or can JSON strings (/pointer) suffice?FormRequest validation? Could it extend Validator rules or replace Rule::json() for complex cases?json_decode() + array_walk()?/a~1b) be surfaced to users? Custom exceptions or Laravel’s ValidationException?symfony/yaml’s pointer utilities) or a Laravel-specific wrapper?JsonPointer to parse/construct nested JSON payloads in Illuminate\Http\Request or Symfony\Component\HttpFoundation\Request.Illuminate\Validation\Validator with custom rules using Specification (e.g., Rule::jsonPointer()).Illuminate\Http\Resources\Json\JsonResource for dynamic field selection (e.g., JsonPointer::fromString($request->input('fields'))).json-patch packages for PATCH endpoints.data->user->profile->avatar) with JsonPointer::fromString('/user/profile/avatar').GET /api/v1/data/{pointer}).JsonPointerRule extending Illuminate\Validation\Rule to validate pointers against a schema.use Ergebnis\Json\Pointer\Specification;
use Illuminate\Validation\Rule;
Rule::macro('jsonPointer', function ($pointerString) {
return new Rule(function ($attribute, $value, $fail) use ($pointerString) {
$spec = Specification::fromJsonString($pointerString);
if (!$spec->isSatisfiedBy($value)) {
$fail('The :attribute must match the JSON pointer: '.$pointerString);
}
});
});
array_get()/data_get() with JsonPointer in business logic.JsonPointer for data traversal (highest ROI).Specification for validation in Phase 2.ReferenceToken for advanced use cases (e.g., dynamic pointer construction)./user/profile) are more readable than nested array_get() calls.JsonPointer/Specification methods.Handler (e.g., JsonPointerExceptionHandler).Specification objects if used repeatedly (e.g., in API gateways).| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Malformed JSON pointer | 500 errors in API | Use try-catch with InvalidJsonPointerException and return 400. |
Pointer traversal into null |
Silent failures in business logic | Add a safeGet() method or integrate with Laravel’s optional() helper. |
| URI fragment conflicts with routes | Routing ambiguity | Prefix fragments with a custom header (e.g., X-Json-Pointer). |
| PHP version deprecation | Package incompatibility | Monitor Laravel’s PHP support policy. |
JsonPointer and Specification).JsonPointer::fromString('/user/orders/0')->get($jsonData)Specification::closure(fn($p) => $p->isRoot())array_get()/data_get() usage.How can I help you explore Laravel packages today?