Skip to content

Commit

Permalink
Abstract validatable types (#16)
Browse files Browse the repository at this point in the history
* Handle including `AVD::Validatable` into abstract parent types
* Bump versions
  • Loading branch information
Blacksmoke16 authored Dec 14, 2021
1 parent 50aad09 commit 6fa7f36
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: athena-validator

version: 0.1.5
version: 0.1.6

crystal: '>= 0.35.0'

Expand Down
30 changes: 30 additions & 0 deletions spec/validatable_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,40 @@ private class ManualConstraints
def initialize(@name : String); end
end

private abstract class Parent
include AVD::Validatable
end

private class Child < Parent
@[Assert::NotBlank]
property name : String = ""
end

private class Obj
include AVD::Validatable

@[Assert::NotBlank]
property name : String = ""
end

describe AVD::Validatable do
describe ".load_metadata" do
it "should manually add constraints to the metadata object" do
ManualConstraints.validation_class_metadata.constrained_properties.should eq ["name"]
end
end

describe ".validation_class_metadata" do
it "is inheritted when included in parent type" do
Child.validation_class_metadata.constrained_properties.should eq ["name"]
end

it "is not defined for abstract types" do
Parent.responds_to?(:validation_class_metadata).should be_false
end

it "is defined when included directly into non-abstract types" do
Obj.validation_class_metadata.constrained_properties.should eq ["name"]
end
end
end
2 changes: 1 addition & 1 deletion src/athena-validator.cr
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ alias Assert = AVD::Annotations
#
# NOTE: See the related types for more detailed information.
module Athena::Validator
VERSION = "0.1.5"
VERSION = "0.1.6"

# :nodoc:
#
Expand Down
8 changes: 7 additions & 1 deletion src/validatable.cr
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ module Athena::Validator::Validatable
macro included
extend AVD::Validatable::Class

class_getter validation_class_metadata : AVD::Metadata::ClassMetadata(self) { AVD::Metadata::ClassMetadata(self).build }
macro inherited
include AVD::Validatable
end

{% unless @type.abstract? %}
class_getter validation_class_metadata : AVD::Metadata::ClassMetadata(self) { AVD::Metadata::ClassMetadata(self).build }
{% end %}
end
end

0 comments on commit 6fa7f36

Please sign in to comment.