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

Feature: Opaque Validator #1

Open
DmitryBochkarev opened this issue Jun 23, 2020 · 0 comments
Open

Feature: Opaque Validator #1

DmitryBochkarev opened this issue Jun 23, 2020 · 0 comments

Comments

@DmitryBochkarev
Copy link
Owner

DmitryBochkarev commented Jun 23, 2020

In some situations, simple error messages are uninformative and can sometimes lead to data leaks. And it would be nice to give another message instead.

An example of a leak and unnecessary noise

IncludedIn = Struct.new(:value) do
  def included_in?(collection)
  	collection.include?(value)
  end

  def inspect
    value.inspect
  end
end

existing_projects =
  FlexValidations::And.new(
	positive_integer,
	FlexValidations::Chain.new(
      FlexValidations::Decorate.new(IncludedIn),
	  FlexValidations::Predicate.new(:included_in?, Project.all.map(&:id))
    )
  )

puts existing_projects.validate(3)

# chain of validation for 3 failed:
#   1. decorated 3 now 3;
#   2. 3.included_in?([1, 2]) failed.

As I can see it should be:

existing_projects =
  FlexValidations::Opaque.new(
	  FlexValidations::And.new(
		positive_integer,
		FlexValidations::Chain.new(
	      FlexValidations::Decorate.new(IncludedIn),
		  FlexValidations::Predicate.new(:overlap?, Project.all.map(&:id))
	    )
	  ),
	  fail: "Invalid project"
  )

puts existing_projects.validate(3)

# Invalid project
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

No branches or pull requests

1 participant