Python marshmallow.validate.Range() Examples

The following are 13 code examples of marshmallow.validate.Range(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module marshmallow.validate , or try the search function .
Example #1
Source File: test_validation.py    From marshmallow-jsonschema with MIT License 7 votes vote down vote up
def test_range_marshmallow_3():
    class TestSchema(Schema):
        foo = fields.Integer(
            validate=Range(min=1, min_inclusive=False, max=3, max_inclusive=False)
        )
        bar = fields.Integer(validate=Range(min=2, max=4))

    schema = TestSchema()

    dumped = validate_and_dump(schema)

    props = dumped["definitions"]["TestSchema"]["properties"]
    assert props["foo"]["exclusiveMinimum"] == 1
    assert props["foo"]["exclusiveMaximum"] == 3
    assert props["bar"]["minimum"] == 2
    assert props["bar"]["maximum"] == 4 
Example #2
Source File: test_marshmallow.py    From pydantic with MIT License 6 votes vote down vote up
def __init__(self, allow_extra):
        class LocationSchema(Schema):
            latitude = fields.Float(allow_none=True)
            longitude = fields.Float(allow_none=True)

        class SkillSchema(Schema):
            subject = fields.Str(required=True)
            subject_id = fields.Integer(required=True)
            category = fields.Str(required=True)
            qual_level = fields.Str(required=True)
            qual_level_id = fields.Integer(required=True)
            qual_level_ranking = fields.Float(default=0)

        class Model(Schema):
            id = fields.Integer(required=True)
            client_name = fields.Str(validate=validate.Length(max=255), required=True)
            sort_index = fields.Float(required=True)
            # client_email = fields.Email()
            client_phone = fields.Str(validate=validate.Length(max=255), allow_none=True)

            location = fields.Nested(LocationSchema)

            contractor = fields.Integer(validate=validate.Range(min=0), allow_none=True)
            upstream_http_referrer = fields.Str(validate=validate.Length(max=1023), allow_none=True)
            grecaptcha_response = fields.Str(validate=validate.Length(min=20, max=1000), required=True)
            last_updated = fields.DateTime(allow_none=True)
            skills = fields.Nested(SkillSchema, many=True)

        self.allow_extra = allow_extra  # unused
        self.schema = Model() 
Example #3
Source File: base.py    From github-stats with MIT License 5 votes vote down vote up
def get_dbs(cls, query=None, ancestor=None, order=None, limit=None, cursor=None, **kwargs):
    args = parser.parse({
      'cursor': wf.Str(missing=None),
      'limit': wf.Int(missing=None, validate=validate.Range(min=-1)),
      'order': wf.Str(missing=None),
    })
    return util.get_dbs(
      query or cls.query(ancestor=ancestor),
      limit=limit or args['limit'],
      cursor=cursor or args['cursor'],
      order=order or args['order'],
      **kwargs
    ) 
Example #4
Source File: test_validation.py    From marshmallow-jsonschema with MIT License 5 votes vote down vote up
def test_range_marshmallow_2():
    class TestSchema(Schema):
        foo = fields.Integer(validate=Range(min=1, max=3))

    schema = TestSchema()

    dumped = validate_and_dump(schema)

    props = dumped["definitions"]["TestSchema"]["properties"]
    assert props["foo"]["minimum"] == 1
    assert props["foo"]["maximum"] == 3 
Example #5
Source File: test_validation.py    From marshmallow-jsonschema with MIT License 5 votes vote down vote up
def test_range_no_min_or_max():
    class SchemaNoMin(Schema):
        foo = fields.Integer(validate=validate.Range(max=4))

    class SchemaNoMax(Schema):
        foo = fields.Integer(validate=validate.Range(min=0))

    schema1 = SchemaNoMin()
    schema2 = SchemaNoMax()

    dumped1 = validate_and_dump(schema1)
    dumped2 = validate_and_dump(schema2)
    assert dumped1["definitions"]["SchemaNoMin"]["properties"]["foo"]["maximum"] == 4
    assert dumped2["definitions"]["SchemaNoMax"]["properties"]["foo"]["minimum"] == 0 
Example #6
Source File: test_validation.py    From marshmallow-jsonschema with MIT License 5 votes vote down vote up
def test_range_non_number_error():
    class TestSchema(Schema):
        foo = fields.String(validate=validate.Range(max=4))

    schema = TestSchema()

    json_schema = JSONSchema()

    with pytest.raises(UnsupportedValueError):
        json_schema.dump(schema) 
Example #7
Source File: test_validation.py    From marshmallow-jsonschema with MIT License 5 votes vote down vote up
def test_custom_validator():
    class TestValidator(validate.Range):
        _jsonschema_base_validator_class = validate.Range

    class TestSchema(Schema):
        test_field = fields.Int(validate=TestValidator(min=1, max=10))

    schema = TestSchema()

    dumped = validate_and_dump(schema)

    props = dumped["definitions"]["TestSchema"]["properties"]
    assert props["test_field"]["minimum"] == 1
    assert props["test_field"]["maximum"] == 10 
Example #8
Source File: params.py    From marshmallow-mongoengine with MIT License 5 votes vote down vote up
def __init__(self, field_me):
        super(SizeParam, self).__init__()
        # Add a length validator for max_length/min_length
        maxmin_args = {}
        if hasattr(field_me, 'max_value'):
            maxmin_args['max'] = field_me.max_value
        if hasattr(field_me, 'min_value'):
            maxmin_args['min'] = field_me.min_value
        self.field_kwargs['validate'].append(validate.Range(**maxmin_args)) 
Example #9
Source File: test_marshmallow_mongoengine.py    From marshmallow-mongoengine with MIT License 5 votes vote down vote up
def test_length_validator_set(self, models):
        fields_ = fields_for_model(models.Student)
        validator = contains_validator(fields_['full_name'], validate.Length)
        assert validator
        assert validator.max == 255
        validator = contains_validator(fields_['email'], validate.Length)
        assert validator
        assert validator.max == 100
        validator = contains_validator(fields_['profile_uri'], validate.Length)
        assert validator
        assert validator.max == 200
        validator = contains_validator(fields_['age'], validate.Range)
        assert validator
        assert validator.max == 99
        assert validator.min == 10 
Example #10
Source File: test_filtering.py    From flask-resty with MIT License 5 votes vote down vote up
def schemas():
    class WidgetSchema(Schema):
        id = fields.Integer(as_string=True)
        color = fields.String()
        size = fields.Integer(validate=validate.Range(min=1))

    return {"widget": WidgetSchema()} 
Example #11
Source File: test_pagination.py    From flask-resty with MIT License 5 votes vote down vote up
def schemas():
    class WidgetSchema(Schema):
        id = fields.Integer(as_string=True)
        size = fields.Integer()

    class WidgetValidateSchema(WidgetSchema):
        size = fields.Integer(validate=validate.Range(max=1))

    return {
        "widget": WidgetSchema(),
        "widget_validate": WidgetValidateSchema(),
    } 
Example #12
Source File: custom_fields.py    From FlowKit with Mozilla Public License 2.0 5 votes vote down vote up
def __init__(self, **kwargs):
        super().__init__(
            fields.Float(validate=Range(min=-1.0, max=1.0)),
            validate=Length(equal=24),
            **kwargs,
        ) 
Example #13
Source File: custom_fields.py    From FlowKit with Mozilla Public License 2.0 5 votes vote down vote up
def __init__(self, **kwargs):
        days_of_week = [
            "monday",
            "tuesday",
            "wednesday",
            "thursday",
            "friday",
            "saturday",
            "sunday",
        ]
        super().__init__(
            keys=fields.String(validate=OneOf(days_of_week)),
            values=fields.Float(validate=Range(min=-1.0, max=1.0)),
            **kwargs,
        )