How to use the cerberus.TypeDefinition function in Cerberus

To help you get started, we’ve selected a few Cerberus examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github scidash / sciunit / sciunit / unit_test / validator_tests.py View on Github external
def test_register(self):

        class TestClass():
            intValue = 0

            def getIntValue(self):
                return self.intValue

        from sciunit.validators import register_quantity, register_type
        from cerberus import TypeDefinition, Validator

        register_type(TestClass, "TestType1")
        q = pq.Quantity([1, 2, 3], 'J')
        register_quantity(q, "TestType2")
        self.assertIsInstance(Validator.types_mapping['TestType1'], TypeDefinition)
        self.assertIsInstance(Validator.types_mapping['TestType2'], TypeDefinition)
github scidash / sciunit / sciunit / unit_test / validator_tests.py View on Github external
def test_register(self):

        class TestClass():
            intValue = 0

            def getIntValue(self):
                return self.intValue

        from sciunit.validators import register_quantity, register_type
        from cerberus import TypeDefinition, Validator

        register_type(TestClass, "TestType1")
        q = pq.Quantity([1, 2, 3], 'J')
        register_quantity(q, "TestType2")
        self.assertIsInstance(Validator.types_mapping['TestType1'], TypeDefinition)
        self.assertIsInstance(Validator.types_mapping['TestType2'], TypeDefinition)
github scidash / sciunit / sciunit / validators.py View on Github external
def register_type(cls, name):
    """Register `name` as a type to validate as an instance of class `cls`."""
    x = TypeDefinition(name, (cls,), ())
    Validator.types_mapping[name] = x
github GIScience / openelevationservice / openelevationservice / server / api / validator.py View on Github external
# -*- coding: utf-8 -*-

from openelevationservice.server.api import api_exceptions
from openelevationservice.server.utils import logger

from cerberus import Validator, TypeDefinition

log = logger.get_logger(__name__)

object_type = TypeDefinition("object", (object,), ())
Validator.types_mapping['object'] = object_type
v = Validator()

schema_post = {'geometry': {'anyof_type': ['object', 'list', 'string'], 'required': True},
               'format_in': {'type': 'string', 'allowed': ['geojson', 'point', 'encodedpolyline', 'encodedpolyline5', 'encodedpolyline6', 'polyline'], 'required': True},
               'format_out': {'type': 'string', 'allowed': ['geojson', 'point', 'encodedpolyline', 'encodedpolyline5', 'encodedpolyline6', 'polyline'], 'default': 'geojson'},
               'dataset': {'type': 'string', 'allowed': ['srtm'], 'default': 'srtm'}
               }

schema_get = {'geometry': {'type': 'string', 'required': True},
              'format_out': {'type': 'string', 'allowed': ['geojson', 'point'], 'default': 'geojson'},
              'dataset': {'type': 'string', 'allowed': ['srtm'], 'default': 'srtm'}
              }

def validate_request(request):
    """
github GIScience / openrouteservice-py / openrouteservice / validator.py View on Github external
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations under
# the License.

"""Validates the used parameter with Cerberus."""

from cerberus import Validator, TypeDefinition
from openrouteservice import exceptions

# Add the tuple type
tuple_type = TypeDefinition("tuple", (tuple), ())
Validator.types_mapping['tuple'] = tuple_type
v = Validator()


def validator(args, function):
    """
    Validates arguments against function specific schemas
    
    :param args: Arguments to validate
    :type args: dict
    
    :param function: calling function
    :type function: string
    
    :raises: ValidationError    
    """
github scidash / sciunit / sciunit / validators.py View on Github external
def register_quantity(quantity, name):
    """Register `name` as a type to validate as an instance of class `cls`."""
    x = TypeDefinition(name, (quantity.__class__,), ())
    Validator.types_mapping[name] = x