Skip to content

Commit

Permalink
dataspec -> dataspecs
Browse files Browse the repository at this point in the history
  • Loading branch information
Giovanni Visciano committed Jul 23, 2021
1 parent 0dba5a4 commit edca624
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 140 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# dataspec
# dataspecs

![CI](https://github.com/visciang/dataspec/workflows/CI/badge.svg) [![Coverage Status](https://coveralls.io/repos/github/visciang/dataspec/badge.svg?branch=master)](https://coveralls.io/github/visciang/dataspec?branch=master)
![CI](https://github.com/visciang/dataspecs/workflows/CI/badge.svg) [![Coverage Status](https://coveralls.io/repos/github/visciang/dataspecs/badge.svg?branch=master)](https://coveralls.io/github/visciang/dataspecs?branch=master)

Typespec based data loader and validator (inspired by [forma](https://github.com/soundtrackyourbrand/forma)).

DataSpec **validate and load** elixir data into a more structured form
DataSpecs **validate and load** elixir data into a more structured form
by trying to map it to conform to a **typespec**. It support most typespec
specification: **basic** types, **literal** types, **built-in** types, **union** type,
**parametrized** types, **maps**, **remote** types and **user defined** types.
Expand Down Expand Up @@ -44,7 +44,7 @@ defmodule Address do
}
end

DataSpec.load(%{
DataSpecs.load(%{
"name" => "Joe",
"surname" => "Smith",
"gender" => "male",
Expand All @@ -71,7 +71,7 @@ DataSpec.load(%{
# }
```

DataSpec tries to figure out how to translate its input to a typespec.
DataSpecs tries to figure out how to translate its input to a typespec.

Scalar types (such as booleans, integers, etc.) and some composite types (such as lists, plain maps), can be simply mapped one to one after validation without any additional transformation.

Expand All @@ -84,7 +84,7 @@ Refer to the library test suite for more examples.
```elixir
def deps do
[
{:dataspec, "~> xxx"}
{:dataspecs, "~> xxx"}
]
end
```
Expand Down Expand Up @@ -121,7 +121,7 @@ end
## Custom type loaders

In these cases you can pass a set of custom type loaders along as an optional argument
to the `DataSpec.load` function
to the `DataSpecs.load` function

```elixir
defmodule LogRow do
Expand All @@ -147,7 +147,7 @@ def custom_isodatetime_loader(value, _custom_type_loaders, []) do
end
end

DataSpec.load(
DataSpecs.load(
%{"log" => "An error occurred", "timestamp" => "2021-07-14 20:22:49.653077Z"},
%{{DateTime, :t, 0} => &custom_isodatetime_loader/3}
)
Expand Down Expand Up @@ -228,7 +228,7 @@ defmodule AStruct do
@type field :: String.t()

def custom_field_loader(value, custom_type_loaders, type_params_loaders) do
name = DataSpec.Loaders.binary(value, custom_type_loaders, type_params_loaders)
name = DataSpecs.Loaders.binary(value, custom_type_loaders, type_params_loaders)

if name == String.upcase(name) do
{:ok, name}
Expand All @@ -238,7 +238,7 @@ defmodule AStruct do
end
end

DataSpec.load(%{field: "AAA"}, {AStruct, :t}, %{{AStruct, :field, 0} => &AStruct.custom_field_loader/3})
DataSpecs.load(%{field: "AAA"}, {AStruct, :t}, %{{AStruct, :field, 0} => &AStruct.custom_field_loader/3})
# => %AStruct{field: "AAA"}
```

Expand Down
6 changes: 3 additions & 3 deletions lib/dataspec.ex → lib/dataspecs.ex
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
defmodule DataSpec do
defmodule DataSpecs do
@moduledoc File.read!("README.md")

use Application
alias DataSpec.Typespecs
alias DataSpecs.Typespecs

@type value() :: any()
@type reason :: [String.t() | reason()]
Expand All @@ -19,6 +19,6 @@ defmodule DataSpec do
end

def start(_type, _args) do
Supervisor.start_link([DataSpec.Cache], strategy: :one_for_one)
Supervisor.start_link([DataSpecs.Cache], strategy: :one_for_one)
end
end
2 changes: 1 addition & 1 deletion lib/dataspec/cache.ex → lib/dataspecs/cache.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule DataSpec.Cache do
defmodule DataSpecs.Cache do
@moduledoc false

use GenServer
Expand Down
2 changes: 1 addition & 1 deletion lib/dataspec/loaders.ex → lib/dataspecs/loaders.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule DataSpec.Loaders do
defmodule DataSpecs.Loaders do
@moduledoc false

def any(value, _custom_type_loaders, _type_params_loaders) do
Expand Down
6 changes: 3 additions & 3 deletions lib/dataspec/typespecs.ex → lib/dataspecs/typespecs.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule DataSpec.Typespecs do
defmodule DataSpecs.Typespecs do
@moduledoc false

alias DataSpec.{Cache, Loaders}
alias DataSpecs.{Cache, Loaders}

require Logger

Expand Down Expand Up @@ -346,7 +346,7 @@ defmodule DataSpec.Typespecs do
# erlang abstract type format:
# {:type, 48, :map,
# [
# {:type, 48, :map_field_exact, [{:atom, 0, :__struct__}, {:atom, 0, Test.DataSpec.SampleStructType}]},
# {:type, 48, :map_field_exact, [{:atom, 0, :__struct__}, {:atom, 0, Test.DataSpecs.SampleStructType}]},
# {:type, 48, :map_field_exact, [{:atom, 0, :f_1}, {:type, 49, :atom, []}]},
# {:type, 48, :map_field_exact, [{:atom, 0, :f_2}, {:type, 50, :integer, []}]}
# ]
Expand Down
14 changes: 7 additions & 7 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
defmodule DataSpec.Mixfile do
defmodule DataSpecs.Mixfile do
use Mix.Project

def project do
[
app: :dataspec,
name: "dataspec",
app: :dataspecs,
name: "dataspecs",
version: "0.0.1",
elixir: "~> 1.7",
elixirc_paths: elixirc_paths(Mix.env()),
Expand All @@ -14,7 +14,7 @@ defmodule DataSpec.Mixfile do
preferred_cli_env: preferred_cli_env(),
description: description(),
package: package(),
source_url: "https://github.com/visciang/dataspec"
source_url: "https://github.com/visciang/dataspecs"
]
end

Expand All @@ -24,18 +24,18 @@ defmodule DataSpec.Mixfile do

defp package do
[
name: "dataspec",
name: "dataspecs",
licenses: ["MIT"],
files: ["lib", "README.md", "LICENSE", "mix.exs"],
maintainers: ["Giovanni Visciano"],
links: %{"GitHub" => "https://github.com/visciang/dataspec"}
links: %{"GitHub" => "https://github.com/visciang/dataspecs"}
]
end

def application do
[
extra_applications: [:logger],
mod: {DataSpec, []}
mod: {DataSpecs, []}
]
end

Expand Down
Loading

0 comments on commit edca624

Please sign in to comment.