Package: okjson
{okjson}A fast, simple, and pythonic JSON Schema ValidatorUsage•Install•Features•Benchmarks•LicenseInstall$ pip install okjson⚠️ NOTE:This library has not been battle-tested, please don't use this in production. yet.FeaturesSimplicity:The schema definition is pythonic in the sense that you can directly specify the expected data type with the corresponding keyword. This is inspired by the design oftiangolo/sqlmodel.Performance:Just 250ish lines of simple and optimized code, performs better than the alternatives simply because it does relatively less work. It also usesorjsonunder the hood for serialization if you're providing a raw JSON string as input.  (SeeBenchmarks)Composability:You can write your own validation function for values, all it has to do is returnTrueon a valid input just like theregexexample below. (SeeUsage)Options:Themax_size_in_bytesoption allows you to prevent processing large payloads (in web/network scenarios) and you can opt out of data type validation with theloosely_typedoption.Helpful Error Messages:Thevalidate()function prints out helpful exception messages for easier debugging.UsageA basic example showing how the library works:For simple schemas where you just want to validate if the structure and data type of the values match, use theis_valid()method.fromokjsonimportJSONValidatorschema={'name':str,'age':int}json_string='{ "name": "Charly Gordon", "age": 32 }'assertJSONValidator().is_valid(instance=json_string,schema=schema)Every feature in a single example:For any schema that includes inner dictonaries/lists or cusom validation functions, use thevalidate()method instead.Tip:Just like how the regex match function is utilized in this example, you can compose any function, all that's required is that it returnsTrueif it passes the validation.fromokjsonimportJSONValidatorfromtypingimportUnionimportreemail_regex=re.compile(r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b')schema={'email':email_regex.match,# the value of `email` should match to the compiled email regex above'password':str,# password is of type `str`'user':{'token':str,# token is of type `str`'badges':[str],# this list can only contain `str` elements'values':[Union[str,float]]# this list can contain both `str` and `float` elements}}json_string="""{"email": "test@mail.com","password": "hunter2","user": {"token": "d08f46c929b5098b61105fdf6e24d014","badges": ["OG"],"values": ["test", 69.420]}}"""assertJSONValidator().validate(instance=json_string,schema=schema)Validating against a large schema:Got a large schema to make? Just call thecreate_schema()function on a valid JSON file that follows the desired schema.⚠️ NOTE:This currently does not validate nested dictionaries!fromokjsonimportJSONValidator# a valid/expected format of JSONexpected_valid_json=open('tests/twitter.json','r').read()# create a schema with itexpected_valid_schema=JSONValidator().create_schema(expected_valid_json)# validate `instances` with the created schemaassertJSONValidator().validate(instance=expected_valid_json,schema=expected_valid_schema)Extra options for validation:assertJSONValidator(max_size_in_bytes=1000,# payload shouldn't exceed 1000 bytesloosely_typed=False# types should be checked (strongly typed)).validate(instance=json_string,schema=schema)What's the difference between methodsis_valid()andvalidate()?Theis_valid()function only checks for the schema structure for correctness thus faster thanvalidate(). Thevalidate()function checks the schema structure along with the configurable options and handles exceptions with verbose error messages. There is no drastic performance difference betwen the two. Both are significantly fast in comparison to other schema validation libraries.NOTE:You can also pass in the result ofjson.loads()for theinstancevalue instead of the raw JSON string.BenchmarksNOTE:This is not an "apples to apples" comparison since this library works entirely differently to that of the ones in comparison here. Both of the other libraries are an implementation of theJSON Schema Specification. However, this library serves the exact same purpose with a simpler solution with more composability (callable custom functions for example) and performance benefits.$ python3 benchmarks/benchmarks.py
[-] Basic Bench okjson `is_valid()` 1000 runs: 0.004736766999485553
[-] Basic Bench okjson `validate()` 1000 runs: 0.007322061999730067
[-] Basic Bench jsonschema 1000 runs: 1.4104747559995303
[-] Basic Bench fastjsonschema 1000 runs: 0.5503115159999652

okjson is 116x faster than fastjsonschema
fastjsonschema is 2x faster than jsonschema

[-] okjson `is_valid()` example 1000 runs: 0.0018266600000060862
[-] okjson `validate()` example 1000 runs: 0.0020380820005811984
[-] jsonschema example 1000 runs: 1.1857670320005127
[-] fastjsonschema example 1000 runs: 0.3467409430004409

okjson is 189x faster than fastjsonschema
fastjsonschema is 3x faster than jsonschemaContributionWays to contribute:Suggest a featureReport a bugFix something and open a pull requestHelp me document the codeSpread the wordLicenseLicensed under the MIT License, seeLICENSEfor more information.