Skip to content

Unknown keys handling (tests to be added)#31

Open
rakesh-voiger wants to merge 3 commits intoozziexsh:mainfrom
voigerio:unknown
Open

Unknown keys handling (tests to be added)#31
rakesh-voiger wants to merge 3 commits intoozziexsh:mainfrom
voigerio:unknown

Conversation

@rakesh-voiger
Copy link

# deps
{:validate, git: "https://github.com/voigerio/validate.git", tag: "unknown"}
  • unknown option is added for map types, which can be one of [:remove, :reject, :allow]
  • this unknown defaults to :remove (current behaviour)
  • For us, we need :reject most of the times.
    Rest of the usage is same as the base, check the README for more details.
input = %{
  "name" => "Jane Doe",
  "colors" => ["red", "green"],
  "address" => %{
    "line0" => "sunga",
    "line1" => "123 Fake St",
    "city" => "Saskatoon",
  },
  "bad key" => "value",
  "friends" => [
    %{"name" => "John", "email" => "john@example.com"},
    %{"name" => "Michelle", "email" => "michelle@example.com", "sunda" => 9}
  ]
}

rules = [
  required: true,
  type: :map,
  unknown: :reject,
  map: %{
    "name" => [required: true, type: :string],
    "colors" => [
      required: true,
      type: :list,
      list: [required: true, type: :string, in: ~w[blue orange red green purple]]
    ],
    "address" => [
      required: true,
      type: :map,
      unknown: :reject,
      map: %{
        "line1" => [required: true, type: :string],
        "city" => [required: true, type: :string],
      }
    ],
    "friends" => [
      required: true,
      type: :list,
      list: [
        required: true,
        type: :map,
        map: %{
          "name" => [required: true, type: :string],
          "email" => [required: true, type: :string]
        }
      ]
    ]
  }
]

result = Validate.validate(input, rules)
IO.inspect(result)

@rakesh-voiger
Copy link
Author

Fix for #29

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant