diff --git a/.gitignore b/.gitignore index 53aa603..ad5a06c 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,7 @@ launch.exe # Internal testing /extra-docs pythonlib/test* +jsonvv/test* /.vscode /bundle/fonts/extra pythonlib/*.png diff --git a/jsonvv/README.md b/jsonvv/README.md new file mode 100644 index 0000000..d4874f2 --- /dev/null +++ b/jsonvv/README.md @@ -0,0 +1,706 @@ +# JSONvv + +JSON value validator + +## Overview + +This is a simple JSON schema validator library. It was created for Camoufox to validate passed user configurations. Because I found it useful for other projects, I decided to extract it into a separate library. + +JSONvv's syntax parser is written in pure Python. It does not rely on any dependencies. + +### Example + +
| Configuration | +Validator | +
|---|---|
| + +```python +config = { + "username": "johndoe", + "email": "johndoe@example.com", + "age": 30, + "chat": "Hello world!", + "preferences": { + "notifications": True, + "theme": "dark" + }, + "allowed_commands": [ + "/help", "/time", "/weather" + ], + "location": [40.7128, -74.0060], + "hobbies": [ + { + "name": "Traveling", + "cities": ["Paris", "London"] + }, + { + "name": "reading", + "hours": { + "Sunday": 2, + "Monday": 3, + } + } + ] +} +``` + + | ++ +```python +validator = { + "username": "str", # Basic username + "email": "str[/\S+@\S+\.\S+/]", # Validate emails + "age": "int[>=18]", # Age must be 18 or older + "chat": "str | nil", # Optional chat message + "preferences": { + "notifications": "bool", + "theme": "str[light, dark] | nil", # Optional theme + }, + # Commands must start with "/", but not contain "sudo" + "allowed_commands": "array[str[/^//] - str[/sudo/]]", + # Validate coordinate ranges + "location": "tuple[double[-90 - 90], double[-180 - 180]]", + # Handle an array of hobby types + "hobbies": "array[@traveling | @other, >=1]", + "@traveling": { + # Require 1 or more cities/countries iff name is "Traveling" + "*name,type": "str[Traveling]", + "*cities,countries": "array[str[A-Za-z*], >=1]", + }, + "@other": { + "name,type": "str - str[Traveling]", # Non-traveling types + # If hour(s) is specified, require days have >0 hours + "/hours?/": { + "*/.*day/": "int[>0]" + } + } +} +``` + + | +