Oasis.Plug.RequestValidator (oasis v0.7.0)

Copy Markdown View Source

A plug to convert types and validate the HTTP request parameters by the schemas of the OpenAPI definition.

The schema options can be found in the generated pre- plug handler file, the full list:

  • :query_schema
  • :header_schema
  • :cookie_schema
  • :body_schema

All of these options are fully map and generated from the corresponding definition of the OpenAPI Specification.

When the query parameters are verified by the validation of :query_schema, the coverted types of query parameters are reserved in :query_params and :params field of the Plug.Conn.

When the header parameters are verified by the validation of :header_schema, the converted types of header parameters are reserved in :req_headers field of the Plug.Conn.

When the cookie parameters are verified by the validation of :cookie_schema, the coverted types of cookie parameters are reserved in :req_cookies field of the Plug.Conn.

When the request body is verified by the validation of :body_schema, the coverted types of request body are reserved in :body_params and :params field of the Plug.Conn.

Primitive JSON bodies

Oasis follows Plug's default nest_all_json: false behavior: Plug wraps non-object JSON roots in a single _json key and leaves object roots as direct maps. That representation is otherwise indistinguishable from a literal JSON object whose only property is named _json, so Oasis only unwraps it when raw-body provenance proves that the wire value was not an object. Generated routers configure the required reader automatically. Handwritten pipelines that accept JSON bodies must configure Plug.Parsers in the same way and keep the default nest_all_json: false setting:

plug Plug.Parsers,
  parsers: [:json],
  pass: ["*/*"],
  json_decoder: Jason,
  body_reader: {Oasis.CacheRawBodyReader, :read_body, []}

plug Oasis.Plug.RequestValidator, body_schema: body_schema

Without that provenance, Oasis deliberately keeps the _json map intact and fails closed rather than guessing from the schema and potentially accepting a literal object as a primitive value. An empty parsed map is also ambiguous—it may represent either an absent body or the JSON object {}—so Oasis returns an actionable 415 unless framing headers prove that bytes were present. After successful validation, primitive roots remain available as conn.body_params["_json"] so Plug's body_params and params fields stay map-shaped.

Summary

Functions

Callback implementation for Plug.call/2.

Callback implementation for Plug.init/1.

Functions

call(conn, opts)

Callback implementation for Plug.call/2.

init(opts)

Callback implementation for Plug.init/1.