How to use the jsonmodels.errors function in jsonmodels

To help you get started, we’ve selected a few jsonmodels 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 jazzband / jsonmodels / tests / test_validation.py View on Github external
def test_validation_multiline():
    validator = validators.Regex('^s.*e$')
    validator.validate('some')
    with pytest.raises(errors.ValidationError):
        validator.validate('some\nso more')

    validator = validators.Regex('^s.*e$', multiline=True)
    validator.validate('some')
    validator.validate('some\nso more')
github jazzband / jsonmodels / tests / test_data_initialization.py View on Github external
class Parking(models.Base):

        location = fields.StringField()
        cars = fields.ListField([Viper, Lamborghini])

    data = {
        'location': 'somewhere',
        'cars': object(),
    }

    with pytest.raises(errors.ValidationError):
        Parking(**data)

    parking = Parking()
    with pytest.raises(errors.ValidationError):
        parking.populate(**data)
github jazzband / jsonmodels / tests / test_validation.py View on Github external
def test_get_field_not_found():

    class Person(models.Base):

        children = fields.ListField()

    alan = Person()

    with pytest.raises(errors.FieldNotFound):
        alan.get_field('bazinga')
github jazzband / jsonmodels / tests / test_jsonmodels.py View on Github external
def test_base_field_should_not_be_usable():

    class Person(models.Base):

        name = fields.BaseField()

    alan = Person()

    with pytest.raises(errors.ValidationError):
        alan.name = 'some name'

    with pytest.raises(errors.ValidationError):
        alan.name = 2345
github jazzband / jsonmodels / tests / test_struct.py View on Github external
def test_to_struct_basic():

    class Person(models.Base):

        name = fields.StringField(required=True)
        surname = fields.StringField(required=True)
        age = fields.IntField()
        cash = fields.FloatField()

    alan = Person()
    with pytest.raises(errors.ValidationError):
        alan.to_struct()

    alan.name = 'Alan'
    alan.surname = 'Wake'
    assert {'name': 'Alan', 'surname': 'Wake'} == alan.to_struct()

    alan.age = 24
    alan.cash = 2445.45

    pattern = {
        'name': 'Alan',
        'surname': 'Wake',
        'age': 24,
        'cash': 2445.45,
    }
github openstack / dragonflow / dragonflow / cli / df_db.py View on Github external
except KeyError:
        print("Model {} is not found in models list".format(table))
        return

    try:
        obj = model.from_json(json_str)
    except ValueError:
        print("Json {} is not valid".format(json_str))
        return
    except TypeError:
        print("Json {} is not applicable to {}".format(json_str, table))
        return

    try:
        nb_api.create(obj)
    except errors.ValidationError:
        print("Json {} is not applicable to {}".format(json_str, table))
github openstack / dragonflow / dragonflow / cli / df_db.py View on Github external
:param table: table name where object should be added
    :param action_handler: which method to run on the received json
    :return: None
    """
    try:
        obj = model_object_from_json(json_str, table)
    except ValueError:
        print("Record {} is not valid".format(json_str))
        return
    except TypeError:
        print("Json {} does not match schema of {}".format(json_str, table))
        return

    try:
        action_handler(obj)
    except errors.ValidationError:
        print("Json {} failed validation for {}".format(json_str, table))
github openstack / dragonflow / dragonflow / db / models / qos.py View on Github external
def validate(self):
        """
        Validate the rule. That is, verify dscp_mark is set if type is
        dscp_marking, and that max_burst_kbps is set if type is bandwidth_limit
        """
        super(QosPolicyRule, self).validate()

        if self.type == RULE_TYPE_DSCP_MARKING:
            if self.dscp_mark is None:
                raise errors.ValidationError("dscp_mark is required if "
                                             "type is dscp_marking")
        elif self.type == RULE_TYPE_BANDWIDTH_LIMIT:
            if self.max_burst_kbps is None:
                raise errors.ValidationError("max_burst_kbps is required if "
                                             "type is bandwidth_limit")
github openstack / dragonflow / dragonflow / db / field_types.py View on Github external
def validate(self, value):
        if self.required and not value:
            raise errors.ValidationError(_('Field is required!'))

        if value is None:
            return

        for elem in value:
            if elem not in self._valid_values:
                raise errors.ValidationError(
                    _('{value} is not one of: [{valid_values}]').format(
                        value=value,
                        valid_values=', '.join(self._valid_values)))
github jazzband / jsonmodels / jsonmodels / builders.py View on Github external
def build(self):
        if issubclass(self.type, six.string_types):
            obj = {'type': 'string'}
        elif issubclass(self.type, bool):
            obj = {'type': 'boolean'}
        elif issubclass(self.type, int):
            obj = {'type': 'number'}
        elif issubclass(self.type, float):
            obj = {'type': 'number'}
        else:
            raise errors.FieldNotSupported(
                "Can't specify value schema!", self.type
            )

        return obj if not self.nullable else {'type': [obj['type'], 'null']}