williamgueiros / Brcpfcnpj

Validação,Formatação e Gerador Cpf/Cnpj em Elixir
MIT License
69 stars 17 forks source link

Add support for Ecto changesets #3

Closed tiagoengel closed 8 years ago

tiagoengel commented 8 years ago

I was thinking on adding some methods to add support for the Ecto changeset api. Would look something like this:

model
    |> cast(params, @required_fields, @optional_fields)
    |> validate_cnpj(:cnpj, message: "Cnpj inválido')

What do you think?

williamgueiros commented 8 years ago

clean and beautiful. go ahead :+1:

williamgueiros commented 8 years ago

more one point, remember create test to this feature .

williamgueiros commented 8 years ago

@tiagoengel What do you thing, change validate_cnpj and validate_cpf to validate_cpfcnpj and using (:cnpj, :cpf) to select the correct validation ?

tiagoengel commented 8 years ago

Maybe internally we could do that, but I prefer the API with the two functions, 'cause a function call like this would be a little bit confusing.

validate_cpfcnpf(:cnpj, :cnpj)
williamgueiros commented 8 years ago

ok no problem. i close the issue.

williamgueiros commented 8 years ago

:+1:

2 functions is good, i don't understend why using atom

validate_cnpj(:cnpj, message: "Cnpj inválido')
validate_cpf(:cpf, message: "Cpf inválido')

instead

validate_cnpj(message: "Cnpj inválido')
validate_cpf(message: "Cpf inválido')

is a Ecto limitation ?

tiagoengel commented 8 years ago

:cnpj/:cpf in this case are field names. For instance, if I have a model definition like this:

defmodule Company do

  schema "companies" do
    field :name, :string
    field :numero_cnpj, :string
  end
end

I would have to call the function this way:

validate_cnpj(:numero_cnpj, message: "Cnpj inválido')
williamgueiros commented 8 years ago

:heart: :green_heart: :blue_heart: