jinja2schema icon indicating copy to clipboard operation
jinja2schema copied to clipboard

How to use with custom filter?

Open NamPNQ opened this issue 9 years ago • 6 comments

How to use jinja2schema with custom filter?

NamPNQ avatar Apr 27 '15 11:04 NamPNQ

This should work:

from jinja2 import Environment
from jinja2schema import infer_from_ast, parse, to_json_schema

# Note: You can also use extensions by passing e.g.
# `extensions=['jinja2.ext.with_']` to the `Environment()` constructor.
environment = Environment()
environment.filters["my_filter"] = my_filter
print(infer_from_ast(parse(template, environment)))

whitelynx avatar Nov 11 '16 19:11 whitelynx

How can I ignore all custom filters?

abhishek-gamooga avatar Jan 30 '17 14:01 abhishek-gamooga

How can we ignore all custom filters ?

dek-odoo avatar May 21 '17 05:05 dek-odoo

Custom filters are rejected during the filter check process in visitors/expr.py in visit_filter(). You can force an unknown filter to be considered as returning a string by modifying the last step : Original code on line 529:

else:
        raise InvalidExpression(ast, 'unknown filter')

Replace with :

else:
        ctx.meet(Scalar(), ast)
        node_struct = Scalar.from_ast(ast.node, order_nr=config.ORDER_OBJECT.get_next())
        return_struct_cls = String

bpil avatar Aug 13 '17 15:08 bpil

Thanks, I will check..

dek-odoo avatar Aug 17 '17 05:08 dek-odoo

Just stumbled upon this. Would it be possible to add an optional argument to the parse method? A dict that contains filtername/(type|callback func) pairs which can be used for lookup?

Or at least an 'ignore_unknown_filters' option?

makkus avatar Oct 07 '19 09:10 makkus