diff --git a/jsonvv/README.md b/jsonvv/README.md index 8427a50..042b573 100644 --- a/jsonvv/README.md +++ b/jsonvv/README.md @@ -187,6 +187,17 @@ Fields that end with `$group_name` are grouped together. If one of the keys is s This will require both `value` is set if and only if `isEnabled` is set. +Multiple `$` can be used to create more complex group dependencies. + +```python +keyOne$group1: "any" +keyTwo$group2: "any" +keyThree$group1$group2: "any" +``` + +If `keyThree` is set, both `keyOne` and `keyTwo` must also be set. + +However, if `keyOne` is set, `keyThree` needs to be set but `keyTwo` does not. --- @@ -724,4 +735,4 @@ graph TD ### Escaping Characters - `!`: Escapes commas, slashes, and other jsonvv characters within strings. -- `\`: Escapes within a regex pattern. \ No newline at end of file +- `\`: Escapes within a regex pattern. diff --git a/jsonvv/jsonvv/validator.py b/jsonvv/jsonvv/validator.py index 72906e5..ec09ae9 100644 --- a/jsonvv/jsonvv/validator.py +++ b/jsonvv/jsonvv/validator.py @@ -1,4 +1,4 @@ -from typing import Any, Dict, List +from typing import Any, Dict, List, Optional from .exceptions import ( MissingGroupKey, @@ -48,11 +48,16 @@ class JsonValidator: ) # Register group dependencies - if (idx := key.rfind('$')) != -1: - base_key, group = key[:idx], key[idx + 1 :] + orig_key: Optional[str] = None + while (idx := key.rfind('$')) != -1: + # Get the original key before all $ + if orig_key is None: + orig_key = key.split('$', 1)[0] + # Add to group registry + key, group = key[:idx], key[idx + 1 :] if group not in self.groups: self.groups[group] = [] - self.groups[group].append(base_key) + self.groups[group].append(orig_key) if isinstance(value, dict): # Recursively validate and parse nested dictionaries diff --git a/jsonvv/pyproject.toml b/jsonvv/pyproject.toml index 9fb31c7..380cdb1 100644 --- a/jsonvv/pyproject.toml +++ b/jsonvv/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "jsonvv" -version = "0.2.0" +version = "0.2.1" description = "JSON value validator" authors = ["daijro "] license = "MIT"