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

Make it possible to configure new Hash constructor type #180

Closed
Kukunin opened this issue Mar 7, 2017 · 4 comments
Closed

Make it possible to configure new Hash constructor type #180

Kukunin opened this issue Mar 7, 2017 · 4 comments
Labels

Comments

@Kukunin
Copy link

Kukunin commented Mar 7, 2017

Currently Types::Hash has 6 pre-defined constructor types, with own properties.

But they aren't enough for all purposes, e.g. there is an issue dry-rb/dry-struct#32 for dry-struct (created by me).

Now, I've got another case, where I want hash to allow defined keys absence, but deny any extra keys.

Instead of defining new 7th constructor type with confusing name, maybe it's good to configure contructor types alone?

Something like:

SpecialHash = Types::Hash.build_constructor(
  accept_invalid_types: false, 
  allow_value_absence: true, 
  allow_extra_keys: false
)
MyType = SpecialHash.schema(name: Types::String, age: Types::Coercible::Int)
@solnic
Copy link
Member

solnic commented Mar 7, 2017

We're definitely not going to add more hash schema types, or an API for configuring them, especially if it's related to validating input, as data validation is meant to by handled by dry-validation. Schema types that we have were added to cover a couple, very specific use-cases, that are related to building structs using data from specific sources (like a database, or JSON).

From what I've seen, people keep asking about more relaxed approach to setting defaults, and it's always related to testing, it seems like that's your situation as well, right?

@Kukunin
Copy link
Author

Kukunin commented Mar 8, 2017

In my case it is a need in Domain layer.

I have an Value Object to represent answers on pre-defined questions. There are 12 of such questions. All answers are optional. All answers are true/false.

So I decided, that Hash with question => answer works here.

I don't want an array of question, because there may be duplicates, and it's more complicated.
I don't want to make API client to enumerate all 12 questions explicitly, if there is only 1 answer available (so, no strict and permissive constructor).
I don't want to allow any unexpected answer to be present in the hash (so, no schema constructor)
Because there is HUGE difference between nil and false value for answer, I don't want to keep questions without answers in that hash (so, no dry_struct)

Currently, the most representative way is Types::Hash.schema with custom predicate to deny unexpected keys.

BTW, feel free to close this issue

@solnic solnic added the question label Mar 8, 2017
@Kukunin
Copy link
Author

Kukunin commented Mar 8, 2017

btw, #113 would work here as well

Hash.keys(Types::Strict::Symbol.constrained(included_in: [whitelist]))

@flash-gordon
Copy link
Member

We implemented support for flexible yet simple schemas. #113 remains open for now but I have a feeling we'll implement it as well before 1.0 is out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants