1. Import the module
Import jsonschemafrom jsonschema import schemas,validatorsfrom jsonschema.validators import Draft4validator
2. Custom Parameter Validation rules
schema = { ' description ': ' Validate the request json data parameter ', ' type ': ' object ', ' properties ': {' sender ': {' type ' ': ' String ', }, ' Msgtype ': { ' type ': ' String ' }, ' receiver ': { ' type ': ' String ' }, ' msg ': { ' type ': ' object ', ' content ': {' type ': ' String '}, ' Required ': [' type ', ' content '] } }, ' Required ': [' sender ', ' receiver ', ' msg '}
3. Specific Use
Class Validator (object): def __init__ (self, name): self.schema = name Checker = Jsonschema. Formatchecker () self.validator = validators. Draft4validator (self.schema,format_checker=checker) def validate (self,data): try: Self.validator.validate (data) return True except Jsonschema. ValidationError as ex: raise Exception (ex.message) return falseverifi_obj = Validator (Schema) # You can write a py file separately, Data is to be validated, call the Validate method
The use of Jsonschema in the development of interface