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

Add Evaluator#schema_error? and Evaluator#rule_error? #617

Merged
merged 6 commits into from
Feb 11, 2020

Conversation

waiting-for-dev
Copy link
Member

This tries to implement what has been discussed in #615

@flash-gordon @solnic please, take a look at the WIP code... the spec is failing because #error?(:email) returns false, even when it has an error added in the first rule. It's because Evaluator#error? delegates to Result#error?, which in turn delegates to Dry::Schema::Result#error?. Is it intended behaviour? I mean, we are looking just to the errors set in the schema phase and ignoring rules.

@waiting-for-dev waiting-for-dev changed the title Set default value for Evaluator#error? [WIP] Set default value for Evaluator#error? Jan 20, 2020
@solnic
Copy link
Member

solnic commented Jan 20, 2020

It's because Evaluator#error? delegates to Result#error?, which in turn delegates to Dry::Schema::Result#error?. Is it intended behaviour? I mean, we are looking just to the errors set in the schema phase and ignoring rules.

Ah, yes it is intended. I forgot this method is used for checking schema errors :( Not sure what to do with this. Maybe leave it as-is for now and introduce rule_error? instead. @flash-gordon ??

@flash-gordon
Copy link
Member

@solnic then rule_error? should only look up rule errors but it's not clear whether it should a single-rule check or all-rules check. I would say the name suggests it's current-rule-only.

@waiting-for-dev
Copy link
Member Author

I think that current name is misleading... what do you think about adding for now #schema_error? and #rule_error?, and adding a deprecation warning for #error?? I think the latest should perform both checks in a future dry-schema release...

@waiting-for-dev
Copy link
Member Author

In order to keep a thin interface another option is to overload #error? method to accept a keyword argument from:, expecting it to take :schema, :rule or :all as values. To keep backward compatibility it should default to :schema, but I'd change it to :all in 2.0

@solnic
Copy link
Member

solnic commented Jan 21, 2020

@waiting-for-dev I was going to suggest rule_error? + schema_error? too. I think it's fine that rule_error? would check all-the-rules. We can always extend it later on to be able to target a specific rule

@flash-gordon
Copy link
Member

I'm for it

@waiting-for-dev
Copy link
Member Author

waiting-for-dev commented Jan 22, 2020

Hmm not sure I'm following here... Currently, #error? accepts a path as argument, so it's already capable of targeting a single key. Should #schema_error? be an alias of #error?. It'd make sense to me. Or are you talking about checking all keys at once? The same applies to #rule_error?. Why not accept a path as argument from the beginning?

@solnic
Copy link
Member

solnic commented Jan 23, 2020

Should #schema_error? be an alias of #error?

Yes, and #error? should be deprecated and removed in 2.0.0.

The same applies to #rule_error?. Why not accept a path as argument from the beginning?

My understanding is that we want to check if a rule failed, doesn't matter under which key, because a rule can fail due to multiple reasons.

@flash-gordon have you considered that ☝️ ?

@flash-gordon
Copy link
Member

I don't think having path argument for rule_error? would be particularly useful. This is based on my experience. If you want to check if other rules failed you likely want to do it explicitly. For that, we need a means of nesting rules or something like this. If you want to check failures in the current rule then it's more likely you don't care about paths. This is what I learned from my experience. The main argument for my reasoning is adding new rules should not affect existing ones.

@waiting-for-dev
Copy link
Member Author

Hey @solnic @flash-gordon you can check it now

@waiting-for-dev waiting-for-dev changed the title [WIP] Set default value for Evaluator#error? Add Evaluator#schema_error? and Evaluator#rule_error? Feb 1, 2020
lib/dry/validation/failures.rb Outdated Show resolved Hide resolved
```

In complex rules you may be interested to know whether the current rule already
had an error. For that, you can use `#rule_error?`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't use hard-wrapping in docs, it can actually screw up MD formatting AND it makes writing docs really annoying

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apologies... I fixed it 😅

Copy link
Member

@solnic solnic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, approved but with a minor doc-related comment 😄

@solnic solnic merged commit 3d1421b into master Feb 11, 2020
@solnic solnic deleted the rule_error_default branch February 11, 2020 13:08
@flash-gordon
Copy link
Member

Thank you @waiting-for-dev!

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.

3 participants