Permalink
Browse files

Automatically validate required fields

Closes #32
  • Loading branch information...
paulcsmith committed Aug 17, 2017
1 parent 5852d4b commit 489a40bbc7a5a0c8c2ebf2741cdd70660af71e06
Showing with 51 additions and 10 deletions.
  1. +18 −0 spec/form_spec.cr
  2. +6 −0 spec/support/task.cr
  3. +21 −4 src/lucky_record/form.cr
  4. +6 −6 src/lucky_record/model.cr
View
@@ -12,11 +12,29 @@ private class LimitedUserForm < User::BaseForm
allow :name
end
private class TaskForm < Task::BaseForm
end
describe "LuckyRecord::Form" do
it "generates the correct form_name" do
LimitedUserForm.new.form_name.should eq "limited_user"
end
it "add required_fields method" do
form = TaskForm.new
form.required_fields.should eq({form.title})
end
it "automatically runs validations for required fields" do
form = TaskForm.new
form.valid?
form.valid?.should be_false
form.title.errors.size.should eq 1
form.body.errors.size.should eq 0
end
describe "save_failed?" do
it "is false if the object is invalid and performed an action" do
form = UserForm.new(name: "")
View
@@ -0,0 +1,6 @@
class Task < LuckyRecord::Model
table :tasks do
field title : String
field body : String?
end
end
View
@@ -57,19 +57,33 @@ abstract class LuckyRecord::Form(T)
def set_{{ field[:name] }}_from_param(value)
cast_result = {{ field[:type] }}.cast(value)
if cast_result.is_a? LuckyRecord::Type::SuccessfulCast
_{{ field[:name] }}.value = cast_result.value
{{ field[:name] }}.value = cast_result.value
else
_{{ field[:name] }}.add_error "is invalid"
{{ field[:name] }}.add_error "is invalid"
end
end
{% end %}
def fields
[
{
{% for field in fields %}
{{ field[:name] }},
{% end %}
]
}
end
def required_fields
{
{% for field in fields %}
{% if !field[:nilable] && !field[:autogenerated] %}
{{ field[:name] }},
{% end %}
{% end %}
}
end
def after_validate
validate_required *required_fields
end
end
@@ -112,9 +126,12 @@ abstract class LuckyRecord::Form(T)
def valid? : Bool
call
# TODO: run_auto_generated_validations
after_validate
fields.all? &.valid?
end
abstract def after_validate
def self.save(params)
form = new(params)
if form.save
View
@@ -1,10 +1,10 @@
class LuckyRecord::Model
macro inherited
FIELDS = [] of {name: Symbol, type: Object, nilable: Boolean}
FIELDS = [] of {name: Symbol, type: Object, nilable: Bool, autogenerated: Bool}
field id : Int32
field created_at : Time
field updated_at : Time
field id : Int32, autogenerated: true
field created_at : Time, autogenerated: true
field updated_at : Time, autogenerated: true
end
def_equals @id
@@ -67,14 +67,14 @@ class LuckyRecord::Model
{% end %}
end
macro field(type_declaration)
macro field(type_declaration, autogenerated = false)
{% if type_declaration.type.is_a?(Union) %}
{% data_type = "LuckyRecord::#{type_declaration.type.types.first}Type".id }
{% nilable = true %}
{% else %}
{% data_type = "LuckyRecord::#{type_declaration.type}Type".id }
{% nilable = false %}
{% end %}
{% FIELDS << {name: type_declaration.var, type: data_type, nilable: nilable.id} %}
{% FIELDS << {name: type_declaration.var, type: data_type, nilable: nilable.id, autogenerated: autogenerated} %}
end
end

0 comments on commit 489a40b

Please sign in to comment.