A library to apply JSON patches with rule-based access control.
Project description
JsonPatchRules
JsonPatchRules is a Python library designed to facilitate the application of JSON patch operations while enforcing customizable validation rules. This library ensures that updates to JSON objects adhere to predefined permissions, making it ideal for systems that require granular access control.
Features
- Rule-Based Validation: Define rules that specify which paths in a JSON object are allowed to be updated.
- Wildcard Support: Use wildcards to specify rules for dynamic keys and array indices.
- Data Integrity: Ensure that only permitted paths can be updated, preserving the integrity of the JSON structure.
Installation
Install JsonPatchRules using pip:
pip install jsonpatchrules
Expanded Example Scenario
Let's imagine a more complex JSON structure representing a user profile, including nested objects for personal details, permissions, and an array of contact methods.
from jsonpatchrules import patch_rules
# Define a complex JSON object
data = {
"user": {
"name": "John Doe",
"email": "john@example.com",
"permissions": {
"edit": True,
"delete": False
},
"contacts": [
{"type": "home", "number": "1234567890"},
{"type": "work", "number": "0987654321"}
]
}
}
# Define rules to specify allowed updates
rules = [
"user.{name,email}", # Allow updates to both the name and email
"user.permissions.*", # Allow updates to any permissions fields
"user.contacts[*].number" # Allow updates to the phone number in any contact
]
# Initialize patch rules
patch = patch_rules(rules)
# Define new data to apply
new_data = {
"user": {
"name": "Jane Doe", # This update is allowed
"email": "jane@example.com", # This update is allowed
"permissions": {
"edit": False, # This update is allowed
"delete": True # This update is allowed
},
"contacts": [
{"type": "home", "number": "1111111111"}, # This update is allowed
{"type": "work", "number": "2222222222"} # This update is allowed
]
}
}
# Apply the patch
result = patch.apply(data, new_data)
# Output the updated JSON object
print("Patched Data:", result.patched_data)
print("Denied Paths:", result.denied_paths)
print("Successed Paths:", result.successed_paths)
Explanation of This Example
-
Complex JSON Structure: The
data
dictionary includes nested objects and arrays, reflecting a realistic data structure you might encounter in applications. -
Diverse Rule Definitions:
"user.{name,email}"
: This rule uses curly braces{}
to specify that bothname
andemail
fields underuser
can be updated."user.permissions.*"
: The wildcard*
allows changes to any fields underpermissions
, demonstrating flexibility in what can be updated without listing every possible field."user.contacts[*].number"
: This rule demonstrates how to allow updates to specific fields within any object in an array. The[*]
wildcard allows the operation on any element index within thecontacts
array.
-
Patch Application and Results: The
apply
method is used to attempt updatingdata
withnew_data
based on the definedrules
. The results show which paths were successfully updated and which were denied, although in this example, all updates conform to the rules.
This example is comprehensive, demonstrating key features of your library and providing users with a clear understanding of how to implement rule-based JSON patching in their applications.
Usage
Defining Rules
Rules are strings that specify the allowed paths in the JSON object:
"user.name"
: Allows updates to thename
field under theuser
key."user.*"
: Allows updates to any field underuser
."array[*]"
: Allows updates to any element inarray
.
Applying Patches
To apply a patch:
patch = patch_rules(rules)
result = patch.apply(original_data, new_data)
The result
object will contain details about the operation, including which paths were updated successfully and which were denied.
Contributing
Contributions are welcome! Please feel free to submit pull requests, report bugs, and suggest features.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for python_json_patch_rules-0.2.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a21344d244162b9025bf67b0f99cc17ca67a953748b0a4c25fa478ff8147f57 |
|
MD5 | a95c2771de72a0a0e0c728b9f4b725bc |
|
BLAKE2b-256 | 1364f98ef9d9c77f4ce715e32a805e07be25c7e5c5d5b2e9273d6021a301b232 |
Hashes for python_json_patch_rules-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7041ed1b6baf041455961075c7ce1217ed5df1f310cae8ed2a062a85f4048c73 |
|
MD5 | e1dd4de231c4f74b6362c7d14c771c2c |
|
BLAKE2b-256 | 4eee38cbb9213b7e72ef934228679b9143e2ad29ac29b44a78d3a0842cc53eb8 |