The use of Jsonschema in the development of interface

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.