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

Type specs does not work with nested schemas #424

Closed
igor-alexandrov opened this issue Sep 21, 2018 · 3 comments

Comments

@igor-alexandrov
Copy link
Collaborator

commented Sep 21, 2018

BridgeExperienceSchema = Dry::Validation.Params do
  configure do
    config.type_specs = true
  end

  required(:repair_value, LendingOne::Types::Currency).filled
end

This works as expected:

r = schema.call(repair_value: '  $1   ', transaction_purpose: 'purchase')
=> #<Dry::Validation::Result output={:repair_value=>#<BigDecimal:7f950901ba58,'0.1E1',9(18)>}>

repair value is preprocessed to Currency type.

class SharedSchema < Dry::Validation::Schema
  configure do
    option :application

    def bridge?
      application.bridge?
    end

    def rental?
      application.rental?
    end

    config.type_specs = true
  end

  define! do
    required(:transaction_purpose, :string).filled(included_in?: Loan::TRANSACTION_PURPOSES)
    required(:required_closing_date, LendingOne::Types::ShortDate) {
      filled? & gt?(Date.parse('1900-01-01'))
    }
    optional(:description, :string).maybe(:str?)
    optional(:refinance_proceeds_purpose, :string).maybe(:str?)

    required(:repair_value, LendingOne::Types::Currency).filled#.maybe(:number?)
  end
end

BridgeExperienceSchema = Dry::Validation.Params(SharedSchema) do
  configure do
    config.type_specs = true
  end
end
r = schema.call(repair_value: '  $1   ', transaction_purpose: 'purchase')
=> #<Dry::Validation::Result output={:repair_value=>"  1   ", :transaction_purpose=>"purchase"}>

This does not work.

@flash-gordon

This comment has been minimized.

Copy link
Member

commented Feb 6, 2019

this will work fine in the next release

schema = Dry::Schema.Params { required(:bar).value(Types::Params::Integer) }

child_schema = Dry::Schema.define(parent: schema)

2.6.1 :001 > child_schema.(bar: '123')
 => #<Dry::Schema::Result{:bar=>123} errors={}>
@solnic

This comment has been minimized.

Copy link
Member

commented Feb 6, 2019

Just for the record:

Dry::Schema.Params { required(:bar).value(Types::Params::Integer) }

# ...is the same as

Dry::Schema.Params { required(:bar).value(:integer) }

...because Params means types are inferred from params.* type group.

@flash-gordon

This comment has been minimized.

Copy link
Member

commented Feb 6, 2019

right, the point was to show arbitrary types supported

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.