Skip to content

Commit

Permalink
Ensure callback constraint methods are not added as getters (#132)
Browse files Browse the repository at this point in the history
* Bump versions
  • Loading branch information
Blacksmoke16 authored Dec 27, 2021
1 parent 177c6a0 commit e7a8e6b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 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.6
version: 0.1.7

crystal: '>= 0.35.0'

Expand Down
30 changes: 29 additions & 1 deletion spec/validatable_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ private class Obj
property name : String = ""
end

private class InstanceCallbackClass
include AVD::Validatable

@[Assert::Callback]
def validate(context : AVD::ExecutionContextInterface, payload : Hash(String, String)?) : Nil
end
end

private class ClassCallbackClass
include AVD::Validatable

@[Assert::Callback]
def self.validate(value : AVD::Constraints::Callback::ValueContainer, context : AVD::ExecutionContextInterface, payload : Hash(String, String)?) : Nil
end
end

describe AVD::Validatable do
describe ".load_metadata" do
it "should manually add constraints to the metadata object" do
Expand All @@ -34,7 +50,7 @@ describe AVD::Validatable do
end

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

Expand All @@ -45,5 +61,17 @@ describe AVD::Validatable do
it "is defined when included directly into non-abstract types" do
Obj.validation_class_metadata.constrained_properties.should eq ["name"]
end

it "properly registers instance method callback constraints" do
constraints = InstanceCallbackClass.validation_class_metadata.constraints
constraints.size.should eq 1
constraints.first.should be_a AVD::Constraints::Callback
end

it "properly registers class method callback constraints" do
constraints = ClassCallbackClass.validation_class_metadata.constraints
constraints.size.should eq 1
constraints.first.should be_a AVD::Constraints::Callback
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.6"
VERSION = "0.1.7"

# :nodoc:
#
Expand Down
4 changes: 2 additions & 2 deletions src/constraints/callback.cr
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
# The callback method can also be defined as a class method.
# Since class methods do not have access to the related object instance, it is passed in as an argument.
#
# That argument is typed as `AVD::Constraints::Callback::Value` which exposes a `AVD::Constraints::Callback::Value#get`
# That argument is typed as `AVD::Constraints::Callback::Value` instance which exposes a `AVD::Constraints::Callback::Value#get`
# method that can be used as an easier syntax than `.as`.
#
# ```
Expand All @@ -90,7 +90,7 @@
# SPAM_DOMAINS = ["fake.com", "spam.net"]
#
# @[Assert::Callback]
# def self.validate(value : AVD::Constraints::Callback::Value, context : AVD::ExecutionContextInterface, payload : Hash(String, String)?) : Nil
# def self.validate(value : AVD::Constraints::Callback::ValueContainer, context : AVD::ExecutionContextInterface, payload : Hash(String, String)?) : Nil
# # Get the object from the value, typed as our `Example` class.
# object = value.get self
#
Expand Down
2 changes: 1 addition & 1 deletion src/metadata/class_metadata.cr
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Athena::Validator::Metadata::ClassMetadata(T)
{% for constraint in AVD::Constraint.all_subclasses.reject &.abstract? %}
{% ann_name = constraint.name(generic_args: false).split("::").last.id %}

{% if ann = m.annotation Assert.constant(ann_name).resolve %}
{% if ann_name != "Callback" && (ann = m.annotation Assert.constant(ann_name).resolve) %}
{% default_arg = ann.args.empty? ? nil : ann.args.first %}

class_metadata.add_getter_constraint(
Expand Down

0 comments on commit e7a8e6b

Please sign in to comment.