def validate_extra_filter_expression(self, value): jexl = JEXL() # Add mock transforms for validation. See # http://normandy.readthedocs.io/en/latest/user/filter_expressions.html#transforms # for a list of what transforms we expect to be available. jexl.add_transform('date', lambda x: x) jexl.add_transform('stableSample', lambda x: x) jexl.add_transform('bucketSample', lambda x: x) jexl.add_transform('preferenceValue', lambda x: x) jexl.add_transform('preferenceIsUserSet', lambda x: x) jexl.add_transform('preferenceExists', lambda x: x) try: errors = list(jexl.validate(value)) except Exception as exc: # The JEXL parser can occasionally throw exceptions when # called to validate certain invalid inputs. # Catch them and at least indicate the field that failed, # even if we can't explain exactly what the problem was. # See https://github.com/mozilla/normandy/issues/1059. error = f'The JEXL parser failed to validate {value}' raise serializers.ValidationError([error]) if errors: raise serializers.ValidationError(errors) return value
def get_normandy_jexl(): global _cached_jexl if not _cached_jexl: _cached_jexl = JEXL() # Add mock transforms for validation. See # https://mozilla.github.io/normandy/user/filters.html#transforms # for a list of what transforms we expect to be available. transforms = [ "bucketSample", "date", "keys", "length", "mapToProperty", "preferenceExists", "preferenceIsUserSet", "preferenceValue", "regExpMatch", "stableSample", "versionCompare", ] for transform in transforms: _cached_jexl.add_transform(transform, lambda x: x) return _cached_jexl
def test_extract_transforms(expression, expected_transforms): jexl = JEXL() jexl.add_transform("transform1", lambda x: x) jexl.add_transform("transform2", lambda x: x) assert set( jexl.analyze( expression, functools.partial(ExtractTransformSubjectAnalyzer, transforms=["transform1"]), )) == set(expected_transforms)
def validate_extra_filter_expression(self, value): jexl = JEXL() # Add mock transforms for validation. See # http://normandy.readthedocs.io/en/latest/user/filter_expressions.html#transforms # for a list of what transforms we expect to be available. jexl.add_transform('date', lambda x: x) jexl.add_transform('stableSample', lambda x: x) jexl.add_transform('bucketSample', lambda x: x) errors = list(jexl.validate(value)) if errors: raise serializers.ValidationError(errors) return value
def validate_extra_filter_expression(self, value): if value: jexl = JEXL() # Add mock transforms for validation. See # https://mozilla.github.io/normandy/user/filters.html#transforms # for a list of what transforms we expect to be available. jexl.add_transform("date", lambda x: x) jexl.add_transform("stableSample", lambda x: x) jexl.add_transform("bucketSample", lambda x: x) jexl.add_transform("preferenceValue", lambda x: x) jexl.add_transform("preferenceIsUserSet", lambda x: x) jexl.add_transform("preferenceExists", lambda x: x) errors = list(jexl.validate(value)) if errors: raise serializers.ValidationError(errors) return value
def validate_extra_filter_expression(self, value): if value: jexl = JEXL() # Add mock transforms for validation. See # http://normandy.readthedocs.io/en/latest/user/filter_expressions.html#transforms # for a list of what transforms we expect to be available. jexl.add_transform("date", lambda x: x) jexl.add_transform("stableSample", lambda x: x) jexl.add_transform("bucketSample", lambda x: x) jexl.add_transform("preferenceValue", lambda x: x) jexl.add_transform("preferenceIsUserSet", lambda x: x) jexl.add_transform("preferenceExists", lambda x: x) errors = list(jexl.validate(value)) if errors: raise serializers.ValidationError(errors) return value
def to_jexl(self): built_expression = "(" + self.initial_data["expression"] + ")" jexl = JEXL() # Add mock transforms for validation. See # https://mozilla.github.io/normandy/user/filters.html#transforms # for a list of what transforms we expect to be available. jexl.add_transform("date", lambda x: x) jexl.add_transform("stableSample", lambda x: x) jexl.add_transform("bucketSample", lambda x: x) jexl.add_transform("preferenceValue", lambda x: x) jexl.add_transform("preferenceIsUserSet", lambda x: x) jexl.add_transform("preferenceExists", lambda x: x) errors = list(jexl.validate(built_expression)) if errors: raise serializers.ValidationError(errors) return built_expression