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

Should be able to declare Apipie::prop with type array of strings #796

Open
notfelineit opened this issue Oct 19, 2022 · 4 comments
Open

Comments

@notfelineit
Copy link

I'm not able to declare an Apipie::prop(:addresses, 'array', array_of: 'string', description: 'Addresses for the user') as a prop in an embedded response descriptions for my class.

From the docs, it doesn't look like this is possible either. It looks like I can only declare an array of objects.

How can I add an array of strings? Thanks.

@mathieujobin
Copy link
Collaborator

could this validator do what you want ?

class Apipie::Validator::CollectionValidator < Apipie::Validator::BaseValidator
  def self.build(param_description, argument, _options, _block)
    if argument == :collection
      new(param_description, argument)
    end
  end

  def initialize(param_description, _argument, _options = {})
    super(param_description)
    @items_type = param_description.options[:of]
  end

  def validate(values)
    return false unless process_value(values).respond_to?(:each) && !process_value(values).is_a?(String)
    values.all? { |v| valid_value?(v) }
  end

  def process_value(values)
    return if values.blank? && !param_description.required
    values.map do |value|
      sub_validator.process_value(value)
    end
  end

  def description
    "Must be an array of #{items_type}"
  end

  def expected_type
    "array"
  end

  private

  def sub_validator
    @sub_validator ||= Apipie::Validator::BaseValidator.find(sub_param_description, items_type, {}, nil)
  end

  attr_reader :items_type

  def sub_param_description
    Apipie::ParamDescription.new(param_description.method_description,
                                 param_description.name,
                                 items_type)
  end

@notfelineit
Copy link
Author

@mathieujobin I think so! Does that validator exist in the apipie codebase, or is it something I should add to our own codebase?

@mathieujobin
Copy link
Collaborator

I haven't took the time to write tests in order to integrate it. So it lives in my application

@notfelineit
Copy link
Author

@mathieujobin Appreciate that you shared it then, Mathieu. I'll integrate this into our codebase!

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

No branches or pull requests

2 participants