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

Support non-string enum validators ... #944

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jaynetics
Copy link

... both for custom validators and self-describing properties.

Before:

expect(pm_schema).to have_field(:num_tails, 'number', {:description => "Number of tails", :enum => [0, 1]})
# => expected param 'num_tails' to have type 'number' (got 'string')

expect_param_def("get", "/users/by_department_number", "department", "type", "integer")
# =>        expected: "integer"
# =>            got: "string"

After:

tests pass.

This PR also adds support for subclassing of validators.

@@ -25,8 +25,11 @@ def inspect
end

def self.inherited(subclass)
BaseValidator.validators.unshift(subclass)
Copy link
Author

Choose a reason for hiding this comment

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

Previously when subclassing a concrete validator, e.g. EnumValidator, this method would init the @validators ivar on EnumValidator and insert the new subclass there.

By explicitly referencing BaseValidator, the new class is added to the correct list.

This change shouldn't affect backwards compatibility. Worst case if anyone subclassed existing validators, they probably did BaseValidator.inherited(my_validator) or so, in which case this fix would cause my_validator to appear in the list twice.

expect_param_def("get", "/users/in_departments", "departments", "in", "query")
expect_array_param_def("get", "/users/in_departments", "departments",
%w[finance operations sales marketing HR])

expect_tags_def("get", "/twitter_example/{id}/followers", %w[twitter_example following index search])

expect_response_params_def("get", "/pets/{id}/as_properties", 200, "pet_name", "example", "mypet")
Copy link
Author

Choose a reason for hiding this comment

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

this added line is not related to this PR. i've just copied it from the other changed example above because it looks as if the two examples were supposed to mirror each other.

... both for custom validators and self-describing properties.

Before:

```ruby
expect(pm_schema).to have_field(:num_tails, 'number', {:description => "Number of tails", :enum => [0, 1]})
# => expected param 'num_tails' to have type 'number' (got 'string')

expect_param_def("get", "/users/by_department_number", "department", "type", "integer")
# =>        expected: "integer"
# =>            got: "string"
```

After:

tests pass.

This PR also adds support for subclassing of validators.
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