Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema with dynamic keys #37

Open
exAspArk opened this issue Jan 3, 2017 · 10 comments
Labels
Milestone

Comments

@exAspArk
Copy link

@exAspArk exAspArk commented Jan 3, 2017

Hi,

Are there any options to describe schema with dynamic keys?

Let's say I have a hash where keys are string numbers:

{
  foo: {
    '1' => { name: 'bar1' },
    '2' => { name: 'bar2' },
     ...
    '100' => { name: 'bar100' }
  }
}

For now I can write a schema by using some limited predefined keys:

Dry::Validation.Schema do
  required(:foo).schema do
    ('1'..'100').each do |position|
      optional(position).schema do
        required(:name).filled(:str?)
      end
    end
  end
end

But this approach is not that flexible in case if I need to accept keys which may be any number.

Is using imperative custom validation blocks the only option to write flexible validation in this case?

BarSchema = Dry::Validation.Schema do
  required(:name).filled(:str?)
end

schema = Dry::Validation.Schema do
  required(:foo).filled

  configure do
    def self.messages
      # How can I get detailed errors here from BarSchema?
      super.merge(en: { errors: { valid_foo: 'foo is not valid' } })
    end
  end

  validate(valid_foo: :foo) do |key_values|
    key_values.all? do |k, v|
      k ~= /\A\d+\z/ && BarSchema.call(v).success?
    end
  end
end
@exAspArk

This comment has been minimized.

Copy link
Author

@exAspArk exAspArk commented Jan 3, 2017

Not sure, but maybe allowing to write some custom key matchers would be useful in this case. For example:

REGEX = /\A\d+\z/

Dry::Validation.Schema do
  matcher(:string_number) { |key| key ~= REGEX }

  required(:foo).schema do
    match(:string_number).schema do
      required(:name).filled(:str?)
    end
  end
end

Or something like:

Dry::Validation.Schema do
  required(:foo).schema do
    optional(REGEX).schema do
      required(:name).filled(:str?)
    end
  end
end
@solnic

This comment has been minimized.

Copy link
Member

@solnic solnic commented Jan 9, 2017

This feature is planned for 1.0.0

@marcohelmerich

This comment has been minimized.

Copy link

@marcohelmerich marcohelmerich commented Mar 14, 2017

Hi, we are just thinking about using dry-validate in our project. Something custom written is in place right now. We face the same problem and have dynamic keys at some places. Any idea when Version 1.0.0 and this feature will be ready?

@ndemianc

This comment has been minimized.

Copy link

@ndemianc ndemianc commented Sep 7, 2017

👍 For this feature

@jphastings

This comment has been minimized.

Copy link

@jphastings jphastings commented Oct 6, 2017

Is there any likelihood of this feature being selected for development soon?

@solnic

This comment has been minimized.

Copy link
Member

@solnic solnic commented Oct 7, 2017

@jphastings we want to support this in 1.0.0, I hope to start working on it next month.

@kkbaranski

This comment has been minimized.

Copy link

@kkbaranski kkbaranski commented Feb 19, 2018

@solnic any update on this?

@solnic

This comment has been minimized.

Copy link
Member

@solnic solnic commented Feb 19, 2018

@kbredemeier it'll be supported in dry-validation 1.0.0, as mentioned, it'll be done later this year, hopefully before the summer

@anthonydmitriyev

This comment has been minimized.

Copy link

@anthonydmitriyev anthonydmitriyev commented Jan 3, 2019

@solnic any news here?

@solnic

This comment has been minimized.

Copy link
Member

@solnic solnic commented Jan 4, 2019

@anthonydmitriyev unfortunately, I'm still working on dry-schema. It'll be ready when it's ready. Stay tuned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
7 participants
You can’t perform that action at this time.