jinja2schema
jinja2schema copied to clipboard
How to use with custom filter?
How to use jinja2schema with custom filter?
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)))
How can I ignore all custom filters?
How can we ignore all custom filters ?
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
Thanks, I will check..
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?