Package: jsvl
JSON ValidatorJSON Validatoris a Python library designed to enhance the validation of JSON documents. It provides a simple yet powerful set of features for schema-based validation, offering flexibility and precision in handling JSON data.OutlineOverviewKey FeaturesSchema DefinitionFlexible Data Type ValidationKey Presence ValidationRequired and Optional FieldsChild Value BypassValue BindingsRegular Expression BindingsString Value ConstraintsNumeric Value ConstraintsCase ConstraintsSpacing in String ValuesCustom Extension LibraryInstallationCommand line guideHow to useUse in ProjectControl ConfigsRegister Custom Validation FiltersUser guideAvailable KeywordsSupport Data TypesLicenseOverviewJSON Validatoris a feature-rich Python library designed to elevate the validation of JSON documents by offering an extensive schema-based validation system. It empowers developers to define custom schemas, providing fine-grained control over the structure, data types, and constraints of their JSON data.Key Features1. Schema DefinitionDefine a schema for your JSON documents, specifying keys, data types, and constraints. The schema acts as a blueprint for validation.2. Flexible Data Type ValidationControl the data types of each key in your JSON document. Enforce strict typing or allow flexibility based on your requirements.3. Key Presence ValidationDetect and handle invalid keys in your JSON document that are not defined in the schema, ensuring data integrity.4. Required and Optional FieldsDesignate which fields are required and which are optional, guiding users in creating valid JSON documents.5. Child Value BypassAllow bypassing the validation of child values, providing flexibility when certain sections of the JSON document don't require strict validation.6. Value BindingsSupport value bindings to establish relationships between different parts of the JSON document, enhancing the expressiveness of your schemas.7. Regular Expression BindingsSupport regular expression bindings for precise validation of values..8. String Value ConstraintsControl the minimum and maximum length of string values, applying constraints to ensure data meets specific length requirements.9. Numeric Value ConstraintsDefine minimum and maximum values for integer and float types, allowing precise control over the numeric range of your data.10. Case ConstraintsApply case constraints to string types, specifying whether they should be in lower, upper, or mixed case.11. Spacing in String ValuesOptionally allow spacing in string data types, enhancing readability in scenarios where formatted text is essential.12. Custom Extension LibraryExtend the functionality of the library by creating custom extensions, tailoring the validation process to your unique needs.Installationpip install jsvl -UCommand line guideList of available commandsCommandDefinition-s or --schemaProvide a schema as URL, JSON, file or a directory for validating the documents.-d or --docProvide a document json, file or a directory path for validation.Note: if the schema source is a directory then provided schema should be in a directory and should have the same name with _schema postfix.-csfp or --change-schema-file-postfixThis argument will be used to change the schema file postfix name.--disable-tagsPass this flag to disable the output tags from logs.--plain-outputPass this flag to remove the formatting from logs.--disable-logsPass this flag to disable all the logs.--show-validation-sourcePass this flag to show the validation class name with logs.--tight-spacePass this flag to disable the allow_space globally.-ml or --min-lengthSet the minimum length globally, default is 0.-xl or --max-lengthSet the maximum length globally, default is None.-mv or --min-valueSet the minimum value globally, default is 0.-xv or --max-valueSet the maximum value globally, default is None.-c or --caseSet the text constraints globally, default is None. setAvailable Keywords-v or --versionCheck version.-h or --helpFor help.How to useCommand lineValidate a single json document.~$ jsvl -s /path/to/schema.json -d /path/to/document.jsonValidate a single json document with URL.~$ jsvl -s http://www.yourdomain.com/schema.json -d /path/to/document.jsonValidate multiple documents with a single schema.~$ jsvl -s /path/to/schema.json -d /path/to/documents/Validate multiple documents with multiple schema.~$ jsvl -s /path/to/schema/ -d /path/to/documents/Disable informative tags from the output.~$ jsvl -s path/to/schema.json -d /path/to/document.json --disable-tagsRemove formatting from the output.~$ jsvl -s path/to/schema.json -d /path/to/document.json --plain-outputUse in Project:Importvalidatefunction fromjsvl.core.validatormodule and validate the document with multiple options.fromjsvl.core.validatorimportvalidate# schema jsonschema="http://www.youdomain.com/schema.json"or"path/to/schema.json"or"path/to/all_schema_dir/"or{}# document jsondocument="path/to/document.json"or"path/to/all_documents_dir/"or{}or[]# perform validationresult=validate(schema,document)Control Configs:importjsvl.configascfgfromjsvl.utils.utilimportreserved_key# change schema file postfix default is _schemacfg.configs[cfg.schema_file_postfix]="_schema"# disable the output tags from logscfg.configs[cfg.enable_output_tags]=False# remove the formatting from logscfg.configs[cfg.formatted_output]=False# disable all the logscfg.configs[cfg.enable_output_logs]=True# show the validation class name with logscfg.configs[cfg.enable_validation_source]=True# disable the allow_space globallycfg.configs[cfg.allow_space]=True# set the minimum length globally, default is 0.cfg.configs[cfg.min_length]=0# set the maximum length globally, default is None.cfg.configs[cfg.max_length]=1000# set the minimum value globally, default is 0.cfg.configs[cfg.min_value]=0# set the maximum value globally, default is None.cfg.configs[cfg.max_value]=1000# set the text constraints globally, default is None.cfg.configs[cfg.case]=reserved_key.upperRegister Custom Validation Filters:You can register two types of validation filter.Schema Validationfromjsvl.validations.schema_validationsimportschema_validation_set,SchemaValidation# creating custom validation class for schema validationclassCustomSchemaValidation(SchemaValidation):defvalidate(self,key,schema,path):pass# register validation in schema validation setschema_validation_set.add(CustomSchemaValidation())Document Validationfromjsvl.validations.doc_validationsimportdoc_validation_set,DocValidation# creating custom validation class for document validationclassCustomDocValidation(DocValidation):defvalidate(self,key,schema,doc,path,index,doc_is_dynamic):pass# register validation in document validation setdoc_validation_set.add(CustomDocValidation())User guideValidating a sample document.schema.json{"cart":{"__data_type__":"object","items*":{"__data_type__":"object_array","title*":{"__case__":"__title__"},"description":{"__min_length__":20},"email":{"__bind_regex__":"__email__"},"category*":{"__bind__":"category"},"keywords":{"__bind_regex__":"^[a-zA-Z]+$","__rem__":"support only alphabets"},"quantity*":{"__data_type__":"integer","__min_value__":1},"discount*":{"__data_type__":"float","__max_value__":45.5}}},"~others":{},"__binder__":{"category":["Health","Fashion","Lifestyle"]},"__defaults__":{"__min_value__":100,"__max_value__":1000}}document.json{"cart":{"items":[{"title":"Product 1","description":"","email":"john@gmail.com","quantity":1,"category":"Fashion","keywords":"Health","discount":30.0},{"title":"Product 2","quantity":4,"category":"Health","keywords":"Health","discount":0.0}]},"others":{"coords":{"lat":0.022,"lng":2.34}}}Available keywords:KeywordDescriptionDefaultScope*Add an asterisk keyword to the end of the key to mark it as required.falseCould be add on any keyword. Excludedreserved keywords~Add a tilde keyword to the start of the key to bypass all the validation of the immediate child.noneCould be add on any keyword. Excludedreserved keywords__data_type__Set data type on field, multiple data types could be supplied using pipe | for example.string|object.stringobject__alow_space__Allow space on text.truestring__min_length__Set the minimum length.0stringand any type ofarray__max_length__Set the maximum length.nonestringand any type ofarray__min_value__Set the minimum value.0integerandfloat__max_value__Set the maximum value.noneintegerandfloat__case__Apply case constraint on value.Available case constraints:__upper____lower____title__nonestring__bind_regex__Apply custom regular expression or use some pre-define.Note:__bind_regex__will take higher precedence over__bind__if both are defined.Some pre-defined expressions:__email____alpha____numeric____alphanumeric____ipv4____ipv6__falsestring__rem__Proivde custom error when regex gets failed.falsestring__bind__Bind value from defined valueset.noneignore the data type__binder__This is a special keyword and that will be used only root object of the schema where you can define binding valueset.noneonly rootobject__defaults__This is a special keyword and that will be used to apply the defined constraints globally on the document.Available properties that could be set globally:__allow_space____min_length____max_length____min_value____max_value____case__noneonly rootobjectSupport Data Typesstringintegerfloatboolobjectstring_arrayinteger_arrayfloat_arraybool_arrayarrayLicenseGPL v3