Pravda is a plug for phoenix to validate input and output against a provided spec. It also supports having multiple versions of the same spec loaded at one time, and the ability to migrate the input version to the current spec, and downgrade the output to the desired spec. It supports callbacks to provide custom error messages and extra logging.
Metrics are implemented via telemetry.
plug(Pravda, %{
spec_var: "spec-version",
all_paths_required: true,
error_callback: MyApp.Utils.PravdaErrorLogger,
migration_callback: MyApp.Utils.PravdaMigrations,
specs: Pravda.Loader.read_dir("deps/my_specs/suppored_releases"),
})Add the following to the deps block in mix.exs:
{:pravda, "~>0.6.0"}
Configuration can be specified in the opts argument to all Pravda
functions, by setting config values with e.g., Application.put_env,
or by a combination of the two.
The following configuration options are supported:
:namea namespace for all other options so you can have multiple instances of the plug. DefaultPravda.DefaultName:enableenable or disable the plug entirely. Defaulttrue.:specsList of files to use for validation,:spec_varName of the spec-var we are matching against. Default"spec-var":spec_var_placementthe location of the spec var. Options are:query, :path, :headerDefault:header:migration_callbackModule to be passed in that will be called when the spec is attempted to migrate Defaultnil:error_callbackModule to be passed in that will be called when there is an error in the input or output, Default:nil:custom_error_callbackModule to be passed in that will provide end-user with a custom error message, Default:nil:all_paths_requiredThe path passed into the plug must match one of its rules if true, Default::true:explain_errorprovide the reason validation failed, Default:true:validate_paramsvalidate input params, header, path, query, Default:true:validate_bodyvalidate input body from user, Default:true:validate_responsevalidate output response to the user, Default:true:allow_invalid_inputallow input to pass even if invalid, Default:false:allow_invalid_outputallow output to pass even if invalid, Default:false:migrate_inputmigrate input from current spec to latest input spec, Default:true:migrate_outputmigrate output from current spec to client requested spec, Default:true
:fallback_to_latestmigrate output to latest if true, otherwise fallback to oldest supported version, Default:true
Metrics are offered via the Telemetry library. The following metrics are emitted:
[:pravda, :request, :params, :valid]Successfully validated input parameters[:pravda, :request, :params, :invalid]Input parameters failed to validate[:pravda, :request, :body, :valid]Successfully validated body[:pravda, :request, :body, :invalid]Input body failed to validate[:pravda, :request, :response, :valid]Successfully validated output response[:pravda, :request, :response, :invalid]Output response failed to validate[:pravda, :request, :migrate, :up]Successfully migrated input spec up[:pravda, :request, :migrate, :down]Successfully migrated output spec down[:pravda, :request, :complete]Request made it through the plug[:pravda, :request, :start]Request made it into the plug
Thanks for considering contributing to this project, and to the free software ecosystem at large!
Interested in contributing a bug report? Terrific! Please open a GitHub issue and include as much detail as you can. If you have a solution, even better -- please open a pull request with a clear description and tests.
Have a feature idea? Excellent! Please open a GitHub issue for discussion.
Want to implement an issue that's been discussed? Fantastic! Please open a GitHub pull request and write a clear description of the patch. We'll merge your PR a lot sooner if it is well-documented and fully tested.
Copyright 2020, Matthew O'Gorman.
This software is released under the MIT License.
