Skip to content

Instantly share code, notes, and snippets.

@rosskevin
Last active July 31, 2016 16:38
Show Gist options
  • Save rosskevin/45173b31ecd0d833ae129716dd15ada9 to your computer and use it in GitHub Desktop.
Save rosskevin/45173b31ecd0d833ae129716dd15ada9 to your computer and use it in GitHub Desktop.
LINE_ITEM = Validations::Schema.new_schema do
required_id(:product_id)
required(:quantity).value(:number?, gteq?: 0)
optional(:period_start_at).maybe(:time?)
optional(:period_end_at).maybe(:time?)
end
SCHEMA = Validations::Schema.new_schema(BaseDocument::SCHEMA) do
required(:line_items).each(LINE_ITEM)
end
contract do
property :deferral_threshold
collection :line_items, populate_if_empty: LineItem do
property :product_id
property :quantity
property :period_start_at
property :period_end_at
end
end
require 'i18n'
require 'reform/form/dry'
module AfCore
module Validations
module DslExtension
#
# Setup our own macros here (much shorter to use)
#
# @see dry-rb/dry-validation extending_dsl_spec.rb
def required_maybe_unique(name, *predicates, &block)
required(name).maybe({unique?: name}, *predicates, &block)
end
def required_unique(name, *predicates, &block)
required(name).value({unique?: name}, *predicates, &block)
end
def required_maybe_id(name, *predicates, &block)
required(name).maybe(:id?, *predicates, &block)
end
def required_id(name, *predicates, &block)
required(name).value(:id?, *predicates, &block)
end
end
class Schema < ::Reform::Form::Dry::Validations::DrySchema # Dry::Validation::Schema
configure do |config|
config.dsl_extensions = DslExtension
config.messages = :i18n
# option :model, -> { model } # FIXME: form should be passed in, do we need anything?
def included_in_enum?(enum, value)
return false if value.nil?
if (value.kind_of?(String) || value.kind_of?(Symbol))
value = value.to_s
enum.keys.include? value
else
# int
enum.values.include? value
end
end
def id?(value)
!value.nil? && value.kind_of?(Integer) && value > 0
end
def email?(value)
!/[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}/i.match(value).nil?
end
def phone?(value)
# same as AfCore::Validations::Remote::PhoneValidator
Util::Phones.parse(value).valid?
end
def role?(value)
included_in_enum? Membership.roles, value
end
def money?(value)
value.instance_of? Money
end
def kind_of?(type, value)
value.kind_of?(type)
end
def unique?(args, value)
if args.is_a? Symbol
attribute = args
options = {}
else
attribute, options = args
end
ActiveRecordORM.unique?(form.model.class, attribute, value, options)
end
end
# create a new schema instance that extends our base schema
# @see https://github.com/dry-rb/dry-validation/blob/master/spec/integration/schema/defining_base_schema_spec.rb
def self.new_schema(base_schema = Schema, options={}, &block)
# allow skipping of passing base_schema
if base_schema.is_a? Hash
options = base_schema
base_schema = Schema
end
Dry::Validation.Schema(base_schema, options, &block)
end
end
end
end
@rosskevin
Copy link
Author

This debugger shot shows that reform populates the line item with valid properties, yet dry-validation finds an error.

debugger

@fran-worley
Copy link

Can you post your actual form so I can see your actual validation block?
Also, you know that you can put validation blocks inside collections so you don't need your required(:line_items).each(LINE_ITEM).

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