Module: Ruby::Rego::Builtins::JsonPatch
- Extended by:
- RegistryHelpers
- Defined in:
- lib/ruby/rego/builtins/json_patch.rb
Overview
JSON Patch (json.patch), matching OPA — applies an RFC 6902 operation list to a
document. Each operation is an object with op (add/remove/replace/move/copy/test) and
a path (an RFC 6901 JSON pointer string, with ~1 for / and ~0 for ~, or an
array of segments — a string for an object key, a string or integer for an array index);
add/replace/test need a value, move/copy need a from. In a string path a
leading run of slashes is stripped, the empty string "" addresses the whole document,
and a path that is only slashes (/, //) addresses the empty-string key "".
Operations apply in order. Any failure — a non-array operand, an operation that is not an
object, a missing/invalid field, a path into a non-existent or scalar location, an
out-of-range array index, or a failed test — yields undefined.
Constant Summary collapse
- ARRAY_INDEX =
A canonical non-negative array index (no leading zeros), matching how OPA renders and accepts an index (its EditTree rejects leading zeros).
/\A(?:0|[1-9]\d*)\z/- JSON_PATCH_FUNCTIONS =
{ "json.patch" => { arity: 2, handler: :patch } }.freeze
Class Method Summary collapse
-
.patch(document_value, operations_value) ⇒ Object
Applies the operation list to the document, or undefined on any failure.
-
.register! ⇒ Ruby::Rego::Builtins::BuiltinRegistry
Methods included from RegistryHelpers
Class Method Details
.patch(document_value, operations_value) ⇒ Object
Applies the operation list to the document, or undefined on any failure.
46 47 48 49 50 |
# File 'lib/ruby/rego/builtins/json_patch.rb', line 46 def self.patch(document_value, operations_value) Base.assert_type(operations_value, expected: ArrayValue, context: "json.patch") document = document_value.to_ruby operations_value.to_ruby.reduce(document) { |doc, operation| apply(doc, operation) } end |
.register! ⇒ Ruby::Rego::Builtins::BuiltinRegistry
33 34 35 36 37 |
# File 'lib/ruby/rego/builtins/json_patch.rb', line 33 def self.register! registry = BuiltinRegistry.instance register_configured_functions(registry, JSON_PATCH_FUNCTIONS) registry end |