pub struct RequestValidationPlugin {
header_schema: Option<Validator>,
body_schema: Option<Validator>,
conditions: Option<Expr>,
rejected_code: u16,
rejected_msg: Option<Template>,
}Expand description
Validates context.request headers and body against compiled JSON
Schemas, and optionally evaluates boolean condition predicates on the
request. On failure the request is rejected through the denied port
with a JSON response using rejected_code.
Headers are validated as a single-value object (first value per header, names lowercased), matching the shape APISIX passes to its schema check. After a successful JSON-body validation the body is re-serialized from the parsed document, so the JSON that was validated is exactly the JSON the upstream receives (guards against JSON-interoperability smuggling).
Fields§
§header_schema: Option<Validator>§body_schema: Option<Validator>§conditions: Option<Expr>§rejected_code: u16§rejected_msg: Option<Template>Fixed message returned instead of the validator’s error description.
Supports {{namespace.path}} references (no legacy $var
interpolation — this field never supported it, so this sweep must not
start).
Implementations§
Source§impl RequestValidationPlugin
impl RequestValidationPlugin
Sourcepub fn from_config(config: &HashMap<String, Value>) -> Result<Self, String>
pub fn from_config(config: &HashMap<String, Value>) -> Result<Self, String>
Builds the plugin from node config.
Accepted keys:
header_schema(object): JSON Schema applied to the request headers, seen as{name: first_value}with lowercase names.body_schema(object): JSON Schema applied to the parsed request body (JSON, or urlencoded decoded to a flat object).conditions(array): a condition expression (seecrate::vars::Expr) — rules ANDed at top level, nestedAND/OR/NOTgroups, JSONPath body subjects. Evaluated after the schemas; failure rejects like a schema failure with message “request conditions not satisfied”.rejected_code(integer 200–599, default400): response status for rejected requests.rejected_msg(string): fixed message returned instead of the validator’s error description. Supports{{namespace.path}}references.
At least one of header_schema, body_schema, or conditions is
required; schemas are compiled here so malformed schemas fail at config
load.
type: request-validation
config:
rejected_code: 422
body_schema:
type: object
required: [name]
properties:
name: { type: string, minLength: 1 }Sourcefn reject(
&self,
ctx: Context,
detail: String,
) -> Result<PluginOutput, PluginExecutionError>
fn reject( &self, ctx: Context, detail: String, ) -> Result<PluginOutput, PluginExecutionError>
Writes the rejection onto the response and routes the context through
the node’s denied port.
Trait Implementations§
Source§impl Debug for RequestValidationPlugin
impl Debug for RequestValidationPlugin
Source§impl Plugin for RequestValidationPlugin
impl Plugin for RequestValidationPlugin
Source§fn plugin_type(&self) -> &str
fn plugin_type(&self) -> &str
Source§fn execute<'life0, 'async_trait>(
&'life0 self,
ctx: Context,
) -> Pin<Box<dyn Future<Output = Result<PluginOutput, PluginExecutionError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn execute<'life0, 'async_trait>(
&'life0 self,
ctx: Context,
) -> Pin<Box<dyn Future<Output = Result<PluginOutput, PluginExecutionError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Source§fn reads_response_body(&self) -> bool
fn reads_response_body(&self) -> bool
context.response.body. Read moreSource§fn cache_target(&self) -> Option<CacheTarget>
fn cache_target(&self) -> Option<CacheTarget>
proxy-cache half. Read moreAuto Trait Implementations§
impl !RefUnwindSafe for RequestValidationPlugin
impl !UnwindSafe for RequestValidationPlugin
impl Freeze for RequestValidationPlugin
impl Send for RequestValidationPlugin
impl Sync for RequestValidationPlugin
impl Unpin for RequestValidationPlugin
impl UnsafeUnpin for RequestValidationPlugin
Blanket Implementations§
§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more