"Fossies" - the Fresh Open Source Software Archive

Member "zun-4.0.0/zun/api/validation/__init__.py" (16 Oct 2019, 1981 Bytes) of package /linux/misc/openstack/zun-4.0.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "__init__.py" see the Fossies "Dox" file reference documentation.

    1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
    2 # not use this file except in compliance with the License. You may obtain
    3 # a copy of the License at
    4 #
    5 # http://www.apache.org/licenses/LICENSE-2.0
    6 #
    7 # Unless required by applicable law or agreed to in writing, software
    8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
    9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
   10 # License for the specific language governing permissions and limitations
   11 # under the License.
   12 
   13 import functools
   14 
   15 from zun.api.validation import validators
   16 
   17 
   18 def validated(request_body_schema):
   19     """Register a schema to validate a resource reference.
   20 
   21     Registered schema will be used for validating a request body just before
   22     API method execution.
   23 
   24     :param request_body_schema: a schema to validate the resource reference
   25     """
   26     schema_validator = validators.SchemaValidator(request_body_schema,
   27                                                   is_body=True)
   28 
   29     def add_validator(func):
   30         @functools.wraps(func)
   31         def wrapper(*args, **kwargs):
   32             schema_validator.validate(kwargs)
   33             return func(*args, **kwargs)
   34         return wrapper
   35     return add_validator
   36 
   37 
   38 def validate_query_param(req, query_param_schema):
   39     """Register a schema to validate a resource reference.
   40 
   41     Registered schema will be used for validating a request query params
   42     just before API method execution.
   43 
   44     :param req: the request object
   45     :param query_param_schema: a schema to validate the resource reference
   46     """
   47 
   48     schema_validator = validators.SchemaValidator(query_param_schema,
   49                                                   is_body=False)
   50 
   51     def add_validator(func):
   52         @functools.wraps(func)
   53         def wrapper(*args, **kwargs):
   54             schema_validator.validate(req.params.mixed())
   55             return func(*args, **kwargs)
   56         return wrapper
   57     return add_validator