You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>Add, remove or replace <ahref="../functions/">filter functions</a> by updating the <ahref="../api/#jsonpath.env.JSONPathEnvironment.function_extensions"><code>function_extensions</code></a> attribute of a <ahref="../api/#jsonpath.env.JSONPathEnvironment"><code>JSONPathEnvironment</code></a>. It is a regular Python dictionary mapping filter function names to any <ahref="https://docs.python.org/3/library/typing.html#typing.Callable">callable</a>, like a function or class with a <code>__call__</code> method.</p>
1076
+
<p>Add, remove or replace <ahref="../functions/">filter functions</a> by updating the <ahref="../api/#jsonpath.JSONPathEnvironment.function_extensions"><code>function_extensions</code></a> attribute of a <ahref="../api/#jsonpath.JSONPathEnvironment"><code>JSONPathEnvironment</code></a>. It is a regular Python dictionary mapping filter function names to any <ahref="https://docs.python.org/3/library/typing.html#typing.Callable">callable</a>, like a function or class with a <code>__call__</code> method.</p>
1077
1077
<h3id="type-system-for-function-expressions">Type System for Function Expressions</h3>
1078
1078
<p><ahref="https://datatracker.ietf.org/doc/html/rfc9535#name-type-system-for-function-ex">Section 2.4.1</a> of RFC 9535 defines a type system for function expressions and requires that we check that filter expressions are well-typed. With that in mind, you are encouraged to implement custom filter functions by extending <ahref="../api/#jsonpath.function_extensions.FilterFunction"><code>jsonpath.function_extensions.FilterFunction</code></a>, which forces you to be explicit about the <ahref="../api/#jsonpath.function_extensions.ExpressionType">types</a> of arguments the function extension accepts and the type of its return value.</p>
1079
1079
<divclass="admonition info">
1080
1080
<pclass="admonition-title">Info</p>
1081
-
<p><ahref="../api/#jsonpath.function_extensions.FilterFunction"><code>FilterFunction</code></a> was new in Python JSONPath version 0.10.0. Prior to that we did not enforce function expression well-typedness. To use any arbitrary <ahref="https://docs.python.org/3/library/typing.html#typing.Callable">callable</a> as a function extension - or if you don't want built-in filter functions to raise a <code>JSONPathTypeError</code> for function expressions that are not well-typed - set <ahref="../api/#jsonpath.env.JSONPathEnvironment.well_typed"><code>well_typed</code></a> to <code>False</code> when constructing a <ahref="../api/#jsonpath.env.JSONPathEnvironment"><code>JSONPathEnvironment</code></a>.</p>
1081
+
<p><ahref="../api/#jsonpath.function_extensions.FilterFunction"><code>FilterFunction</code></a> was new in Python JSONPath version 0.10.0. Prior to that we did not enforce function expression well-typedness. To use any arbitrary <ahref="https://docs.python.org/3/library/typing.html#typing.Callable">callable</a> as a function extension - or if you don't want built-in filter functions to raise a <code>JSONPathTypeError</code> for function expressions that are not well-typed - set <ahref="../api/#jsonpath.JSONPathEnvironment.well_typed"><code>well_typed</code></a> to <code>False</code> when constructing a <ahref="../api/#jsonpath.JSONPathEnvironment"><code>JSONPathEnvironment</code></a>.</p>
1082
1082
</div>
1083
1083
<h3id="example">Example</h3>
1084
1084
<p>As an example, we'll add a <code>min()</code> filter function, which will return the minimum of a sequence of values. If any of the values are not comparable, we'll return the special <code>undefined</code> value instead.</p>
<h3id="compile-time-validation">Compile Time Validation</h3>
1153
-
<p>Calls to <ahref="#type-system-for-function-expressions">type-aware</a> function extension are validated at JSONPath compile-time automatically. If <ahref="../api/#jsonpath.env.JSONPathEnvironment.well_typed"><code>well_typed</code></a> is set to <code>False</code> or a custom function extension does not inherit from <ahref="../api/#jsonpath.function_extensions.FilterFunction"><code>FilterFunction</code></a>, its arguments can be validated by implementing the function as a class with a <code>__call__</code> method, and a <code>validate</code> method. <code>validate</code> will be called after parsing the function, giving you the opportunity to inspect its arguments and raise a <code>JSONPathTypeError</code> should any arguments be unacceptable. If defined, <code>validate</code> must take a reference to the current environment, an argument list and the token pointing to the start of the function call.</p>
1153
+
<p>Calls to <ahref="#type-system-for-function-expressions">type-aware</a> function extension are validated at JSONPath compile-time automatically. If <ahref="../api/#jsonpath.JSONPathEnvironment.well_typed"><code>well_typed</code></a> is set to <code>False</code> or a custom function extension does not inherit from <ahref="../api/#jsonpath.function_extensions.FilterFunction"><code>FilterFunction</code></a>, its arguments can be validated by implementing the function as a class with a <code>__call__</code> method, and a <code>validate</code> method. <code>validate</code> will be called after parsing the function, giving you the opportunity to inspect its arguments and raise a <code>JSONPathTypeError</code> should any arguments be unacceptable. If defined, <code>validate</code> must take a reference to the current environment, an argument list and the token pointing to the start of the function call.</p>
0 commit comments