What is ndto
?
ndto
is an Erlang library for generating DTO (Data Transfer Object) validation modules from schemas.
Motivation
Validating incoming data is a critical step to ensure the integrity, consistency, and security of your application’s data flow. However, writing custom validation logic for each DTO can quickly become a time-consuming and error-prone task.
With ndto
, you can define validation schemas that describe the structure and constraints of your data. These schemas are then used to automatically generate validation modules, reducing development time and avoiding human-induced errors in the validation step.
A basic example
- Define an
ndto
schema.
Schema = #{
<<"type">> => <<"string">>,
<<"minLength">> => 8,
<<"pattern">> => <<"^hello">>
}.
- Generate a module using the
ndto:generate/2
function.
DTO = ndto:generate(string_schema, Schema).
- Load the generated module on the fly.
ok = ndto:load(DTO).
- Call the
is_valid/1
function from the generated module to validate your data.
true = string_schema:is_valid(<<"hello world">>).
false = string_schema:is_valid(<<"hello">>).
false = string_schema:is_valid(<<"hi world">>).
ndto
schema language
ndto
defines a custom data type (i.e., ndto:schema()
) to represent schemas.
%%% ndto.erl
-type schema() ::
empty_schema()
| universal_schema()
| ref_schema()
| enum_schema()
| boolean_schema()
| integer_schema()
| number_schema()
| string_schema()
| array_schema()
| object_schema()
| union_schema()
| intersection_schema()
| complement_schema()
| symmetric_difference_schema().
The goal is to provide an abstraction that decouples the library functionality from a specific schema specification format. This enables developers to use ndto
to generate modules for schemas defined in the specification language that better fits their development environment by implementing an ndto_parser
.
Integration
While ndto
provides an interface for the generation and loading of validation modules in runtime, a rebar3
plugin for the generation of static validation modules in compile time has been also developed.
Links
ndto
repository: GitHub - nomasystems/ndto: ✅ An Erlang library for DTOs validation.ndto
docs: ndto v0.1.0 — Documentationrebar3_ndto
repository: GitHub - nomasystems/rebar3_ndto: A rebar3 plugin for the automatic generation of ndto modules.