def string_to_boolean(value): validator = Boolean() value, error = validator.validate_or_error(value) if value is None: return False else: return value
def test_boolean(): validator = Boolean() value, error = validator.validate_or_error(True) assert value is True validator = Boolean() value, error = validator.validate_or_error(False) assert value is False validator = Boolean() value, error = validator.validate_or_error("True") assert value is True validator = Boolean() value, error = validator.validate_or_error(1) assert value is True validator = Boolean() value, error = validator.validate_or_error(None) assert error == ValidationError(text="May not be null.", code="null") validator = Boolean() value, error = validator.validate_or_error(2) assert error == ValidationError(text="Must be a boolean.", code="type") validator = Boolean() value, error = validator.validate_or_error([]) assert error == ValidationError(text="Must be a boolean.", code="type") validator = Boolean(allow_null=True) value, error = validator.validate_or_error(None) assert value is None assert error is None validator = Boolean(allow_null=True) value, error = validator.validate_or_error("") assert value is None assert error is None validator = Boolean() value, error = validator.validate_or_error("True", strict=True) assert error == ValidationError(text="Must be a boolean.", code="type")