{"library":"pyjsg","title":"Python JSON Schema Grammar Interpreter","description":"pyjsg (Python JSON Schema Grammar interpreter) is a library that allows defining JSON schemas as Python classes and validating data against these generated structures. It provides tools to convert JSON Schema definitions into Python objects, facilitating type-safe data handling and validation within Python applications. The current stable version is 0.12.3, with releases occurring as features and bug fixes are implemented, typically not on a fixed schedule.","language":"python","status":"active","last_verified":"Fri Apr 17","install":{"commands":["pip install pyjsg"],"cli":null},"imports":["from pyjsg.jsg import JSG","from pyjsg.jsg import JSG_DUMP","from pyjsg.jsg import JSG_LOAD","from pyjsg.validate import JSGValidate"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"from pyjsg.jsg import JSG, JSG_DUMP\nfrom pyjsg.validate import JSGValidate\n\n# Define a JSON Schema using pyjsg decorators\n@JSG(\n    jsg_name=\"Person\",\n    jsg_uri=\"http://example.com/Person.jsg\",\n    jsg_description=\"A simple person schema\"\n)\nclass Person:\n    name: str\n    age: int\n    isStudent: bool = False # Optional with default\n\n# Example data\nvalid_person_data = {\"name\": \"Alice\", \"age\": 30}\ninvalid_person_data = {\"name\": \"Bob\", \"age\": \"twenty\"}\n\n# Create a validator instance\nvalidator = JSGValidate(Person)\n\n# Validate valid data\ntry:\n    validated_person = validator.validate(valid_person_data)\n    print(f\"Valid person: {validated_person}\")\n    # Access validated data as a pyjsg object\n    print(f\"Name: {validated_person.name}, Age: {validated_person.age}, Is Student: {validated_person.isStudent}\")\n    print(f\"Dumped valid person: {JSG_DUMP(validated_person)}\")\nexcept Exception as e:\n    print(f\"Validation failed for valid data: {e}\")\n\n# Validate invalid data\ntry:\n    validator.validate(invalid_person_data)\n    print(\"Invalid person data somehow passed validation.\")\nexcept Exception as e:\n    print(f\"Validation correctly failed for invalid data: {e}\")","lang":"python","description":"This quickstart defines a simple 'Person' JSON Schema using `pyjsg` decorators, creates a validator, and demonstrates validating both valid and invalid data against it. It also shows how to access the validated data as a Python object and dump it back to a dictionary.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}