Adzz / data_schema

Declarative schemas for data transformations.
Apache License 2.0
85 stars 9 forks source link

Feature: empty_values option #33

Closed jekku closed 2 years ago

jekku commented 2 years ago

What's here?

defmodule Wallet do
  import DataSchema, only: [data_schema: 1]

  data_schema(
    field:
    {:account_number, "account_number", &DataSchemaTest.to_stringg/1,
     [optional?: false, empty_values: ["", nil, :undefined]]},
  )
end

Declaring Wallets with an account number, among any of ["", nil, :undefined] will cause an error.

iex(1)> DataSchema.to_struct(%{account_number: :undefined}, Wallet)
 {:error,
  %DataSchema.Errors{
    errors: [
      account_number: "Field was marked as not null but was found to be null."
    ]
  }}

Rundown of changes

ulissesalmeida commented 2 years ago

@Adzz should/can we change the null error message as well?

From

Field was marked as not null but was found to be null.

To be something like

Field was marked as not optional but was found to be #{inspect(value_found)}.
jekku commented 2 years ago

The error type should change as well after the draft.

It's currently:

%DataSchema.Errors.null_error{}

I imagine it to be something along the lines of:

%DataSchema.Errors.empty_required_value_error{}
Adzz commented 2 years ago

should/can we change the null error message as well?

The error type should change as well

Yes please! ❤️

Adzz commented 2 years ago

Small non blocking comment but great work overall, thanks!