Search code examples
perlmojolicious

How to validate placeholders?


The Placeholders are available in Mojolicious.

We can access them via $c->param( 'placeholder_name' ) as other input data from user $c->param( 'post_or_query_param' )

But when we access parameters after validation we use:

$c->validation->param( 'post_or_query_param' )

The undef is returned if post_or_query_param did not pass validation.

We can validate placeholders partially via Restrective placeholders but still access them as $c->param( 'placeholder_name' ) (because placeholder is not available to validation object)

This often lead programmer to think that in this place of code not validated data is used because validated data is available as: $v->param( 'xxx' )

The placeholders is same input from user as query or post data.

How to make placeholders available to validation object? and access user's input in same way as other validated data:

$v->param( 'validated parameter name' );

Solution

  • Usually I use OpenAPI plugin and describe API using this format. Placeholder parameters also will be validated through schema (just like Query/JSON/Form/XML parameters).

    OpenAPI validator is based on JSON Schema Validator and is rich. Supports min, max, patterns, enums, deep structures etc.

    Read article about Mojolicious and OpenAPI.

    Here is example of validation schema and controller.