A simple python package for JSON validation
Find a file
Iván Christian Mónaco a34ff2c9a4
Update README.md
2025-11-04 00:34:08 -03:00
isjsonvalid Initial Commit 2021-11-16 11:31:31 -03:00
tests Initial Commit 2021-11-16 11:31:31 -03:00
.gitignore gitignore 2021-11-16 11:50:32 -03:00
README.md Update README.md 2025-11-04 00:34:08 -03:00
setup.py Update setup.py 2025-11-04 00:07:24 -03:00

IsJSONValid

A simple python package for JSON validation

Installation

python -m pip install "git+https://github.com/ivancmonaco/isjsonvalid.git"

Usage

Example

from isjsonvalid import validate

spec = {
  "name": {"type": str, "required": True},
  "age": {"type": int, "min": 0, "fallback": 0, "validator": lambda x: x >= 0},
  "role": {"type": str, "one_of": ["user", "admin"], "fallback": "user"},
  "prefs": {
    "type": dict,
    "keys": {
      "tags": {
        "type": list,
        "min_length": 1,
        "element_validator": lambda s: isinstance(s, str) and len(s) <= 10
      }
    }
  }
}

data = {"name": "Ivan", "age": -5, "role": "superuser", "prefs": {"tags": ["gojira", "metal"]}}

validate(data, spec)
ValueError: {"age": {"cause": "Incorrect value for age", "hint": ""}, "role": {"cause": "Value for role must be one of ['user', 'admin']", "hint": ""}}